]> git.localhorst.tv Git - l2e.git/commitdiff
added simple victory state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 5 Feb 2013 19:59:37 +0000 (20:59 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 5 Feb 2013 20:58:30 +0000 (21:58 +0100)
refs #5

src/battle/Battle.h
src/battle/BattleState.cpp
src/battle/BattleState.h
src/battle/Hero.h
src/battle/states/VictoryState.cpp [new file with mode: 0644]
src/battle/states/VictoryState.h [new file with mode: 0644]

index e7cf8a2a2cd6f986d3201124b9e1cb34d8c6b13d..cd2894fa753dbba0cbc0f39bc4aea765c524df0b 100644 (file)
@@ -107,6 +107,9 @@ public:
        bool Victory() const;
        bool Defeat() const;
 
+       int ExpReward() const { return expReward; }
+       int GoldReward() const { return goldReward; }
+
 private:
        const PartyLayout *heroesLayout;
        const PartyLayout *monstersLayout;
index a17732ca8c612c0c3bc810d46c73050e491e46d6..1f68acfa8bf05576202233639e008bc0d31d0c43 100644 (file)
@@ -3,6 +3,7 @@
 #include "PartyLayout.h"
 #include "states/SelectMoveAction.h"
 #include "states/PerformAttacks.h"
+#include "states/VictoryState.h"
 #include "../app/Application.h"
 #include "../app/Input.h"
 #include "../common/GameState.h"
@@ -119,7 +120,12 @@ void BattleState::OnResumeState(SDL_Surface *screen) {
                return;
        }
        if (battle.Victory()) {
-               Ctrl().PopState();
+               if (alreadyPushed) {
+                       Ctrl().PopState();
+               } else {
+                       Ctrl().PushState(new VictoryState(&battle, this));
+                       alreadyPushed = true;
+               }
                return;
        }
        if (battle.Defeat()) {
index 0f3216e4618b51fe3a0f822086dd2ca2eb013ced..cfd344dea4bce431b8f9a67adb534d1097a23520 100644 (file)
@@ -45,7 +45,8 @@ public:
        , battle(game->heroesLayout, monstersLayout)
        , attackTypeMenu(res->attackIcons)
        , moveMenu(res->moveIcons)
-       , ranAway(false) { assert(background && game); }
+       , ranAway(false), alreadyPushed(false)
+       { assert(background && game); }
 
 public:
        void AddMonster(const Monster &);
@@ -122,6 +123,7 @@ private:
        math::Vector<int> offset;
 
        bool ranAway;
+       bool alreadyPushed;
 
 };
 
index 98cda935f4b08a27d0dbe7ccce1b960771671fe1..df23aa10162e965babba702dfba38982fd195b6f 100644 (file)
@@ -28,6 +28,9 @@ public:
        ~Hero();
 
 public:
+       common::Hero &Master() { return *master; }
+       const common::Hero &Master() const { return *master; }
+
        const char *Name() const { return master->Name(); }
        Uint8 Level() const { return master->Level(); }
        const graphics::Sprite *Sprite() const { return master->BattleSprite(); }
diff --git a/src/battle/states/VictoryState.cpp b/src/battle/states/VictoryState.cpp
new file mode 100644 (file)
index 0000000..3516bde
--- /dev/null
@@ -0,0 +1,113 @@
+#include "VictoryState.h"
+
+#include "../Battle.h"
+#include "../BattleState.h"
+#include "../Hero.h"
+#include "../../app/Application.h"
+#include "../../app/Input.h"
+#include "../../common/Hero.h"
+#include "../../math/Vector.h"
+#include "../../graphics/Font.h"
+#include "../../graphics/Frame.h"
+
+#include <sstream>
+
+using app::Application;
+using app::Input;
+using graphics::Font;
+using graphics::Frame;
+using math::Vector;
+using std::string;
+using std::vector;
+
+namespace battle {
+
+void VictoryState::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
+       LoadResults();
+}
+
+void VictoryState::LoadResults() {
+       lines.clear();
+
+       std::stringstream s;
+       s << "Gets " << battle->ExpReward() << " EXP";
+       lines.push_back(s.str());
+
+       s.str("");
+       s << "Gets " << battle->GoldReward() << " gold";
+       lines.push_back(s.str());
+
+       for (std::vector<Hero>::const_iterator
+                       i(battle->HeroesBegin()), end(battle->HeroesEnd());
+                       i != end; ++i) {
+               if (i->Health() <= 0) continue;
+               const common::Hero &hero = i->Master();
+               s.str("");
+               s << hero.Name() << " next level " << hero.NextLevel();
+               lines.push_back(s.str());
+       }
+}
+
+void VictoryState::OnExitState(SDL_Surface *screen) {
+
+}
+
+void VictoryState::OnResumeState(SDL_Surface *screen) {
+       timer = GraphicsTimers().StartCountdown(1500);
+}
+
+void VictoryState::OnPauseState(SDL_Surface *screen) {
+
+}
+
+
+void VictoryState::OnResize(int width, int height) {
+       const Vector<int> offset = parent->ScreenOffset();
+
+       const Resources &res = parent->Res();
+       const Frame &frame = *res.selectFrame;
+
+       framePosition = offset + frame.BorderSize();
+       frameSize = Vector<int>(
+                       parent->Width() - 2 * frame.BorderWidth(),
+                       res.normalFont->CharHeight() * 13);
+       textPosition = framePosition + frame.BorderSize();
+}
+
+
+void VictoryState::HandleEvents(const Input &input) {
+       if (timer.Finished()) {
+               Ctrl().PopState(); // pop self
+       }
+}
+
+
+void VictoryState::UpdateWorld(Uint32 deltaT) {
+
+}
+
+void VictoryState::Render(SDL_Surface *screen) {
+       parent->RenderBackground(screen);
+       RenderFrame(screen);
+       RenderLines(screen);
+}
+
+void VictoryState::RenderFrame(SDL_Surface *screen) {
+       const Frame &frame = *parent->Res().selectFrame;
+       frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
+}
+
+void VictoryState::RenderLines(SDL_Surface *screen) {
+       // naive implementation
+       const Font &font = *parent->Res().normalFont;
+       Vector<int> position = textPosition;
+       for (vector<string>::const_iterator
+                       i(lines.begin()), end(lines.end());
+                       i != end; ++i) {
+               font.DrawString(i->c_str(), screen, position);
+               position.Y() += font.CharHeight();
+       }
+}
+
+}
diff --git a/src/battle/states/VictoryState.h b/src/battle/states/VictoryState.h
new file mode 100644 (file)
index 0000000..bb872a2
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef BATTLE_VICTORYSTATE_H_
+#define BATTLE_VICTORYSTATE_H_
+
+namespace battle {
+       class Battle;
+       class BattleState;
+}
+
+#include "../../app/State.h"
+#include "../../math/Vector.h"
+
+#include <string>
+#include <vector>
+
+namespace battle {
+
+class VictoryState
+: public app::State {
+
+public:
+       VictoryState(
+                       Battle *battle,
+                       BattleState *parent)
+       : battle(battle)
+       , parent(parent) { }
+
+public:
+       virtual void HandleEvents(const app::Input &);
+       virtual void UpdateWorld(Uint32 deltaT);
+       virtual void Render(SDL_Surface *);
+
+private:
+       virtual void OnEnterState(SDL_Surface *screen);
+       virtual void OnExitState(SDL_Surface *screen);
+       virtual void OnResumeState(SDL_Surface *screen);
+       virtual void OnPauseState(SDL_Surface *screen);
+
+       virtual void OnResize(int width, int height);
+
+private:
+       void LoadResults();
+       void RenderFrame(SDL_Surface *screen);
+       void RenderLines(SDL_Surface *screen);
+
+private:
+       Battle *battle;
+       BattleState *parent;
+       app::Timer<Uint32> timer;
+       std::vector<std::string> lines;
+       math::Vector<int> framePosition;
+       math::Vector<int> frameSize;
+       math::Vector<int> textPosition;
+
+};
+
+}
+
+#endif