1 #include "VictoryState.h"
4 #include "../BattleState.h"
6 #include "../../app/Application.h"
7 #include "../../app/Input.h"
8 #include "../../common/Capsule.h"
9 #include "../../common/GameConfig.h"
10 #include "../../common/GameState.h"
11 #include "../../math/Vector.h"
12 #include "../../graphics/Font.h"
13 #include "../../graphics/Frame.h"
17 using app::Application;
20 using graphics::Frame;
27 void VictoryState::OnEnterState(SDL_Surface *screen) {
28 OnResize(screen->w, screen->h);
32 void VictoryState::LoadResults() {
36 s << "Gets " << battle->ExpReward() << " EXP";
37 lines.push_back(s.str());
40 s << "Gets " << battle->GoldReward() << " gold";
41 lines.push_back(s.str());
45 vector<common::Hero::UpgradeInfo> upgrade;
46 for (std::vector<Hero>::iterator
47 i(battle->HeroesBegin()), end(battle->HeroesEnd());
49 if (i->Health() <= 0) continue;
51 common::Hero &hero = i->Master();
52 hero.AddExperience(battle->ExpReward(), upgrade);
53 LoadResults(hero.Name(), upgrade, lines);
55 s << hero.Name() << " next level " << hero.NextLevel();
56 lines.push_back(s.str());
59 if (battle->HasCapsule()) {
60 const Capsule &capsule = battle->GetCapsule();
61 if (capsule.Health() > 0) {
63 s << capsule.Name() << " next level " << capsule.Master().NextLevel();
64 lines.push_back(s.str());
70 s << parent->Game().state->money << " gold";
71 lines.push_back(s.str());
74 void VictoryState::LoadResults(
76 const vector<common::Hero::UpgradeInfo> &upgrade,
77 vector<string> &lines) {
79 for (vector<common::Hero::UpgradeInfo>::const_iterator
80 i(upgrade.begin()), end(upgrade.end());
84 case common::Hero::UPGRADE_LVL:
85 s << who << " levels up.";
87 case common::Hero::UPGRADE_MHP:
88 s << "Max. HP increases by " << i->amount;
90 case common::Hero::UPGRADE_MMP:
91 s << "Max. MP increases by " << i->amount;
93 case common::Hero::UPGRADE_ATK:
94 s << "ATK increases by " << i->amount;
96 case common::Hero::UPGRADE_DFP:
97 s << "DFP increases by " << i->amount;
99 case common::Hero::UPGRADE_STR:
100 s << "STR increases by " << i->amount;
102 case common::Hero::UPGRADE_AGL:
103 s << "AGL increases by " << i->amount;
105 case common::Hero::UPGRADE_INT:
106 s << "INT increases by " << i->amount;
108 case common::Hero::UPGRADE_GUT:
109 s << "GUT increases by " << i->amount;
111 case common::Hero::UPGRADE_MGR:
112 s << "MGR increases by " << i->amount;
115 s << "There's an error in common::Hero::"
118 lines.push_back(s.str());
122 void VictoryState::OnExitState(SDL_Surface *screen) {
126 void VictoryState::OnResumeState(SDL_Surface *screen) {
130 void VictoryState::OnPauseState(SDL_Surface *screen) {
135 void VictoryState::OnResize(int width, int height) {
136 const Vector<int> offset = parent->ScreenOffset();
138 const Resources &res = parent->Res();
139 const Frame &frame = *res.selectFrame;
141 framePosition = offset + frame.BorderSize();
142 frameSize = Vector<int>(
143 parent->Width() - 2 * frame.BorderWidth(),
144 res.normalFont->CharHeight() * 13);
145 textPosition = framePosition + frame.BorderSize();
149 void VictoryState::HandleEvents(const Input &input) {
150 if (input.JustPressed(Input::ACTION_A)) {
152 timer = GraphicsTimers().StartInterval(150);
153 } else if (input.IsDown(Input::ACTION_A)
154 && timer.JustHit()) {
155 if (timer.Iteration() > 3) {
161 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
163 timer = GraphicsTimers().StartInterval(150);
164 } else if (input.IsDown(Input::SHOULDER_LEFT)
165 && timer.JustHit()) {
166 if (timer.Iteration() > 3) {
174 && !input.IsDown(Input::ACTION_A)
175 && !input.IsDown(Input::SHOULDER_LEFT)) {
179 if (cursor >= int(lines.size())) {
180 Ctrl().PopState(); // pop self
185 void VictoryState::UpdateWorld(Uint32 deltaT) {
189 void VictoryState::Render(SDL_Surface *screen) {
190 parent->RenderBackground(screen);
191 parent->RenderHeroes(screen);
196 void VictoryState::RenderFrame(SDL_Surface *screen) {
197 const Frame &frame = *parent->Res().selectFrame;
198 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
201 void VictoryState::RenderLines(SDL_Surface *screen) {
202 // naive implementation
203 const Font &font = *parent->Res().normalFont;
204 const Vector<int> lineBreak = Vector<int>(
205 0, font.CharHeight() * 5 / 4);
206 const int start = cursor > 7 ? cursor - 8 : 0;
207 Vector<int> position = textPosition;
209 int end = cursor + 1;
212 if (timer.Running() && !stalling) {
214 position += lineBreak;
215 const int correction = timer.IterationElapsed();
216 if (correction > 0) {
218 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
223 if (end > int(lines.size())) end = lines.size();
225 for (int i = start; i < end; ++i) {
226 font.DrawString(lines[i].c_str(), screen, position);
227 position += lineBreak;