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
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
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.
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.
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.
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.
- 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
- 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
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.