The Legend of the Tomato Quest Tutorial - The Healer II

Pointing our AI
Before we do anything, we need to make a change to the SetAnimationFrames method in the Character class in Character.cs. Change the declaration so that it looks like the following.

Next we need a method that points our AI into the right direction, basically in the direction of the Player.

Add the following method to the AIController class in AIController.cs source file:

Basically, what this command is doing is changing the animation of the AI, depending on where the player is. If the player is above the AI, the AI's animation is changed to look towards the top.

Moving our AI
This next method is what we will use to move our AI, if they are able to move.

Add the following method to the AIController class in the AIController.cs source file:

This code is very similar to the code that points the AI towards the player. This method checks if the player is above, below, to the left, or to the right of the player, and moves the AI towards him.

See how in both methods we are using the methods we alreadly made in the Characters class. This is an important thing because reusing as much code as possible reduces how much code is in your game.

Player vs AI Collision
There are 2 methods require to implement the collision between our AI and our Player.

We'll start with the collisions between the Player and the AI.

Add the following method to the AIController class in the AIController.cs source file:

This method goes through each AI in the list that is passed in, and check if any of them collide with the player. If one of them collides, the Player is moved back a step, and the routine is terminated.

Next we have the AI to Player collision detection method.

Add the following method to the AIController class in the AIController.cs source file:

This method checks if the AI passed into the method collides with the player, and if so, the AI is moved back 1 step.

The difference between these methods is that the first method takes in an List of AI while the second takes only 1 AI. You will see why this is need as we continue.

Completing the Update AI Method
The UpdateAI method we made before only drew Sprites to the screen. We need to add the code that will update their movements, animations, and collisions.

Modify the UpdateAI in the AIController class within the AIController.cs source file to look like the following:

As we can see, the method has changed alot. We still have our drawing command, but its right at the bottom.

This method first, goes through all the AI in the List of AI that is passed in, and then checks if each AI is on the screen. We do not want to do anything with AI characters that are not on screen, and it also helps to optimize the game, to help make it run faster.

If the sprite is indeed on the screen, we then check if the AI can move, and if so we call the MoveAI command. Otherwise we call the PointAI Command.

The next Command, AICollideWithPlayer, checks if the AI has collided with the player, and moves the AI back 1 step, so that it is no longer colliding.

Next we use UpdateCharacterAnimation to update the animation state of the AI, so that the AI can animate.

And of course, we Draw the AI to the screen.

Finishing Touches
Now that we have a command that can stop the Player from colliding into the AI, we'll add the line of code that will handle this for us.

In the Game Class in Game.cs source file, within the Run method, add the following line:

And that's it, Compile and run the game, and now you'll see that the Healer's will try to point towards you, and when you try to run into them, you will stop due to the collisions we made.

I haven't uploaded a screen shot because there isn't much to look at, and screen shots don't really show the collisions.

Project so far
The source code for the project up until this point can be found [[Media:RPGTutorial8.zip|Here]].

It is strongly recommended you read through the tutorial and look at the source code at the same time to get a better understanding of how things work.