The Legend of the Tomato Quest Tutorial - Enemies
From SwinGame
Contents |
Enemy Images
Before we can add our enemies, we need their images. Here are the images that are used in this tutorial. Save them into your Resources\images folder in your project directory.
Adding The Enemy Images to our Game
Add the following lines of code in the GameResources.cs source file, in the LoadImages() method within the Resources class.
private static void LoadImages() { NewImage("Hero", "Hero.png"); NewImage("Overlay", "Overlay.png"); NewImage("Health", "Health.png"); NewImage("HealthVial", "HealthVial.png"); NewImage("Slash Up", "Slash_up.png"); NewImage("Slash Down", "Slash_down.png"); NewImage("Slash Left", "Slash_left.png"); NewImage("Slash Right", "Slash_right.png"); NewImage("Healer", "Healer.png"); //Add these lines of code NewImage("Critter", "Critter.png"); NewImage("Thief", "Thief.png"); NewImage("ThiefLeader", "ThiefLeader.png"); }
Now we can access these new enemy images.
Making the Enemies
The next step to getting our enemies into the game, is to create a method that will construct the enemies for us.
Create a new source file called Enemy.cs and add the following code as a template that we can build from.
using System; using System.Drawing; using System.Collections.Generic; using SwinGame; using Graphics = SwinGame.Graphics; using Bitmap = SwinGame.Bitmap; using Font = SwinGame.Font; using FontStyle = SwinGame.FontStyle; using Event = SwinGame.Event; using CollisionSide = SwinGame.CollisionSide; using Sprite = SwinGame.Sprite; using GameResources; namespace TomatoQuest { public class Enemy { } }
Now that we have a template, the first thing we need to do is to create some constants to represent the Enemy spawn's, much like the PLAYERSPAWN constant we created earlier in the tutorial for our player character.
Add the following constants to the Game class in Game.cs
const Event ENEMY1SPAWN = Event.Event4; const Event ENEMY2SPAWN = Event.Event5; const Event BOSSSPAWN = Event.Event3;
Remember earlier, we had decided that the boss was Event3, and the Enemies were Events 4 and 5. The benefit of using constants, is that where you use ENEMY1SPAWN or BOSSSPAWN, those labels get replaced with its value, which means we only need to remember the labels, not their values.
Add the following field to the Enemy class in Enemy.cs
//An Array of Characters public List<Character> Characters = new List<Character>();
Add the following method after the constants in the Enemy class within Enemy.cs:
public Enemy(String name, Level map, int Strength, int Vitality, int Agility, int Intelligence, int Luck, Event eventtype, int Experience) { //Goes through each healer and creates a new character for each one //Placing each healer at it's spawn point. //Also giving each Enemy, experience, that will given to the player when //the player defeats the enemy. for (int i = 0; i < MappyLoader.EventCount(map.Map, eventtype) - 1; i++) { Characters.Add(new Character(name, map.EventPositionX(eventtype, i), map.EventPositionY(eventtype, i), Strength, Vitality, Agility, Intelligence, Luck, true, true, false)); Characters[i].Experience = Experience; } }
This method simply goes through each Character element in the List, and creates a new Character method from the Character class, and pass in the name, Strength, Vitality, Agility, Intelligence, Luck, true that it can attack, true that it can move, but false that it can interact.
We also set the experience of the character to whatever was passed into the method. Unlike the main player character, the experience for the AI characters will be used to determine how much experience the Player will gain after defeating an AI.
Combining Multiple AI Types
Firstly, Add these 3 fields to the Game class in Game.cs
//Enemies private Enemy _Critters; private Enemy _Thieves; private Enemy _Leader;
To load our enemies Add the following lines of code to the Game class constructor in Game.cs
//Game Constructor public Game() { _Player = new Character("Hero", _Map.EventPositionX(PLAYERSPAWN, 0), _Map.EventPositionY(PLAYERSPAWN, 0), 10, 10, 10, 10, 10, true, true, true); //Load the Healers _Healers = new Healers("Healer", _Map, 1, 1, 1, 1, 1, HEALERSPAWN); //ADD THESE 3 LINES OF CODE //Load the Enemies _Critters = new Enemy("Critter", _Map, 5, 5, 4, 4, 5, ENEMY1SPAWN, 40); _Thieves = new Enemy("Thief", _Map, 10, 8, 8, 4, 8, ENEMY2SPAWN, 120); _Leader = new Enemy("ThiefLeader", _Map, 35, 45, 20, 20, 10, BOSSSPAWN, 1000); //Create the AI List _AI = new List<Character>(); //Add the Healers to the total AI _AI.AddRange(_Healers.Characters); //ADD THESE 3 LINES OF CODE //Add the AI to the total AI _AI.AddRange(_Critters.Characters); _AI.AddRange(_Thieves.Characters); _AI.AddRange(_Leader.Characters); }
This new code, is loading all the AI types into their own arrays, and then creates an array called _AI which will hold ALL AI. We then use our new AddRange function to add the content of all the AI into the _AI array.
After this, _AI holds all of our AI, so now when the program calls Update AI, it will update the Critters, Thieves and the Boss as well.
Finishing Up
Compile and build the game, there are some issues that we will address in the next tutorial such as:
- AI colliding with AI
- Optimizing the Collisions
- Making the AI move and attack, only when they are within a reasonable distance
- Making the AI move and attack, when they are alive.
- Monitor the AI's health so that when they have no health left, they die.
The Project so far
The Source code for the above tutorial can be found Here.
It is strongly recommended that you read through the code as you go through the tutorial to get a better understanding of how it works.




