Because we are creating a sneaking game one of the main goals will be to stay undetected. To indicate when you are in or out of an enemy's sight we create a sightcone which we will attach to the enemy.
CREATING THE CONE
Let's start by actually creating the sightcone the same way we created other sprites and objects. Create a new sprite and make it 120x48. Make it look like the image below. Give it precise collision checking and set it's origin at x = 0 and y = 24 (left/middle). Give it an appropriate name.
ATTACHING THE CONE
Once the cone is created we can attach it to the enemy. We do this in the enemy's Create Event with the following code.
scone = instance_create(x,y,obj_scone);
We have created scone (sightcone) at the x and y of the enemy and used the sightcone object. The sightcone is now attached to the enemy but will not turn with the enemy yet. For this we need the following piece of code in the enemy's Step Event.
scone.image_xscale = enemy_direction;
scone.x = x;
scone.y = y-8;
Because we are referring to scone from the enemy object, we write scone. infront of our code. So when we write scone.image_xscale we are talking about the xscale of scone and not the xscale of the enemy itself.
The image_xscale is linked to enemy_direction. Meaning if the enemy walks to the right (direction = 1) the scale of the scone will be 1 (original image, facing right). If the enemy walks to the left (direction = -1) the scale will be -1 and the image will be mirrored, facing left.
The scone.x = x and scone.y = y-8 position the scone according to the enemy x and y. So now the scone will be drawn 8 pixels above the enemy center.
Next create an object for it with an appropriate name and link the sprite to the object. This sightcone is quite large and it can be quite annoying if objects in the level are hidden behind it. This is why we will give it an alpha of 0.5 so we can see partly through it. To do this we must add a Draw Event to the object. In this Draw Event we write the following piece of code which draws the cone from scratch and gives the ability to edit it in various ways.
draw_sprite_ext(spr_scone,0,x,y,image_xscale,1,0,c_white,0.5);
In the brackets the following aspects of the image must be given: which sprite must be drawn, which subimage of the sprite must be drawn, x position, y position, x scale, y scale, rotation, color and alpha. If the color is set to white it means it will be shown as its original color as given in the sprite editor.
MAKING IT KILL
To make it more interesting we'll have the sightcone kill players straight away once they touch it. To do this we must first add a Create Event to the sightcone object and type in the following code.
char_collision = false;
We have now created char_collision and defined it as false when the cone is created. Next we create a
Collision Event in the sightcone object with player1 as the collision target. In here we type the following code.
char_collision = true;
Now we go to the enemy object's Step Event. Here we will decide what will happen if collision between player and sightcone is true. We do this with the following code.
if scone.char_collision = true;
{
obj_char.destroy = true;
scone.
char_collision = false;
}
This code is quite simpel. If scone collides with player1, set player1's destroy value to true and set collision to false. This last bit is important, if you don't set it back to false after the player has died, he will keep on dying because the collision value will still be true. Player1 doesn't have a destroy value yet though. In the assignment below you will set that value and actually make the player die.
We now have a working collision code (if you take in account the assignment below) except for one problem. What if the player has collision with the sightcone but there is a tile in between the player and the enemy? This should mean the player should not be seen by the enemy shouldn't it? To do this we use the following code.
collision_line(x,y,obj_char.x,obj_char.y, obj_tile,1,0);
This piece of code draws an imaginary line between a given x and y to another given x and y and checks if it collides with a certain object. It also needs to know prec and notme. Prec means precise and refers to if you want to check the precise sprite or the bounding box. Notme refers to if you want to check the for the item in which you have set the code as well. In our case we use the enemy's x and y and player1's x and y, check for the tile object, check the precise sprite (1 = true) and do not check for the enemy object in which the code is made (0 = false).
Now we'll use this code in combination with our previous destroying code.
if collision_line(x,y,obj_char.x,obj_char.y, obj_tile,1,0) < 1 && scone.char_collision = true
{
obj_char.destroy = true;
scone.char_collision = false;
}
scone.char_collision = false;
This is the exact same as our code before except now it checks if the collision_line check is false. So if there is no collision with a tile and the collision line AND the player has collision with the sightcone, the player will be destroyed and the sightcone's player collision will be set to false. We have also added a second line which sets collision with player1 to false. This one is to prevent the following scenario: Player 1 is behind a tile. So there is no line of sight but there is collision. Because there is collision scone.char_collision is set to true. If the player would later move away when there is no longer collision but there is line of sight, the player will still be destroyed.
ASSIGNMENT: ACTUALLY DESTROYING PLAYERS
So we see the above code changes the destroy value in obj_char (player1) to true but obj_char does not actually have a destroy value yet. With the things you have learned create a destroy value for the player and make sure when it is true, the player respawns at his spawn location using the following code.
x = xstart;
y = ystart;
After that create a piece of code which destroys he player when he is touched by the enemy. Then Do everything you've done for player1 for player2 as well.
Take a look at how I did it (including assignment answers) here.