Add Multiple Weapons

So, we built a basic gun that fires bullets, but what about adding something a little more exciting? How about a grenade that bounces and explodes when it hits stuff? We can easily add in new types of weapons just like we did with our bullet. Let’s set up the beginning of our grenade class after our EntityBullet. Add the following inner class to your player.js module:

EntityGrenade = ig.Entity.extend({


Just like with our bullet, we are ready to customize the grenade’s properties. We will need to give it a graphic and set the size and offset:

size: {x: 4, y: 4},
offset: {x: 2, y: 2},
animSheet: new ig.AnimationSheet( 'media/grenade.png', 8, 8 ),

Now let’s set up our collision detection:

type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.BOTH,
collides: ig.Entity.COLLIDES.PASSIVE,

Pay special attention to the fact that we are setting checkAgainst to TYPE.BOTH. What this means is that our grenade can collide with our zombie and our player. You’ll see how this works when we are ready to test our grenade later on. However, in order for our grenade to move and bounce, we will need to add a few additional properties. Add the following to your grenade class:

maxVel: {x: 200, y: 200},
bounciness: 0.6,
bounceCounter: 0,

Here, we are setting our grenade’s maximum velocity. Also, we are going to keep track of how many times it will bounce before blowing up, as well as its bounciness value. You can tweak these values once we enable the player to actually fire the grenade, so you can test what effect the bounciness value will have.

Let’s override the init() method with the following code:

init: function( x, y, settings ) {
    this.parent( x + (settings.flip ? −4 : 7), y, settings );
    this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
    this.vel.y = -(50 + (Math.random()*100));
    this.addAnim( 'idle', 0.2, [0,1] );

Here, we are going to determine the beginning x velocity based on a flip parameter that will be passed in via the settings object. Just like with our bullet, when the player fires the grenade we will set the player’s own flip value into a property of the settings object so that we know what direction to fire the grenade. It’s also incredibly important that we get the start x,y position offset correct. Since the grenade can collide with the player, we wouldn’t want it to fire the weapon and instantly blow up.

Next, we offset the y velocity by negative 50 plus a random number that ranges from 0 to 100, which will help add an arc to the grenade when it gets fired. The randomness makes sure that the player throws the grenade slightly differently each time. This, along with the fact that the grenade can also kill the player, will help balance the fact that this is a more powerful weapon. After that, we just set the idle animation to display sprites 0 and 1, which will loop through images of the grenade rotating as it flies through the air.

We are getting very close to testing out our grenade, but before we do, we’ll have to override the handleMovementTrace() method and write some logic to handle collisions, track the number of bounces, and remove the grenade from the display if it bounces too many times:

handleMovementTrace: function( res ) {
    this.parent( res );
    if( res.collision.x || res.collision.y ) {
          // only bounce 3 times
          if( this.bounceCounter > 3 ) {

This works exactly like our bullet, except that when a collision is detected, we increment the .bounceCounter by 1. If the .bounceCounter is greater than 3, we kill the grenade. We will talk more about the entity’s kill() method later.

Now that we can handle tracking bounces, let’s add logic when the grenade collides with an enemy. Like we did before in the EntityBullet class, we are going to override the check function, which gets called when a .checkAgainst group has been detected. Add the following function to your grenade class:

check: function( other ) {
    other.receiveDamage( 10, this );

Finally, we have increased the damage of the grenade so that it kills anything in a single hit. Now, all we need to do in order to have the player toggle between weapons is bind a new key to toggle between the weapons and then have the player swap between the correct one. Let’s add the following bind logic into our main.js init() function:

ig.input.bind( ig.KEY.TAB, 'switch' );

Now, if we go back into our player class, we will need a way to keep track of the current weapon. Add the following property to the beginning of our EntityPlayer class in the player.js file:

weapon: 0,
totalWeapons: 2,

From there, we can add a simple test to see when the player presses the weapon toggle button, so we can toggle the weapon property. Add the following code below where we test if the shoot button was pressed in the PlayerEntity update() method:

if( ig.input.pressed('switch') ) {
    this.weapon ++;
    if(this.weapon >= this.totalWeapons)
          this.weapon = 0;
    switch(this.weapon){          case(0):
               this.activeWeapon = "EntityBullet";
               this.activeWeapon = "EntityGrenade";

Now, when we go to spawn our weapon instance, we can simply check to see which weapon is set in the weapon property and spawn the correct instance.

We will also need to add a new property called this.activeWeapon to the top of our class:

activeWeapon: "EntityBullet",

Notice how we have to set the value to a string instead of a reference to the class itself? This will be evaluated correctly when the player class gets created. If we don’t make the default value a string, it will break the player class when it tries to load.

The last thing we need to do is update our shoot code in the player class with the following:

if( ig.input.pressed('shoot') ) { this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );

This simply spawns a new instance of any class you have set as the this.activeWeapon value. Now you are ready to test out your new grenade and switch between the two weapons.

The player can now throw grenades.

Figure 4-22. The player can now throw grenades.

You may have noticed that the player still looks like he has a gun in his hand and that there really aren’t any visual changes when you switch weapons. We can quickly fix this by modifying how we set up our player’s animations in the init() method. Go ahead and delete the four lines of code where we set up the idle, run, jump, and fall animations. Then add the following in its place:


Now we will create a new method called setupAnimation(), which takes our current weapon ID as an offset. Here is the code to add below the init() method:

setupAnimation: function(offset){
    offset = offset * 10;
    this.addAnim('idle', 1, [0+offset]);
    this.addAnim('run', .07, [0+offset,1+offset,2+offset,3+offset,4+offset,5+offset]);
    this.addAnim('jump', 1, [9+offset]);
    this.addAnim('fall', 0.4, [6+offset,7+offset]);

This is just like our original animation setup, except we now take the weapon ID (which becomes an offset), multiply it by the total number of player frames with a weapon, and add it to each animation. Let’s look at the player sprite sheet so you can see what’s going on.

The player sprites with a gun and without one.

Figure 4-23. The player sprites with a gun and without one.

As you can see, we have 10 sprites holding a gun and 10 sprites without the gun. By offsetting the animation by 10 frames, we can easily switch between the different sets of sprites. This is a common trick, and one we will use later on when we add death animations. For now, we just need to add one last line of code to help update the player graphics when we switch weapons. Add a call to this.setupAnimation() at the end of where we test for the weapon switch key press:

          this.activeWeapon = "EntityBullet";
          this.activeWeapon = "EntityGrenade";

Now when you test and hit the Tab key, you should see the player’s animation change based on the weapon he is using (Figure 4-24).

Now you can see the player’s sprite update when switching between weapons.

Figure 4-24. Now you can see the player’s sprite update when switching between weapons.

From here, you should be able to add even more weapons to your game by simply using the above pattern and building upon it.

Get Building HTML5 Games with ImpactJS now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.