The Legend of the Tomato Quest Tutorial - Interaction

In an RPG game we can have many kinds of Interactions, such as a Shop to buy weapons, or Item boxes to open.

This tutorial is only going to cover 1 of these Interactions, because implementing all the other standard interactions in an RPG would take quite a bit of time. However, they are for the most part the same, in that, to activate the interaction, your player must be close to, and facing the NPC.

The Interaction we'll be implementing in this tutorial, is healing. We'll be using our Healers that we've put into the game to heal our player when he walks up and faces them, and hits the Return key.

Setting Up the Heal Sound
We need a sound that will be played when the player is healed.

Download, unzip, and place the Heal.wav file below and place it into the Resources\sounds folder in your project direction

[[Media:Heal.zip|Heal.zip]]

Now add the following line of code to your LoadSounds method in the Resources class within the GameResources.cs source file.

Now we have access to a healing sound.

Creating the Healing and Damage Enumeration
We need a way to tell our characters, whether they have been healed, or damaged.

Add the following enumeration above and outside of the Characters class in Character.cs source file.

Just to explain, Heal means that the Player has been healed, Enemy, means the enemy has dealt damage to the Player, Player means that the Player has dealt damage to the Enemy, Critical means that either a Player or Enemy has dealt a critical hit to the other, and None is just for a default value.

Next we need to change the Character structure to include this new enumeration, as well as some other new types.

Add the following fields to the Character class in the Character.cs source file.

As said before DamageType is the type of damage being dealt or healed. The new Damage field is to record how much damage has been healed or damaged, and the StatusCooldown is going to be used for the user notification.

The User notification is just a number that will appear above the victim of the attack of healing, telling the player how much damage he has taken, or how much health has been healed. The StatusCooldown, is going to act as a timer, representing how long the notification will appear on the screen for.

Setting the Default Values
Now that we've added these new fields to our Character class, we need to set the default values when we create a new Character.

Add the following lines to the Character constructor method in the Characters class within the Character.cs source file.

This will set the defaults to 0, None, and 0, meaning our character will start in a normal state.

Healing the Character
Next step, is to create the method that will heal the character.

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

This method takes the amount of health to heal as its parameter. First it adds the amount of health to the characters current health, then it checks if the characters health, goes beyond his maximum, and if so, sets his health to the maximum possible.

Next the character's damage record is set to healthamount, so the notification can know how much damage was healed, and next the DamageType is set to Heal and the StatusCooldown is set to 60, which is approximately 1 second if your game is running at the standard 60 Frames Per Second.

Finally, the Heal Sound is played, and the method ends.

Showing our notifications
The next method we need to make will show the notifications above the heads of the characters, when their health changes, due to being healed or attacked.

Add the following method to the Characters class in the Character.cs source file.

First we check if the characters StatusCooldown is greater then 0, then we check what DamageType has been recorded.

For each case, Text is simply drawn to the screen with the amount of health that has been healed or damaged appearing above the head of the character being updated.

However, if you look at the code for the positioning of the text, you'll notice that we've included the StatusCooldown in the calculation. This is so the text will slowly fly into the air, starting from the characters head. Just a nice, cool effect.

The differences between each case, are the color of the text itself.


 * Heal = Green
 * Enemy Damage = Red
 * Player Damage = White
 * Critical Hit = Orange

There is also one more difference, the Critical Hit type of damage has extra text saying "Critical Hit!" above the actual amount of damage itself, just for a cool effect.

While we are only healing our player at current time, we've put all 4 types in place ready for when we implement combat in the next tutorial.

Interacting with the Healer
We now need to write the method that will check whether we are attempting to interact with a Healer, and if we do, make the Healer, heal the player.

Create a new source file called Interaction.cs and include the following code as a template to build our interaction from.

Next we need to create the Interaction Method that will let our healers, heal our Player. Add the following method to the Interaction class in Interaction.cs.

This method takes in the Player, and all the Healers in an array as its parameters.

It goes through each healer, checking if the healer is alive, on screen, and canInteract.

The next if statement checks if the player and the healer are within a reasonable distance to be considered, next to each other. It also checks if the user has hit the Return key, and also if the Healer's cooldown is 0.

Next we have a switch which goes through each of the Player's directions.

Each case checks if the Player is appropriately next to the Healer, so that the Player is facing toward the Healer, if the check is successful, the HealCharacter method is called, and the cooldown for the Healer is set to 100, meaning it will take approximately 3 seconds to recharge its heal.

At the end of the method, we check if the Healer's cooldown is greater then 0, and if so, reduces the cooldown by 1, to stop the player from spamming the Healer for health.

Final Touches
We now need to add 2 Lines of code in the Run method in the Game class in Game.cs

This will run the Interactions and Notifications

Here is a screen shot showing the notification (Note: In the actual game, the notification slowly moves up).



The Project so far
The Source code for the project so far can be found [[Media:RPGTutorial11.zip|Here]].

It is strongly recommended that you read through the source code as you go through the tutorial, to get a better understanding of how everything works.