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

Fix bug in selecting units

New problems:
- illegal instruction error
- units disappear after moving them
parent 91cd1cb4
No related branches found
No related tags found
1 merge request!15Merge units into main
......@@ -25,30 +25,63 @@ Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
const int RENDERING_SCALE = 3;
bool Level::clickCheck(int mouseX, int mouseY) {
bool Level::click_check_left(int mouseX, int mouseY) {
int tileX = mouseX/(16*RENDERING_SCALE);
int tileY = mouseY/(16*RENDERING_SCALE);
if(selectUnit(tileX, tileY)) return true;
if(selectBuilding(tileX, tileY)) return true;
if(selectUnit(tileX, tileY)) {
return true;
}
if(selectBuilding(tileX, tileY)) {
return true;
}
return false;
}
bool Level::click_check_right(int mouseX, int mouseY) {
std::cout << "Neither building nor unit clicked" << std::endl;
int tileX = mouseX/(16*RENDERING_SCALE);
int tileY = mouseY/(16*RENDERING_SCALE);
if(target_unit(tileX, tileY)) {
return true;
}
return false;
}
bool Level::selectUnit (int tileX, int tileY) {
//std::cout << "tileX:" << tileX << "tileX:" << tileY << std::endl;
for (auto& unit : units) {
if(unit.x == tileX && unit.y == tileY) {
//std::cout << "X:" << unit.x << "Y:" << unit.y << std::endl;
//std::cout << "unitX:" << unit.x << "unitY:" << unit.y << std::endl;
selectedUnit = &unit;
return true;
}
}
selectedUnit = nullptr;
return false;
}
bool Level::target_unit (int tileX, int tileY) {
//std::cout << "tileX:" << tileX << "tileX:" << tileY << std::endl;
for (auto& unit : units) {
if(unit.x == tileX && unit.y == tileY) {
//std::cout << "unitX:" << unit.x << "unitY:" << unit.y << std::endl;
targetedUnit = &unit;
return true;
}
}
return false;
}
......@@ -62,7 +95,6 @@ bool Level::selectBuilding (int tileX, int tileY) {
return true;
}
}
selectedBuilding = nullptr;
return false;
}
......@@ -75,8 +107,10 @@ 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(click_check_left(event.button.x, event.button.y)) {
if(selectedUnit) {
selectedUnit->onClick(event, units);
......@@ -85,32 +119,44 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
if(selectedBuilding) {
//building stuff
}
} else {
std::cout << "Neither building nor unit clicked!" << std::endl;
selectedUnit = nullptr;
selectedBuilding = nullptr;
}
} 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;
int tileX = event.button.x;
int tileY = event.button.y;
selectedUnit->attack(unit);
if(click_check_right(tileX, tileY)) {
selectedUnit->attack(targetedUnit);
units.erase(
std::remove_if(units.begin(), units.end(),
[](const Unit& unit) { return unit.health < 0; }),
units.end());
} else {
selectedUnit->update_position(tileX, tileY);
}
}
}
}
} else {
break;
std::cout << "No unit selected! " << std::endl;
default:
break;
}
}
}
}
......
......@@ -34,11 +34,14 @@ private:
std::vector<Unit> units;
std::vector<Effect> effects;
Unit* selectedUnit;
Unit* targetedUnit;
Building* selectedBuilding;
bool selectUnit (int tileX, int tileY);
bool target_unit (int tileX, int tileY);
bool selectBuilding(int tileX, int tileY);
bool clickCheck(int mouseX, int mouseY);
bool click_check_left(int mouseX, int mouseY);
bool click_check_right(int mouseX, int mouseY);
};
} // namespace advanced_wars
......@@ -69,7 +69,7 @@ namespace advanced_wars
}
}
void Unit::attack(Unit &enemy)
void Unit::attack(Unit *enemy)
{
secondary_weapon = fill_matchupTable(0);
primary_weapon = fill_matchupTable(1);
......@@ -83,38 +83,38 @@ namespace advanced_wars
int attackerDamageValue = 0;
if (attackerSecondaryWeaponTable.find(enemy.id) != attackerSecondaryWeaponTable.end())
if (attackerSecondaryWeaponTable.find(enemy->id) != attackerSecondaryWeaponTable.end())
{
attackerDamageValue = attackerSecondaryWeaponTable[enemy.id];
attackerDamageValue = attackerSecondaryWeaponTable[enemy->id];
}
if (attackerPrimaryWeaponTable.find(enemy.id) != attackerPrimaryWeaponTable.end())
if (attackerPrimaryWeaponTable.find(enemy->id) != attackerPrimaryWeaponTable.end())
{
if (attackerDamageValue < attackerPrimaryWeaponTable[enemy.id])
if (attackerDamageValue < attackerPrimaryWeaponTable[enemy->id])
{
// Here ammo deduction should happen if applicable
attackerDamageValue = attackerPrimaryWeaponTable[enemy.id];
attackerDamageValue = attackerPrimaryWeaponTable[enemy->id];
}
}
if (attackerDamageValue == 0)
{
std::cout << "No damage value found for attack from unit " << static_cast<int>(id)
<< " against unit " << static_cast<int>(enemy.id) << std::endl;
<< " against unit " << static_cast<int>(enemy->id) << std::endl;
}
else
{
int offDamage = attackerDamageValue * (static_cast<float>(health) / max_health);
enemy.health -= offDamage;
enemy.health = std::max(0, enemy.health); // Ensuring health is not negative
std::cout << "Enemy health after attack: " << enemy.health << std::endl;
enemy->health -= offDamage;
enemy->health = std::max(0, enemy->health); // Ensuring health is not negative
std::cout << "Enemy health after attack: " << enemy->health << std::endl;
// Prüfen, ob der Gegner noch am Leben ist um zurückzuschlagen
if (enemy.health > 0)
if (enemy->health > 0)
{
// Weapon tables for the defender
auto &defenderSecondaryWeaponTable = secondary_weapon[enemy.id];
auto &defenderPrimaryWeaponTable = primary_weapon[enemy.id];
auto &defenderSecondaryWeaponTable = secondary_weapon[enemy->id];
auto &defenderPrimaryWeaponTable = primary_weapon[enemy->id];
int defenderDamageValue = 0; // Declare outside for later use
......@@ -135,7 +135,7 @@ namespace advanced_wars
// If a valid damage value was determined for retaliation
if (defenderDamageValue > 0)
{
int defDamage = static_cast<int>(defenderDamageValue * static_cast<float>(enemy.health) / enemy.max_health);
int defDamage = static_cast<int>(defenderDamageValue * static_cast<float>(enemy->health) / enemy->max_health);
this->health -= defDamage;
this->health = std::max(0, this->health); // Safeguard against negative health
std::cout << "Ally health after retaliation: " << this->health << std::endl;
......@@ -188,6 +188,7 @@ Features:
// we have to re-initialize the unit.state (probably to idle)
this->is_selected = true;
std::cout << "I am selected!!" << std::endl;
std::cout << "And my position is:" << this->x << " " << this->y << std::endl;
//make move range calc
......@@ -196,6 +197,7 @@ Features:
this->is_targeted = true;
std::cout << "I am targeted!!" << std::endl;
std::cout << "And my position is:" << this->x << " " << this->y << std::endl;
for (Unit unit : unitVector)
{
......
......@@ -87,7 +87,7 @@ public:
Attacker deals damage to the defender first
*/
void attack(Unit& enemy);
void attack(Unit *enemy);
/*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment