From: Daniel Karbach Date: Tue, 5 Feb 2013 19:59:37 +0000 (+0100) Subject: added simple victory state X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=e1dab8a680a76f8621e967a693dbf2b481ba8f75;p=l2e.git added simple victory state refs #5 --- diff --git a/src/battle/Battle.h b/src/battle/Battle.h index e7cf8a2..cd2894f 100644 --- a/src/battle/Battle.h +++ b/src/battle/Battle.h @@ -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; diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index a17732c..1f68acf 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -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()) { diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 0f3216e..cfd344d 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -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 offset; bool ranAway; + bool alreadyPushed; }; diff --git a/src/battle/Hero.h b/src/battle/Hero.h index 98cda93..df23aa1 100644 --- a/src/battle/Hero.h +++ b/src/battle/Hero.h @@ -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 index 0000000..3516bde --- /dev/null +++ b/src/battle/states/VictoryState.cpp @@ -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 + +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::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 offset = parent->ScreenOffset(); + + const Resources &res = parent->Res(); + const Frame &frame = *res.selectFrame; + + framePosition = offset + frame.BorderSize(); + frameSize = Vector( + 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 position = textPosition; + for (vector::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 index 0000000..bb872a2 --- /dev/null +++ b/src/battle/states/VictoryState.h @@ -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 +#include + +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 timer; + std::vector lines; + math::Vector framePosition; + math::Vector frameSize; + math::Vector textPosition; + +}; + +} + +#endif