Wednesday, 15 August 2012

Post 6 - Hiding

The game we are building wouldn't be a sneaking game (which it will be) if you wouldn't be able to hide. And this is exactly what we are going to do in this post. We are giving the player a way to hide behind certain objects so he can pass by enemies undetected. To start off let's create an object the players can hide behind.

CREATING THE OBJECT
Create a new sprite and the same way we made the ducking sprite but give it another color. Then make an object and link the sprite to it. Don't forget your naming conventions! My objects to hide behind is a crate, so I've called it obj_crate.

SNAPPING BEHIND THE OBJECT
We want the player to be able to hide behind this object. We'll do this by writing a piece of code which ensures that whenever the player ducks while in collision with a crate he will snap to the precise position of the crate and will not be killed by the enemy or his sightcone.

Let's start with the position snapping. For this we write the following bit of code in the crates Collision Event with player1.

if (obj_char.sprite_index = spr_char_low)
{
    obj_char.x = x;
    obj_char.y = y;
}

If there is collision between player 1 and the crate the code checks if the player sprite is the ducking sprite, meaning the player is ducking. If this is true the players x and y position are made exactly the same as the crate's x and y position. 

NOT DYING WHILE HIDING
Now that we've got the snapping down let's code that the players don't die by the sightcone or enemy when hiding behind a crate. We do this by first creating hide_status in the player's Create Event.

hide_status = false;

Then we set hide_status to true whenever the player is hiding behind a crate by editing the above code.

if (obj_char.sprite_index = spr_char_low)
{
    obj_char.x = x;
    obj_char.y = y;

    obj_char.hide_status = true;  
}
else
{
    obj_char.hide_status = false;
}

Next we edit the code in the enemy Step Event which kills the player whenever he touches the sightcone and include a check to see if the player is hiding or not. So now it checks the collision line, player collision with scone and hiding status before deciding whether to kill the player or not.

if (collision_line(x,y,obj_char.x,obj_char.y, obj_tile,1,0) < 1 && scone.char_collision = true && obj_char.hide_status = false)
{
    obj_char.destroy = true;
    scone.char_collision = false;
}
scone.char_collision = false;

And finally we edit the enemy Collision Event with player 1 (the piece of code you had to write on the last post's assignment) to include a check if the player is hiding or not. 

if obj_char.hide_status = false
{
    obj_char.destroy = true;
}
else
{
    obj_char.destroy = false;
}

Whenever there is collision between the enemy and the player this code checks if the player is in hiding. If not the player is destroyed.

We now have a fully functioning hiding mechanic!

ASSIGNMENT: CAN'T HIDE BEHIND SAME CRATE
Let's make our hiding mechanic a tad fancier. By editing the code in the crate's collision event with the players, make it impossible for both players to hide behind the same crate. Don't forget to add all the functions you've written above to player2 as well!

Take a look at how I did it (including assignment answers) here.