Enemies
Before I started implementing the UI we found out that one of our team members would be leaving university and so would not be carrying on the project with us. That left us a man down and without any enemies implemented in to the game. Since we didn’t know how big of a task this would be and is more crucial to the game than the UI, I decided to start implementing the enemies.
The game has 3 enemies: Demons, Ghosts and the Psycho Axe Man. All three will inherit from an Enemy class which will hold information such as health, damage, and virtual update, setup and render functions.
I started by setting up the different classes based on the image above. I didn’t setup the ShootComponent yet because I knew that was being worked on as part of the Player and I could implement it into the Demon enemy later. At this stage I implemented the Demons movement.
We wanted the demon to move towards the Player until a certain distance and then stop, trying to keep the same distance to the Player and shooting them. I did this by using Pythagoras’s theorem to get the distance and move the Demon based on whether the distance was greater or smaller than the distance I wanted them to keep at.
// Getting The Distance Between Two Positions
float GameObject::getDistanceBetween(float from_x,
float from_y,
float to_x,
float to_y)
{
float x_dir = to_x - from_x;
float y_dir = to_y - from_y;
return sqrt((x_dir * x_dir) + (y_dir * y_dir));
}
// Move the Demon Either Towards or Away From the Player
void Demon::update(ASGE::Renderer* renderer,
double delta_time,
Player* player,
std::vector<InteractableObjects*> scene_objects)
{
ASGE::Sprite* sprite = player->spriteComponent()->getSprite();
// Move Towards Player
float distance_to_player =
getDistanceBetween(spriteComponent()->getSprite()->xPos(),
spriteComponent()->getSprite()->yPos(),
sprite->xPos(),
sprite->yPos());
std::vector<float> direction_to_player =
getDirectionFromTo(spriteComponent()->getSprite()->xPos(),
spriteComponent()->getSprite()->yPos(),
sprite->xPos(),
sprite->yPos());
if (distance_to_player > distance_to_keep)
{
move(delta_time, direction_to_player[0], direction_to_player[1], speed);
}
else if (distance_to_player < distance_to_keep)
{
move(delta_time, -direction_to_player[0], -direction_to_player[1], speed);
}
}
I also setup a similar movement for the Psycho Axe Man, but without the retreating. At this point I didn’t know where to continue, and wanted to move on to the UI, so I left the enemies here and started looking at ways to successfully implement the UI.