Making Enemies


On Thursday I was working on my enemies and what they do in the basic game. I was working on the wolf enemy. I had some basic ideas for what I wanted them to do.

I wanted them to wander around till the player gets into their "aggro range" and when once the player is in that aggro range for the wolf to cercal them and attack them at certain intervals, they should only attack the player if they are in their line of sight. I also wanted them to only spawn in the "farm crops" object and for them to change the sprite of the crop to show the player that its there. 

So to start off with I had to make the wolfs move, I could have just used the drag and drop features that are in game maker studio but, wheres the fun in that. I chose to code the movement of the wolf by using scripts.
This is the basic code for the movement, this makes the wolf object move towards the players x and y coordinates. I tried to make the enemy circle the player by adding 64 and subtracting 64 from the players x and y. It didn't work and I'm still trying to think how to fix it. "enemySpeed" is quite obviously the speed of the "wolf". I have set it as a variable so that I can change it within the game if I need to.

To handle the movement of the "wolfs" I have made 2 scripts to either make the enemy move independently of the player and one were they are following the player. They both check two variables that have been set in the wolf's create event.


It checks the distance between the player and the enemy and if that distance is more than the "aggro range" it will stay in its static state meaning it will move around by itself. however if that distance less than the "aggro range" it will change state and run the follow script,


where it then keeps checking to see if the distance has gotten greater than the aggro range. When it is greater the wolf then stop following the player.

Now on to the spawning of the wolf. I only wanted the wolfs to spawn in the crops. to do this I made a control object which controls how many of the wolfs will spawn, where they spawn and how the time between each spawn. It uses an alarm to set the spawn rate to a random number between 60 and 180 which is somewhere around 2 to 6 seconds. Any way to make the wolfs only spawn inside of the "farm crops" I got a random variable between the crops x and y and made it so the instance is made inside of the object. I then reset the clock to the spawn rate and the prosses repeats.

To make the crops when then wolf is inside of them, I simply dragged and dropped a collision event and made it so the sprite would change if the wolf was touching it.



Comments