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

Make progress towards porting new attack function

Currently the matchupTable needs to be loaded for both primary and secondary weapontypes
How do we do that?
parent 5feb27d1
Branches
No related tags found
1 merge request!15Merge units into main
......@@ -69,16 +69,90 @@ namespace advanced_wars
}
}
void Unit::attack(Unit& enemy) {
void Unit::attack(Unit &enemy)
{
secondary_weapon = fill_matchupTable(0);
primary_weapon = fill_matchupTable(1);
// Zuerst die Tabel für die Waffen der angreifenden Einheit holen
auto &attackerSecondaryWeaponTable = secondary_weapon[this->id];
auto &attackerPrimaryWeaponTable = primary_weapon[this->id];
// Schadenswert für die angreifende Einheit gegen die verteidigende Einheit berechnen
// Es wird die Waffe genommen die mehr Schaden macht
int attackerDamageValue = 0;
if (attackerSecondaryWeaponTable.find(enemy.id) != attackerSecondaryWeaponTable.end())
{
attackerDamageValue = attackerSecondaryWeaponTable[enemy.id];
}
if (attackerPrimaryWeaponTable.find(enemy.id) != attackerPrimaryWeaponTable.end())
{
if (attackerDamageValue < attackerPrimaryWeaponTable[enemy.id])
{
// Here ammo deduction should happen if applicable
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;
}
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;
// Prüfen, ob der Gegner noch am Leben ist um zurückzuschlagen
if (enemy.health > 0)
{
// Weapon tables for the defender
auto &defenderSecondaryWeaponTable = secondary_weapon[enemy.id];
auto &defenderPrimaryWeaponTable = primary_weapon[enemy.id];
int offDamage = 50;
int defDamage = 1000;
int defenderDamageValue = 0; // Declare outside for later use
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;
// Determine the damage value for the defender
if (defenderSecondaryWeaponTable.find(id) != defenderSecondaryWeaponTable.end())
{
defenderDamageValue = defenderSecondaryWeaponTable[id];
}
if (defenderPrimaryWeaponTable.find(id) != defenderPrimaryWeaponTable.end())
{
if (defenderDamageValue < defenderPrimaryWeaponTable[id])
{
// Deduct ammo for primary weapon, if applicable
defenderDamageValue = defenderPrimaryWeaponTable[id];
}
}
// 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);
this->health -= defDamage;
this->health = std::max(0, this->health); // Safeguard against negative health
std::cout << "Ally health after retaliation: " << this->health << std::endl;
}
}
}
}
MatchupTable Unit::fill_matchupTable(int type) {
switch (type)
{
case 0:
break;
default:
break;
}
}
......
......@@ -55,7 +55,7 @@ enum class MovementType {
LANDER = 5,
};
using MatchupTable = std::unordered_map<u_int8_t, std::unordered_map<u_int8_t, int>>;
using MatchupTable = std::unordered_map<UnitId, std::unordered_map<UnitId, int>>;
class Unit {
public:
......@@ -108,7 +108,7 @@ public:
This function fills the MatchupTable
It would be better if this table would be placed in the level
*/
void fill_matchupTable();
MatchupTable fill_matchupTable(int);
/*
This function will be called by an external event-handler, eventually.
......@@ -131,13 +131,11 @@ private:
bool has_attacked;
bool is_selected;
bool is_targeted;
MatchupTable secondary_weapon;
MatchupTable primary_weapon;
// Primary weapon ammo
int ammo;
/*
std::optional<Weapon> primary;
std::optional<Weapon> secondary;
*/
};
} // namespace advanced_wars
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment