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 "../../common/Upgrade.h"
12 #include "../../math/Vector.h"
13 #include "../../graphics/Font.h"
14 #include "../../graphics/Frame.h"
18 using app::Application;
20 using common::GameState;
21 using common::Upgrade;
23 using graphics::Frame;
30 void VictoryState::OnEnterState(SDL_Surface *screen) {
31 OnResize(screen->w, screen->h);
35 void VictoryState::LoadResults() {
39 s << "Gets " << battle->ExpReward() << " EXP";
40 lines.push_back(s.str());
43 s << "Gets " << battle->GoldReward() << " gold";
44 lines.push_back(s.str());
48 GameState &state = *parent->Game().state;
49 vector<Upgrade> upgrade;
50 battle->ApplyRewards(state, upgrade);
51 for (std::vector<Upgrade>::const_iterator
52 i(upgrade.begin()), end(upgrade.end());
54 LoadResult(*i, lines);
59 s << state.money << " gold";
60 lines.push_back(s.str());
63 void VictoryState::LoadResult(
65 vector<string> &lines) {
67 switch (u.GetType()) {
68 case Upgrade::LEVEL_UP:
69 s << u.Name() << " levels up.";
71 case Upgrade::MAX_HEALTH:
72 s << "Max. HP increases by " << u.Amount();
74 case Upgrade::MAX_MAGIC:
75 s << "Max. MP increases by " << u.Amount();
78 s << "ATK increases by " << u.Amount();
80 case Upgrade::DEFENSE:
81 s << "DFP increases by " << u.Amount();
83 case Upgrade::STRENGTH:
84 s << "STR increases by " << u.Amount();
86 case Upgrade::AGILITY:
87 s << "AGL increases by " << u.Amount();
89 case Upgrade::INTELLIGENCE:
90 s << "INT increases by " << u.Amount();
93 s << "GUT increases by " << u.Amount();
95 case Upgrade::MAGIC_RSISTANCE:
96 s << "MGR increases by " << u.Amount();
98 case Upgrade::LEVEL_NEXT:
99 s << u.Name() << " next level " << u.Amount();
102 s << "unknown upgrade type " << u.GetType();
104 lines.push_back(s.str());
107 void VictoryState::OnExitState(SDL_Surface *screen) {
111 void VictoryState::OnResumeState(SDL_Surface *screen) {
115 void VictoryState::OnPauseState(SDL_Surface *screen) {
120 void VictoryState::OnResize(int width, int height) {
121 const Vector<int> offset = parent->ScreenOffset();
123 const Resources &res = parent->Res();
124 const Frame &frame = *res.selectFrame;
126 framePosition = offset + frame.BorderSize();
127 frameSize = Vector<int>(
128 parent->Width() - 2 * frame.BorderWidth(),
129 res.normalFont->CharHeight() * 13);
130 textPosition = framePosition + frame.BorderSize();
134 void VictoryState::HandleEvents(const Input &input) {
135 if (input.JustPressed(Input::ACTION_A)) {
137 timer = GraphicsTimers().StartInterval(150);
138 } else if (input.IsDown(Input::ACTION_A)
139 && timer.JustHit()) {
140 if (timer.Iteration() > 3) {
146 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
148 timer = GraphicsTimers().StartInterval(150);
149 } else if (input.IsDown(Input::SHOULDER_LEFT)
150 && timer.JustHit()) {
151 if (timer.Iteration() > 3) {
159 && !input.IsDown(Input::ACTION_A)
160 && !input.IsDown(Input::SHOULDER_LEFT)) {
164 if (cursor >= int(lines.size())) {
165 Ctrl().PopState(); // pop self
170 void VictoryState::UpdateWorld(Uint32 deltaT) {
174 void VictoryState::Render(SDL_Surface *screen) {
175 parent->RenderBackground(screen);
176 parent->RenderHeroes(screen);
181 void VictoryState::RenderFrame(SDL_Surface *screen) {
182 const Frame &frame = *parent->Res().selectFrame;
183 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
186 void VictoryState::RenderLines(SDL_Surface *screen) {
187 // naive implementation
188 const Font &font = *parent->Res().normalFont;
189 const Vector<int> lineBreak = Vector<int>(
190 0, font.CharHeight() * 5 / 4);
191 const int start = cursor > 7 ? cursor - 8 : 0;
192 Vector<int> position = textPosition;
194 int end = cursor + 1;
197 if (timer.Running() && !stalling) {
199 position += lineBreak;
200 const int correction = timer.IterationElapsed();
201 if (correction > 0) {
203 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
208 if (end > int(lines.size())) end = lines.size();
210 for (int i = start; i < end; ++i) {
211 font.DrawString(lines[i].c_str(), screen, position);
212 position += lineBreak;