Jumat, 08 Januari 2016

Simple Artificial Intelligence: Waypoints

Hi there everyone, happy new year 2016! I know I haven't been so active in the last one or two months, and I'm really sorry for that. I'm trying to make it up this year by making tutorials regularly (hopefully). Now, usually I'll continue from where I left off, but I think I'll start this year with a smaller tutorial. Hopefully this will kick my habit back up to writing more tutorials.

Okay, so let's start with making a simple tutorial: Waypoints. What are waypoints? They are small points that are positioned in your game for an object to follow. The objects are usually enemies that need to follow a certain route, and they follow these waypoints in order. There are several genres of games that can use this AI method in them, for example a shmup game can use this to make the route for enemy ships, an RPG can use this to make an enemy patrol system, and many others.

Let's start with an empty layout with a blue square for our player. For the graphics in this tutorial, I'll only use placeholder-worthy sprites, because I'll try to focus more on the code behind the game than the presentation. You can always change the graphics to fit your game later on. Resize it to 64x64 and add an 8 Direction behavior to it, because we want to move it around.

And then we'll make our enemy. The same as our player, we'll make it as a simple 64x64 sized square, just coloured it red now because we want the enemy to look scary. Then add a number instance variable named "targetPoint" to the enemy, this instance variable will hold the ID of the next waypoint this enemy must go to, this value will change to a new target after the enemy has arrived at the target waypoint. Don't forget that we want the enemy to move around so we'll need a behavior to do that, so add the bullet behavior and change the speed value to 200.



Right now, your layout should look something like this:

Our first prototype

Not much, huh? Well, we're about to get into the exciting part. Create a small 32x32 green square, this will be one of many of our waypoints, first add two instance variables: "currentID", and "nextID", both are numbers. The currentID is the ID of a waypoint, while nextID is the next destination the enemy will go to after it collide with this waypoint. The logic is like this: The enemy will go to a waypoint according to its "targetPoint" instance variable, and after colliding with the target waypoint, it will change its "target"Point" value to the waypoint's "nextID" value. The result is the enemy will follow waypoints from one ID to the next.

That's a great theory but to put that to the test we will need to fill the layout with some waypoints first. Copy and paste the waypoints and arrange it on the layout the way you want the enemy to move, for me I'll make it a kind of circular movement because I want the enemy to move around. Edit the values of the instance variables so that one waypoint's nextID is the same as next waypoint's currentID, it will make the enemy to follow the waypoints in order. If you want the enemy to move in a repeating way (like moving in a circular motion), make the last waypoint's nextID to be the first waypoint's currentID.

For now, my layout look like this:

Waypoints

Now, our layout's preparation is complete, the next step is coding the event. Go to the event sheet, and for our first event we want to check each and every waypoints for its "currentID" instance variable. So, add an event with a "For each waypoint" and a second condition "enemy targetPoint = waypoint.currentID". This will check every waypoints and see which one of them is the next waypoint target. The action is simply setting the bullet's angle of motion to the next waypoint, we know this by using the angle() expression from construct 2, we just need to supply the enemy object's X and Y position, and the waypoint object's X and Y position.

The second event we'll make is going to change the enemy's target waypoint after colliding with the current target waypoint. The first condition is a trigger "enemy on collision with waypoint", and the second condition is checking is the waypoint's currentID is the same as the enemy's targetPoint. If all conditions are met, then we simply set the targetPoint to be the waypoint's nextID.

And we're done! You can try running the game now, but make sure that the enemy's targetPoint's default value is set to one of the waypoint's currentID, if not then the enemy won't move to the next waypoint. By now, your event sheet should look like this:

AI code

Yep, I think that's it for today. At first I was thinking maybe we can do something with our character there but in the end it wasn't needed to make our AI. Maybe we could use it to shoot at the enemy or something. If you want to develop this AI further you can try to add other enemies that follow the waypoints in a different order (there's no rule that you have to start at waypoint number 1), or you can add other sets of waypoints and have other enemies to follow those waypoints side-by-side with this enemy.

If you want to download the source code (for whatever reason), you can grab it from my Drive. And if you like this tutorial and would like to support me, please consider visiting my Patreon Page. And with that, this tutorial is over! I'll see you at the next one, thank you and happy new year!

Tidak ada komentar:

Posting Komentar