From: Daniel Karbach Date: Sun, 19 Aug 2012 18:01:50 +0000 (+0200) Subject: apply damage indicated by attack selection X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=fbf5a98f8fd0da951e469003fe87d575a6bb30a4;p=l2e.git apply damage indicated by attack selection --- diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index dcad8b2..be3c970 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -213,6 +213,7 @@ void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) { ctrl.PopState(); // quit the battle scene return; } + // TODO: this should not push a state while quitting if (AttackSelectionDone()) { ctrl.PushState(new PerformAttacks(this)); } else { @@ -272,14 +273,14 @@ void BattleState::CalculateDamage() { } else { TargetSelection &ts(AttackChoiceAt(CurrentAttack().index).Selection()); if (ts.TargetsEnemies()) { - for (int i(0); i < NumHeroes(); ++i) { - if (ts.IsSelected(i)) { + for (int i(0); i < MaxMonsters(); ++i) { + if (ts.IsSelected(i) && MonsterAt(i).Health() > 0) { ts.SetBad(i, 15); } } } else { - for (int i(0); i < MaxMonsters(); ++i) { - if (ts.IsSelected(i) && MonsterAt(i).Health() > 0) { + for (int i(0); i < NumHeroes(); ++i) { + if (ts.IsSelected(i) && HeroAt(i).Health() > 0) { ts.SetBad(i, 15); } } @@ -287,6 +288,25 @@ void BattleState::CalculateDamage() { } } +void BattleState::ApplyDamage() { + if (attackCursor < 0) return; + AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index)); + TargetSelection &ts(ac.Selection()); + if (ts.TargetsEnemies()) { + for (int i(0); i < MaxMonsters(); ++i) { + if (ts.IsBad(i)) { + MonsterAt(i).SubtractHealth(ts.GetAmount(i)); + } + } + } else { + for (int i(0); i < NumHeroes(); ++i) { + if (ts.IsSelected(i) && MonsterAt(i).Health() > 0) { + HeroAt(i).SubtractHealth(ts.GetAmount(i)); + } + } + } +} + void BattleState::ClearAllAttacks() { attackCursor = -1; activeHero = -1; diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 5898a2d..45c43f9 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -130,6 +130,7 @@ public: void NextAttack(); bool AttacksFinished() const { return attackCursor >= int(attackOrder.size()); } void CalculateDamage(); + void ApplyDamage(); const Order &CurrentAttack() const { return attackOrder[attackCursor]; }; void ClearAllAttacks(); diff --git a/src/battle/Hero.cpp b/src/battle/Hero.cpp index 4bfdf0a..ab3b23e 100644 --- a/src/battle/Hero.cpp +++ b/src/battle/Hero.cpp @@ -45,4 +45,13 @@ Hero::~Hero() { } + +void Hero::SubtractHealth(int amount) { + if (amount > Health()) { + health = 0; + } else { + health -= amount; + } +} + } diff --git a/src/battle/Hero.h b/src/battle/Hero.h index d5284ae..7abfb68 100644 --- a/src/battle/Hero.h +++ b/src/battle/Hero.h @@ -38,6 +38,7 @@ public: Uint16 MaxHealth() const { return maxHealth; } Uint16 Health() const { return health; } int RelativeHealth(int max) const { return Health() * max / MaxHealth(); } + void SubtractHealth(int amount); Uint16 MaxMana() const { return maxMana; } Uint16 Mana() const { return mana; } diff --git a/src/battle/Monster.cpp b/src/battle/Monster.cpp index acbe6cf..0336142 100644 --- a/src/battle/Monster.cpp +++ b/src/battle/Monster.cpp @@ -40,4 +40,13 @@ Monster::~Monster() { } + +void Monster::SubtractHealth(int amount) { + if (amount > Health()) { + health = 0; + } else { + health -= amount; + } +} + } diff --git a/src/battle/Monster.h b/src/battle/Monster.h index f37786a..f79eaab 100644 --- a/src/battle/Monster.h +++ b/src/battle/Monster.h @@ -28,6 +28,7 @@ public: Uint16 MaxHealth() const { return maxHealth; } Uint16 Health() const { return health; } int RelativeHealth(int max) const { return health * max / maxHealth; } + void SubtractHealth(int amount); Uint16 MaxMana() const { return maxMana; } Uint16 Mana() const { return mana; } diff --git a/src/battle/TargetSelection.h b/src/battle/TargetSelection.h index 64500b0..70c95b5 100644 --- a/src/battle/TargetSelection.h +++ b/src/battle/TargetSelection.h @@ -54,6 +54,9 @@ public: void SetGood(int index, int amount) { selected[index].type = State::GOOD; selected[index].number = amount; } void SetBad(int index, int amount) { selected[index].type = State::BAD; selected[index].number = amount; } int GetAmount(int index) const { return selected[index].number; } + bool Missed(int index) const { return selected[index].type == State::MISS; } + bool IsGood(int index) const { return selected[index].type == State::GOOD; } + bool IsBad(int index) const { return selected[index].type == State::BAD; } private: void FindNextEnemy(); diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 6baa1da..9a81e00 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -60,6 +60,7 @@ void PerformAttacks::HandleEvents(const Input &input) { CheckAnimations(); if (HasAnimationsRunning()) return; ResetAnimation(); + battle->ApplyDamage(); battle->NextAttack(); if (battle->AttacksFinished()) { battle->ClearAllAttacks();