Carbine

2011/01/01

4 minute read

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.

https://youtu.be/lpYcCAgKMxs

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

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

Monotonic Trajectory

Projectile direction angle

Projectile Trajectory

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.