Retro Platformer tutorial Part 3 – Getting hurt and dying

In Part 2 we went through:

  • creating a bullet by extending the o_projectile object
  • adding input to shoot said bullets
  • adding animations related to shooting

Now that our hero can shoot, let’s make her receive damage!

Adding a new HURT behavior

Mega Woman only has one behavior so far (STATES.NORMAL).

She can already receive damage as it is handled out of the box by projectile and enemy objects in Super State Engine. However, we need to manage what happens when she receives damage and to do this we need to create a new state.

Actor objects change state when they are damaged. The state they go in is defined by hurt_state in their Variable Definitions and is set by default to be STATES.HURT.

So we need to create that state:

  • Open o_megawoman’s Step event
  • Under the case STATES.NORMAL block (after the break), add the new HURT state block:
case STATES.HURT:

    break;

In this state, we’ll be checking her hit points to see if she should die or not and then script a simple behavior showing her being hurt.

Scripting the hurt animation

Add the parts in bold to the STATES.HURT block:

case STATES.HURT:

    state_play("hurt");
    state_set_speed(1*damage_dir_h,0);
    state_pause(20);
    state_become(STATES.NORMAL);

    break;

Explanations

In Super State Engine, state_* functions are played in the order they are written. This is handy to script animations, cutscenes or complex behaviors. Let’s see what happens in this sequence:

  • The first line plays the “hurt” animation (defined in Mega Woman’s Create event in part 1).
  • The second line sets her speed to 1 in the direction from which she’s taken damage (1 * damage_dir_h). The value of damage_dir_h (1 or -1) has been set by the enemy or projectile that damaged the actor.
  • The third line simply adds a 20 steps pause. These three lines together mean that our hero will play the hurt animation while moving at a constant speed of 1 in the direction of the damage she received for 20 steps.
  • Once this pause / delay has passed, she moves to the fourth line which sends her back to her STATES.NORMAL state.

Checking hit points

Add this in the STATES.HURT block:

case STATES.HURT:

    if(hp <= 0)
        become(STATES.DIE);

    state_play("hurt");
    state_set_speed(1*damage_dir_h,0);
    state_pause(20);
    state_become(STATES.NORMAL);

    break;

Explanations

What this does is when our hero receives damage (and thus enters her STATES.HURT state), she checks how many hit points (hp) she has left and if it’s equal to or less than 0 she instantly goes to her STATES.DIE state instead (we’ll add that soon).

All o_actor objects (which our o_hero object is a child of) have a hp variable. In the Create event of the object, hp is set to hp_max so it starts with full health. By default hp_max is set to 1 but can be customized in the object’s Variable Definitions.


Adding a dying state

Upon the hero dying we’ll show a small explosion animation, wait for a short time then restart the level. Let’s start by creating the explosion effect.

  • Import and set up the sprite
    • From the Mega Woman asset pack, drag “spr_fx_explosion_quick_strip6.png” into your project
    • Open the new sprite
    • Remove “_strip6” from its name
    • Set the origin to “middle center”
    • Set the animation speed (Fps) to 24
  • Create the effect object
    • Create a new object named “fx_explosion
    • Set its sprite to “spr_fx_explosion_quick
    • Set its parent to “o_fx

Once the FX object is created, add this code to Mega Woman’s Step event after the STATES.HURT block we created previously:

case STATES.DIE:

    state_set_var("visible", false);
    state_create_instance(fx_explosion,x,y,depth);
    state_pause(60);
    state_create_instance(o_transition,0,0,0,{t_room:rm_level_restart});

    break;

Explanations

This creates the die state we need when the hero’s hit points reaches 0 or below. What this does is:

  • Set the hero object to be invisible
  • Create the fx_explosion at the hero’s position
  • Wait for 60 steps (one second)
  • Create a transition object with “rm_level_restart” as the target room

The o_fx object (parent of the fx_explosion object we created) is a very simple object that simply waits for its animation to reach the last frame before destroying itself. It offers a quick way to add FX animations to your game to juice it up.

The o_transition object is also very handy. For now it only has a single animation but I might add more in the future. I invite you to modify it to your liking! It has a “progress” variable that gets up from 0 to 100. When it reaches 100, it send the game to the target room then the “progress” value gets back down from 100 to 0. You can get creative and use this value to make your own animations!

The room “rm_level_restart” is a very simple room. In its Creation code it launches the level_restart() function which essentially clears the level before sending the game back to “rm_level_launch“, starting the selected level again from the start (or saved checkpoint).

Conclusion

So now Mega Woman can run, jump, shoot, get hurt and die, in which case the level is restarted.

If you have not changed the demo level that comes with importing Super State Engine into your project, you can test what happens when Mega Woman touches the little enemy (that I like to call a “gamboo”). You should also be able to kill the gamboo by shooting at it!

Now would be a good time to try to change things. Change Mega Woman’s hit points, speed, jump speed. Maybe try to change her hurt behavior or create new animations effects. Have fun!

In the next part, we’ll add a sliding move.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *