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.