## Carbine

Game development
dendiz
Posts: 234
Joined: Wed Oct 10, 2018 3:48 am

### Carbine

---
title: Carbine
Date: 2011-01-01
description: an HTML5 game prototype
---

Carbine is an implementation of the game Crimsonland developed by 10tons. It’s a shoot’em up style game where you start the map in the middle and all sorts of zombies, spiders, monsters start creeping up towards you and you have to shoot them and keep running away from them. Here’s a video of the game if you don’t know it already.

Here are some screenshots

Source code

The source code is released under any public domain license, i really don’t care. Use it, modify it, do what ever you want with it. You can find it on http://github.com/dendiz

Zombie movement

Each zombie has x and y coordinates that get updated at each game tick. The new x,y coordinates are calculated as x = velocity * math.sin(direction) and y = vel * math.cos(direction) where direction is the angle that the zombie is facing. This angle has to be towards the player at some times if we ever want the player to drop dead. The angle is calculated as alpha = atan2(player.x-zombie.x, player.y-zombie.y) derived from some simple vector calculations
57f45260c7ec9.png (38.14 KiB) Viewed 87 times

Blast effect when firing

Implemented by swaping the sprites of the original player with sprites that have a blast at the end on the gun at each move() function call for the player if the fire_anim variable is enabled. The fire_anim variable is set to true at the fire() method of the player and a timer is set to disable in after 10ms have passed.

Player keybinding

the a,s,d,w and keys are bound using the javascript evet handler. The a,s keys set the current player Y velocity to +maxspeed and -maxspeed and the a,d keys set the X velocity to -max and +max. They are triggered by the keydown event, and at each move() function call the coordinates are updated according to the value of the velocity variable. The keyup event sets these variables to zero. This way if the key is held in a pressed state the actor will keep moving.

Player movement

the direction of the player is calculated by taking the atan2 of the current player x,y and the current mouse x,y so that the player is always facing towards the cross-hair. If the velocity of the player is 0 than the current player sprite isn’t swapped because we don’t want the player to perform a moving animation otherwise the seconds that have passed from the game start is calculated form the current frame number and FPS and the correct sprite is selected form the sprite array of the player.

Player rendering

The player is rendered on the canvas by translating the canvas to the player x,y and rotating the canvas by the player direction and drawing the current sprite from the sprite index.

Player weapon firing

If the bullet count is 0 the weapon is reloaded. Then a bullet object is created an pushed on the projectiles array.

Projectile system

Projectiles are bullets fired by weapons. A weapon may fire multiple projectiles e.g shotgun. Rockets are also projectiles.The projectiles are stored in the C.projectiles array when they are displayable on the screen. Different types of projectiles may be present with different velocities and different damage factors. Location of the bullet is calculated each frame by the main event loop. Collision detection is performed at each frame with each zombie on the screen. If a collision is true the projectile is removed from the C.projectiles array.

Effects
• projectiles are small but visible to the user
• projectiles should leave a trail of smoke or something to indicate the path. this trail is calculated by saving the x,y at the bullets creation time and drawing a line gradient with a fading alpha form the current x,y position of the bullet to saved position
Monotonic Trajectory
• at the time of firing the bullet life is 0 and incremented each frame
• the x,y of the projectile is the player x,y at the time of firing.
• the direction angle phi is calculated as the angle between the x-axis and the line between the player x,y and an x,y at a random point in the crosshair circle
at each frame the next x,y is current x,y + sin(direction)/cos(direction) * velocity * bullet life

Projectile direction angle
57f4527f8642c.png (30.09 KiB) Viewed 87 times

Projectile Trajectory
57f45298050e8.png (22.88 KiB) Viewed 87 times
Accuracy

The crosshair of the player widens with consecutives shots less than N seconds apart. The trajectory of the projectile intersect at a random point within this widend cross hair circle.