1 #include "VictoryState.h"
4 #include "../BattleState.h"
6 #include "../Resources.h"
7 #include "../../app/Application.h"
8 #include "../../app/Input.h"
9 #include "../../common/Capsule.h"
10 #include "../../common/GameConfig.h"
11 #include "../../common/GameState.h"
12 #include "../../common/Upgrade.h"
13 #include "../../math/Vector.h"
14 #include "../../graphics/ColorFade.h"
15 #include "../../graphics/Font.h"
16 #include "../../graphics/Frame.h"
21 using app::Application;
23 using common::GameState;
24 using common::Upgrade;
25 using graphics::ColorFade;
27 using graphics::Frame;
34 using std::stringstream;
39 void VictoryState::OnEnterState(SDL_Surface *screen) {
40 OnResize(screen->w, screen->h);
44 void VictoryState::LoadResults() {
45 const Resources &res = parent->Res();
49 s << res.victoryGetsText << ' '
50 << battle->ExpReward() << ' '
51 << res.victoryExpText << '.';
52 lines.push_back(s.str());
55 s << res.victoryGetsText << ' '
56 << battle->GoldReward() << ' '
57 << res.victoryGoldText << '.';
58 lines.push_back(s.str());
62 GameState &state = *parent->Game().state;
63 vector<Upgrade> upgrade;
64 battle->ApplyRewards(state, upgrade);
65 for (vector<Upgrade>::const_iterator
66 i(upgrade.begin()), end(upgrade.end());
68 LoadResult(*i, lines);
72 s << state.money << ' ' << res.victoryGoldText;
73 string goldStr = s.str();
77 s << right << setw(28) << setfill(' ') << goldStr;
78 lines.push_back(s.str());
81 void VictoryState::LoadResult(
83 vector<string> &lines) {
84 const Resources &res = parent->Res();
86 switch (u.GetType()) {
87 case Upgrade::LEVEL_UP:
88 s << left << setw(6) << setfill(' ') << u.Name()
89 << res.victoryLevelUpText << '.';
91 case Upgrade::MAX_HEALTH:
92 s << res.victoryMHPText << ' '
93 << res.victoryUpgradeText << ' '
96 case Upgrade::MAX_MAGIC:
97 s << res.victoryMMPText << ' '
98 << res.victoryUpgradeText << ' '
101 case Upgrade::ATTACK:
102 s << res.victoryATPText << ' '
103 << res.victoryUpgradeText << ' '
104 << u.Amount() << '.';
106 case Upgrade::DEFENSE:
107 s << res.victoryDFPText << ' '
108 << res.victoryUpgradeText << ' '
109 << u.Amount() << '.';
111 case Upgrade::STRENGTH:
112 s << res.victorySTRText << ' '
113 << res.victoryUpgradeText << ' '
114 << u.Amount() << '.';
116 case Upgrade::AGILITY:
117 s << res.victoryAGLText << ' '
118 << res.victoryUpgradeText << ' '
119 << u.Amount() << '.';
121 case Upgrade::INTELLIGENCE:
122 s << res.victoryINTText << ' '
123 << res.victoryUpgradeText << ' '
124 << u.Amount() << '.';
127 s << res.victoryGUTText << ' '
128 << res.victoryUpgradeText << ' '
129 << u.Amount() << '.';
131 case Upgrade::MAGIC_RESISTANCE:
132 s << res.victoryMGRText << ' '
133 << res.victoryUpgradeText << ' '
134 << u.Amount() << '.';
136 case Upgrade::LEVEL_NEXT:
137 s << setw(7) << setfill(' ')
138 << left << u.Name() << ' '
139 << res.victoryNextLevelText
140 << ' ' << u.Amount();
143 s << "unknown upgrade type " << u.GetType();
145 lines.push_back(s.str());
148 void VictoryState::OnExitState(SDL_Surface *screen) {
152 void VictoryState::OnResumeState(SDL_Surface *screen) {
156 void VictoryState::OnPauseState(SDL_Surface *screen) {
161 void VictoryState::OnResize(int width, int height) {
162 const Vector<int> offset = parent->ScreenOffset();
164 const Resources &res = parent->Res();
165 const Frame &frame = *res.selectFrame;
167 framePosition = offset + frame.BorderSize();
168 frameSize = Vector<int>(
169 parent->Width() - 2 * frame.BorderWidth(),
170 res.normalFont->CharHeight() * 13);
171 textPosition = framePosition + frame.BorderSize();
175 void VictoryState::HandleEvents(const Input &input) {
176 if (input.JustPressed(Input::ACTION_A)) {
178 timer = GraphicsTimers().StartInterval(150);
179 } else if (input.IsDown(Input::ACTION_A)
180 && timer.JustHit()) {
181 if (timer.Iteration() > 3) {
187 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
189 timer = GraphicsTimers().StartInterval(150);
190 } else if (input.IsDown(Input::SHOULDER_LEFT)
191 && timer.JustHit()) {
192 if (timer.Iteration() > 3) {
200 && !input.IsDown(Input::ACTION_A)
201 && !input.IsDown(Input::SHOULDER_LEFT)) {
205 if (cursor >= int(lines.size())) {
207 ColorFade *fade = new ColorFade(this, 0, 650);
208 fade->SetLeadInTime(150);
209 fade->SetDoublePop();
210 Ctrl().PushState(fade);
215 void VictoryState::UpdateWorld(Uint32 deltaT) {
219 void VictoryState::Render(SDL_Surface *screen) {
220 parent->RenderBackground(screen);
221 parent->RenderHeroes(screen);
226 void VictoryState::RenderFrame(SDL_Surface *screen) {
227 const Frame &frame = *parent->Res().selectFrame;
228 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
231 void VictoryState::RenderLines(SDL_Surface *screen) {
232 // naive implementation
233 const Font &font = *parent->Res().normalFont;
234 const Vector<int> lineBreak = Vector<int>(
235 0, font.CharHeight() * 5 / 4);
236 int start = cursor > 7 ? cursor - 8 : 0;
237 Vector<int> position = textPosition;
239 int end = cursor + 1;
242 if (timer.Running() && !stalling) {
244 position += lineBreak;
245 const int correction = timer.IterationElapsed();
246 if (correction > 0) {
247 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
252 if (end > int(lines.size())) {
255 if (start > int(lines.size()) - 9) {
256 start = lines.size() - 9;
259 for (int i = start; i < end; ++i) {
260 font.DrawString(lines[i].c_str(), screen, position);
261 position += lineBreak;