Skip to content
Snippets Groups Projects
Commit 1c234b83 authored by Max Cherris's avatar Max Cherris
Browse files

Implement removal of units on stage if defeated

parent abb13726
Branches
No related tags found
1 merge request!15Merge units into main
......@@ -8,6 +8,7 @@
#include <SDL.h>
#include <iostream>
#include <string>
#include <algorithm>
namespace advanced_wars {
......@@ -74,6 +75,7 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
if(clickCheck(event.button.x, event.button.y)) {
if(selectedUnit) {
......@@ -83,8 +85,28 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
if(selectedBuilding) {
//building stuff
}
}
} else if (event.button.button == SDL_BUTTON_RIGHT) {
if(selectedUnit) {
int tileX = event.button.x/(16*RENDERING_SCALE);
int tileY = event.button.y/(16*RENDERING_SCALE);
for (auto& unit : units) {
if(unit.x == tileX && unit.y == tileY) {
//std::cout << "X:" << unit.x << "Y:" << unit.y << std::endl;
selectedUnit->attack(unit);
units.erase(
std::remove_if(units.begin(), units.end(),
[](const Unit& unit) { return unit.health < 0; }),
units.end());
}
}
}
}
break;
default:
......
......@@ -73,35 +73,16 @@ namespace advanced_wars
MatchupTabel damageMatrix;
std::vector<Unit*> units;
void Unit::attack(Unit *ally, Unit *enemy)
{
if (ally->has_attacked)
{
// display the unit as not able to attack (maybe greyscale?)
}
void Unit::attack(Unit& enemy) {
// Start Attack: choose the appropriate weapon:
/*
int offDamage = damageMatrix[static_cast<u_int8_t>(ally->id)][static_cast<u_int8_t>(enemy->id)] * ((ally->health) / (ally->max_health));
int defDamage = damageMatrix[static_cast<u_int8_t>(enemy->id)][static_cast<u_int8_t>(ally->id)] * ((enemy->health) / (enemy->max_health));
*/
int offDamage = 10;
int offDamage = 50;
int defDamage = 1000;
enemy->health = enemy->health - offDamage;
if (enemy->health > 0)
{
ally->health = ally->health - defDamage;
std::cout << "Health ally:" << ally->health << std::endl;
if (ally->health <= 0)
{
ally->~Unit();
}
}
else
{
enemy->~Unit();
enemy.health = enemy.health - offDamage;
std::cout << "Enemy health:" << enemy.health << std::endl;
if (enemy.health > 0) {
this->health = this->health - defDamage;
std::cout << "Health ally:" << this->health << std::endl;
}
}
......@@ -175,7 +156,7 @@ Features:
if (attacker != nullptr && defender != nullptr)
{
attack(attacker, defender);
//attack(attacker, defender);
std::cout << "We are fighting!!" << std::endl;
break;
}
......
......@@ -62,6 +62,7 @@ class Unit {
public:
int x;
int y;
int health; //health equals max_health at construction
Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state);
......@@ -86,7 +87,8 @@ public:
Will Update the health for both units
Attacker deals damage to the defender first
*/
void attack(Unit *ally ,Unit *enemy);
void attack(Unit& enemy);
/*
......@@ -109,6 +111,7 @@ public:
void loadXML(const char* filename);
/*
This function will be called by an external event-handler, eventually.
Currently, it should be called if a Unit is interacted with and the resulting SDL_EVENT is passed through, and then decided upon
......@@ -120,7 +123,7 @@ private:
UnitId id;
UnitState state;
int health; //health equals max_health at construction
int max_health; // max_health required for damage_scaling
int range;
int fuel;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment