The Legend of the Tomato Quest Tutorial - Combat

This part of the tutorial will focus entirely on the combat of the RPG Game. So our character is able to attack, and our enemies can also attack. But when their swords hit their enemies, nothing happens... yet.

Hit Sounds
Every sword needs a sound that plays when you hit an enemy, and for a cool effect, we'll have a special sound for when a combatant scores a critical hit.

Download the following sounds.zip file, and extract both Critical.wav and Hit.wav to the Resources\sounds folder in your project directory.

[[Media:Tomato Quest Sounds.zip|Sounds.zip]]

Now open the GameResources.cs sourcefile and add the following lines.

Now we have a Critical Hit sound, and a normal Hit sound.

Damaging a Character
The next step to getting combat working, is to create a method that damages an character. But before that, we need to make a change to our DamageType enumeration.

In the Character.cs source file, modify the DamageType enumeration to look like the following.

We also need to add some code to the UpdateCharacterStatus method. Add the following lines of code to the UpdateCharacterStatus in the Characters class within the Character.cs source file.

Now our characters can evade attacks if they are lucky enough.

Now let's move onto the actual method that will damage our characters.

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

This code is almost identical to the code for the HealCharacter method, except that we have to pass in what DamageType has occured, since damage could be from the Player, Enemy, or a Critical Hit. The attack could also have been evaded.

First we damage the character health, by subtracting the healthamount pass in from the character's current health. We then store the amount of damage that has occured, and store DamageType that occured as well, so we can use both for our player's notification.

Lastly, we set the StatusCooldown to 60, so the notification doesn't spam.

Setting up our Characters
We need to make a quick change to the RefreshCharacterStats method. Add the following line of code to the RefreshCharacterStats method in the Characters class in the Character.cs source file:

What this will do, is give the Player his full health and mana back when he grow's a level.

Random Number
For our Critical Hits, and Evasion, we need the use of random numbers. This is easy to implement.

Add the following field to the Game class in Game.cs

This will create an object that will give us random numbers. We pass in the System.DateTime.Now.Millisecond as the parameter, because if we do, if helps make sure that our numbers are always different.

Combat
We need to make a new source file now. Create a source file called Combat.cs and add the following code as a template.

This will serve as a template for our combat

Critical Hits and Evasion
Now we need to create a function that will determine whether the Character has scored a critical hit.

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

This method takes in a character, and a random object (the object we created in GameLogic.cs).

We use the random objects, next command to get a random number from 1 to 100. This will be our luck roll, to determine whether we have scored a critical hit.

We then determine if the roll is less then or equal to the Character's critical rate, and if so, he has score a critical hit, and the method returns true, else it returns false.

Next is the evasion method, just like the Critical hit method, Add the following method to the Combat class in the Combat.cs source file:

There really isn't that difference between the Evasion method and Critical Hit method, other then we check against the character's evasion instead of the critical rate.

With these methods we can now determine if a character, Evades an attack, or Scores and Critical Hit.

Dealing the Damage
Now we'll implement the method that will actually do the damage.

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

Also add this following method to the Combat class in Combat.cs

Now our Player and AI can attack each other.

Finishing Touches
Add the following Lines of code to the Run method in the Game class in Game.cs source file, within the loop:

This will run the checks for the combat.

One final thing needs to be done.

Add the following line of code to the UpdateAI method in the AIController class in the AIController.cs source file:

This will add the notifications to the AI.

I've also made a new CharacterInformation method that shows you your level and how many unused skill points you have, while your in the game.

Add the following method to the UserInterface class of the UserInterface.cs source file:

Also add the following line of code to the RunUI method in the UserInterface class within the UserInterface.cs source file:

And that's that, Compile and run the game, now you'll be able to fight your enemies, gain levels, and add stats.



Next tutorial will be looking at making the Legendary Tomato Item, and adding in the functionality to win and lose the game.

The Project so far
The Source code for the tutorial up to this point can be found [[Media:RPGTutorial12.zip|Here]].

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