Hello everyone! Today we are going to talk about Enemy AI! At the end of this tutorial, you have made an enemy AI Agent for a top down game we've just made!
But what is AI, and how will this AI work? AI stands for Artificial Intelligence, and it basically processes an input, and decides on an output. If we made some kind of Monster AI for a game about Monsters, then we'd make the AI of the monster, say for like, guard the door and monitor any changes in their behavior.
Let's say that the monster sees that a door is closed. If the player were to say, open it, then the monster runs towards that sound. If the door is opened, the monster senses that the player is present, and will start hunting the player down in rooms connected to that specific door.
So now we know how AI works and how we'd implement them into our game! But how does OUR AI work?
Our AI works by this: First, when it catches you, it places a marker. The next run, it goes TO that marker and sees if you are there. If you aren't, it searches for you and hunts you down. Simple enough? Then let's go make an AI for our game!
Start off by running Snap! by pressing the "Run Snap!" button on your screen. It should be at the top banner of the page.
Next, you are going to make a simple map for your game. I've made a maze.
Next, you will make two sprites, one called "Player" and the other one called "Enemy AI Agent"
Next, code some simple movement for the Player.
But the player can go through walls! We don't want the player to go through the walls! So make 2 variables that we will call "Last X" and "Last Y"
Next, you will add collisions so the player won't be able to go through walls and noclip
Now, after we are finished with the Player movement scripts, let's move over the the AI script!
So our AI will go to a keyspot, or the area we last got caught. So to do that, we have to make a marker so that our AI agent will know where to go the next run we get caught.
To do that, we will make a sprite. So first, make a sprite, and hide it. The AI will still know that the sprite is there even though it is not visible. Call this sprite "LastCaught"
Next, we will make the AI agent move across the whole map. Unfortunately, Snap doesn't come with a "[Glide () secs to (random position)]" block, so we have to do some math here. Luckily, you can just use my calculations in the screenshot below instead of doing your own math!
This should be able to give our Enemy AI Agent some kind of random movement! But we aren't done yet.
The AI can only move at random places. We want a learning AI, not an enemy. So let's add some code so that our AI can learn from it's past experiences with you!
First, you will add an IF statement for the player. We will add some code so that our Player knows if it is caught. If the player IS caught, then we will broadcast a "Caught" message so that the other sprites (Like the Enemy AI) would go back to it's resting spot, the "LastCaught" object will go to where the player was last caught, and the player can go back to it's resting spot too.
This makes sure that if the player touches the enemy AI agent, it's lights out.
Now, let's move on to the enemy AI, as that's what this tutorial is talking about. So first, add a script for the LastCaught game object so that the AI would go towards that LastCaught area.
This script means that once we get caught, the "LastCaught" marker will mark the spot the player was caught in the last run. The reason why we need a "[wait (0.3) secs]" block is because if we don't have that block, then the marker will mark the player spawn location, not the caught location.
Now let's make 2 messsages, one called "BeforeCurrentState" and one called "CurrentState". These will be the blocks of code that will indicate when to hunt and search for the player and when to go room to room in search for the player.
Now add these blocks of code for "BeforeCurrentState" and "CurrentState".
BeforeCurrentState is the state of which the AI will go room to room in our maze and try and find us. CurrentState is when the AI is hunting and searching to see if the player was where it last got caught.
For spawning the AI back to it's resting place, we will add these blocks of code:
Great! Now we have finished our AI! And that means that you have officially finished a Basic Enemy AI tutorial for a top-down game! Thank you for watching, and I can't wait to see what YOU make with this AI!