]> git.localhorst.tv Git - l2e.git/commitdiff
moved upgrade process to battle class
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 10 Feb 2013 11:07:08 +0000 (12:07 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 13 Feb 2013 18:44:17 +0000 (19:44 +0100)
src/battle/Battle.cpp
src/battle/Battle.h
src/battle/states/VictoryState.cpp
src/battle/states/VictoryState.h
src/common/Capsule.cpp
src/common/Capsule.h
src/common/Hero.cpp
src/common/Hero.h
src/common/Upgrade.h [new file with mode: 0644]

index 7bfa940461e12cff44f90400d94327b58fc00e00..cc481e3c993eb19033e3f3e6e5ccb3fbd8e3e16a 100644 (file)
@@ -3,12 +3,17 @@
 #include "AttackChoice.h"
 #include "PartyLayout.h"
 #include "TargetSelection.h"
+#include "../common/GameState.h"
 #include "../common/Stats.h"
+#include "../common/Upgrade.h"
 
 #include <cassert>
 #include <stdexcept>
 
+using common::GameState;
 using common::Stats;
+using common::Upgrade;
+using std::vector;
 
 
 namespace battle {
@@ -382,4 +387,19 @@ bool Battle::Defeat() const {
        return true;
 }
 
+
+void Battle::ApplyRewards(
+               GameState &state,
+               vector<Upgrade> &info) {
+       for (vector<Hero>::iterator i(HeroesBegin()), end(HeroesEnd());
+                       i != end; ++i) {
+               if (i->Health() <= 0) continue;
+               i->Master().AddExperience(expReward, info);
+       }
+       if (capsule.Health() > 0) {
+               capsule.Master().AddExperience(expReward, info);
+       }
+       state.money += goldReward;
+}
+
 }
index 314288c6213dbebd892bd90d85c49558aeddecf1..d794ebb3cefee126175a8faee2bd5398e3466214 100644 (file)
@@ -8,7 +8,9 @@ namespace battle {
        class TargetSelection;
 }
 namespace common {
+       struct GameState;
        class Stats;
+       class Upgrade;
 }
 
 #include "Capsule.h"
@@ -111,6 +113,9 @@ public:
 
        int ExpReward() const { return expReward; }
        int GoldReward() const { return goldReward; }
+       void ApplyRewards(
+                       common::GameState &,
+                       std::vector<common::Upgrade> &);
 
 private:
        const PartyLayout *heroesLayout;
index 409f0846aa380b4a8153f53a12aaa287f616cda6..64318e0157d87b50afbdd75334ad22647c8a983f 100644 (file)
@@ -8,6 +8,7 @@
 #include "../../common/Capsule.h"
 #include "../../common/GameConfig.h"
 #include "../../common/GameState.h"
+#include "../../common/Upgrade.h"
 #include "../../math/Vector.h"
 #include "../../graphics/Font.h"
 #include "../../graphics/Frame.h"
@@ -16,6 +17,8 @@
 
 using app::Application;
 using app::Input;
+using common::GameState;
+using common::Upgrade;
 using graphics::Font;
 using graphics::Frame;
 using math::Vector;
@@ -42,81 +45,63 @@ void VictoryState::LoadResults() {
 
        lines.push_back("");
 
-       vector<common::Hero::UpgradeInfo> upgrade;
-       for (std::vector<Hero>::iterator
-                       i(battle->HeroesBegin()), end(battle->HeroesEnd());
+       GameState &state = *parent->Game().state;
+       vector<Upgrade> upgrade;
+       battle->ApplyRewards(state, upgrade);
+       for (std::vector<Upgrade>::const_iterator
+                       i(upgrade.begin()), end(upgrade.end());
                        i != end; ++i) {
-               if (i->Health() <= 0) continue;
-               upgrade.clear();
-               common::Hero &hero = i->Master();
-               hero.AddExperience(battle->ExpReward(), upgrade);
-               LoadResults(hero.Name(), upgrade, lines);
-               s.str("");
-               s << hero.Name() << " next level " << hero.NextLevel();
-               lines.push_back(s.str());
-       }
-
-       if (battle->HasCapsule()) {
-               const Capsule &capsule = battle->GetCapsule();
-               if (capsule.Health() > 0) {
-                       s.str("");
-                       s << capsule.Name() << " next level " << capsule.Master().NextLevel();
-                       lines.push_back(s.str());
-               }
+               LoadResult(*i, lines);
        }
 
        lines.push_back("");
        s.str("");
-       s << parent->Game().state->money << " gold";
+       s << state.money << " gold";
        lines.push_back(s.str());
 }
 
-void VictoryState::LoadResults(
-               const char *who,
-               const vector<common::Hero::UpgradeInfo> &upgrade,
+void VictoryState::LoadResult(
+               const Upgrade &u,
                vector<string> &lines) {
        std::stringstream s;
-       for (vector<common::Hero::UpgradeInfo>::const_iterator
-                       i(upgrade.begin()), end(upgrade.end());
-                       i != end; ++i) {
-               s.str("");
-               switch (i->type) {
-                       case common::Hero::UPGRADE_LVL:
-                               s << who << " levels up.";
-                               break;
-                       case common::Hero::UPGRADE_MHP:
-                               s << "Max. HP increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_MMP:
-                               s << "Max. MP increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_ATK:
-                               s << "ATK increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_DFP:
-                               s << "DFP increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_STR:
-                               s << "STR increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_AGL:
-                               s << "AGL increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_INT:
-                               s << "INT increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_GUT:
-                               s << "GUT increases by " << i->amount;
-                               break;
-                       case common::Hero::UPGRADE_MGR:
-                               s << "MGR increases by " << i->amount;
-                               break;
-                       default:
-                               s << "There's an error in common::Hero::"
-                                               "AddExperience()";
-               }
-               lines.push_back(s.str());
+       switch (u.GetType()) {
+               case Upgrade::LEVEL_UP:
+                       s << u.Name() << " levels up.";
+                       break;
+               case Upgrade::MAX_HEALTH:
+                       s << "Max. HP increases by " << u.Amount();
+                       break;
+               case Upgrade::MAX_MAGIC:
+                       s << "Max. MP increases by " << u.Amount();
+                       break;
+               case Upgrade::ATTACK:
+                       s << "ATK increases by " << u.Amount();
+                       break;
+               case Upgrade::DEFENSE:
+                       s << "DFP increases by " << u.Amount();
+                       break;
+               case Upgrade::STRENGTH:
+                       s << "STR increases by " << u.Amount();
+                       break;
+               case Upgrade::AGILITY:
+                       s << "AGL increases by " << u.Amount();
+                       break;
+               case Upgrade::INTELLIGENCE:
+                       s << "INT increases by " << u.Amount();
+                       break;
+               case Upgrade::GUT:
+                       s << "GUT increases by " << u.Amount();
+                       break;
+               case Upgrade::MAGIC_RSISTANCE:
+                       s << "MGR increases by " << u.Amount();
+                       break;
+               case Upgrade::LEVEL_NEXT:
+                       s << u.Name() << " next level " << u.Amount();
+                       break;
+               default:
+                       s << "unknown upgrade type " << u.GetType();
        }
+       lines.push_back(s.str());
 }
 
 void VictoryState::OnExitState(SDL_Surface *screen) {
index 41a541e87c4ed78eb9d7401f9972df15ad6ad7df..6c3befeb3037f11892f3fcdb96019a63b2b50c7c 100644 (file)
@@ -5,6 +5,9 @@ namespace battle {
        class Battle;
        class BattleState;
 }
+namespace common {
+       class Upgrade;
+}
 
 #include "../../app/State.h"
 #include "../../common/Hero.h"
@@ -42,9 +45,8 @@ private:
 
 private:
        void LoadResults();
-       void LoadResults(
-                       const char *,
-                       const std::vector<common::Hero::UpgradeInfo> &,
+       void LoadResult(
+                       const common::Upgrade &,
                        std::vector<std::string> &);
        void RenderFrame(SDL_Surface *screen);
        void RenderLines(SDL_Surface *screen);
index 083931c5c0a6a2835e933b4134aa0a5c1149879d..de2ea56583fd7ff77a0d5375782d0f1f93f907e1 100644 (file)
@@ -1,8 +1,9 @@
 #include "Capsule.h"
 
-#include "../common/Item.h"
-#include "../common/Spell.h"
-#include "../common/Stats.h"
+#include "Item.h"
+#include "Spell.h"
+#include "Stats.h"
+#include "Upgrade.h"
 #include "../graphics/Animation.h"
 #include "../graphics/Sprite.h"
 #include "../loader/Interpreter.h"
@@ -82,9 +83,11 @@ int Capsule::NextLevel() const {
        }
 }
 
-void Capsule::AddExperience(int exp, vector<Hero::UpgradeInfo> &info) {
+void Capsule::AddExperience(int exp, vector<Upgrade> &info) {
        if (level > numLevels) {
                // don't award any experience if at highest level
+               info.push_back(Upgrade(
+                               name, Upgrade::LEVEL_NEXT, NextLevel()));
                return;
        }
        int remain = exp;
@@ -94,15 +97,19 @@ void Capsule::AddExperience(int exp, vector<Hero::UpgradeInfo> &info) {
                remain -= added;
                ++level;
 
-               info.push_back(Hero::UpgradeInfo(Hero::UPGRADE_LVL, level));
+               info.push_back(Upgrade(name, Upgrade::LEVEL_UP, level));
 
                // TODO: upgrade attributes and push info
 
                if (level > numLevels) {
+                       info.push_back(Upgrade(
+                                       name, Upgrade::LEVEL_NEXT, NextLevel()));
                        return;
                }
        }
        experience += remain;
+       info.push_back(Upgrade(
+                       name, Upgrade::LEVEL_NEXT, NextLevel()));
 }
 
 
index a1afc1b2c012ddd1b0647f1ff185ecffdc46e3d3..1e2a6311f07fe1f27ad871a527f24567f575fa21 100644 (file)
@@ -3,13 +3,13 @@
 
 namespace common {
        class Item;
+       class Upgrade;
 }
 namespace graphics {
        class Animation;
        class Sprite;
 }
 
-#include "Hero.h"
 #include "../common/Stats.h"
 #include "../math/Vector.h"
 
@@ -67,7 +67,7 @@ public:
        int Experience() const { return experience; }
        int NextLevel() const;
 
-       void AddExperience(int, std::vector<Hero::UpgradeInfo> &);
+       void AddExperience(int, std::vector<Upgrade> &);
 
        graphics::Sprite *BattleSprite();
        const graphics::Sprite *BattleSprite() const;
index 4e7b5b91068f08e8db5137fb8f97656afae79bd8..beeab8305844b8a9b2cccb95a98a66d73c8153ee 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "Item.h"
 #include "Spell.h"
+#include "Upgrade.h"
 #include "../graphics/Animation.h"
 #include "../graphics/Sprite.h"
 #include "../loader/Interpreter.h"
@@ -70,9 +71,11 @@ int Hero::NextLevel() const {
        }
 }
 
-void Hero::AddExperience(int exp, vector<UpgradeInfo> &info) {
+void Hero::AddExperience(int exp, vector<Upgrade> &info) {
        if (level > numLevels) {
                // don't award any experience if at highest level
+               info.push_back(Upgrade(
+                               name, Upgrade::LEVEL_NEXT, NextLevel()));
                return;
        }
        int remain = exp;
@@ -82,15 +85,19 @@ void Hero::AddExperience(int exp, vector<UpgradeInfo> &info) {
                remain -= added;
                ++level;
 
-               info.push_back(UpgradeInfo(UPGRADE_LVL, level));
+               info.push_back(Upgrade(name, Upgrade::LEVEL_UP, level));
 
                // TODO: upgrade attributes and push info
 
                if (level > numLevels) {
+                       info.push_back(Upgrade(
+                                       name, Upgrade::LEVEL_NEXT, NextLevel()));
                        return;
                }
        }
        experience += remain;
+       info.push_back(Upgrade(
+                       name, Upgrade::LEVEL_NEXT, NextLevel()));
 }
 
 
index 2016396a4458d605c58c0d7fc77919c17f407791..1b4c8d10954c3076007d38730a2a622daf900f3d 100644 (file)
@@ -4,6 +4,7 @@
 namespace common {
        class Item;
        class Spell;
+       class Upgrade;
 }
 namespace graphics {
        class Animation;
@@ -73,13 +74,7 @@ public:
        int Experience() const { return experience; }
        int NextLevel() const;
 
-       struct UpgradeInfo {
-               UpgradeType type;
-               int amount;
-               UpgradeInfo(UpgradeType t, int a = 0)
-               : type(t), amount(a) { }
-       };
-       void AddExperience(int, std::vector<UpgradeInfo> &);
+       void AddExperience(int, std::vector<Upgrade> &);
 
        bool CanEquip(const Item &) const;
        bool CanInvoke(const Spell &) const;
diff --git a/src/common/Upgrade.h b/src/common/Upgrade.h
new file mode 100644 (file)
index 0000000..0a5c079
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef COMMON_UPGRADE_H_
+#define COMMON_UPGRADE_H_
+
+namespace common {
+
+class Upgrade {
+
+public:
+       enum Type {
+               LEVEL_UP,
+               MAX_HEALTH,
+               MAX_MAGIC,
+               ATTACK,
+               DEFENSE,
+               STRENGTH,
+               AGILITY,
+               INTELLIGENCE,
+               GUT,
+               MAGIC_RSISTANCE,
+               LEVEL_NEXT,
+       };
+
+public:
+       Upgrade(const char *name, Type type, int amount)
+       : name(name), type(type), amount(amount) { }
+
+public:
+       const char *Name() const { return name; }
+       Type GetType() const { return type; }
+       int Amount() const { return amount; }
+
+private:
+       const char *name;
+       Type type;
+       int amount;
+
+};
+
+}
+
+#endif