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/Font.h"
15 #include "../../graphics/Frame.h"
20 using app::Application;
22 using common::GameState;
23 using common::Upgrade;
25 using graphics::Frame;
32 using std::stringstream;
37 void VictoryState::OnEnterState(SDL_Surface *screen) {
38 OnResize(screen->w, screen->h);
42 void VictoryState::LoadResults() {
43 const Resources &res = parent->Res();
47 s << res.victoryGetsText << ' '
48 << battle->ExpReward() << ' '
49 << res.victoryExpText << '.';
50 lines.push_back(s.str());
53 s << res.victoryGetsText << ' '
54 << battle->GoldReward() << ' '
55 << res.victoryGoldText << '.';
56 lines.push_back(s.str());
60 GameState &state = *parent->Game().state;
61 vector<Upgrade> upgrade;
62 battle->ApplyRewards(state, upgrade);
63 for (vector<Upgrade>::const_iterator
64 i(upgrade.begin()), end(upgrade.end());
66 LoadResult(*i, lines);
70 s << state.money << ' ' << res.victoryGoldText;
71 string goldStr = s.str();
75 s << right << setw(28) << setfill(' ') << goldStr;
76 lines.push_back(s.str());
79 void VictoryState::LoadResult(
81 vector<string> &lines) {
82 const Resources &res = parent->Res();
84 switch (u.GetType()) {
85 case Upgrade::LEVEL_UP:
86 s << left << setw(6) << setfill(' ') << u.Name()
87 << res.victoryLevelUpText << '.';
89 case Upgrade::MAX_HEALTH:
90 s << res.victoryMHPText << ' '
91 << res.victoryUpgradeText << ' '
94 case Upgrade::MAX_MAGIC:
95 s << res.victoryMMPText << ' '
96 << res.victoryUpgradeText << ' '
100 s << res.victoryATPText << ' '
101 << res.victoryUpgradeText << ' '
102 << u.Amount() << '.';
104 case Upgrade::DEFENSE:
105 s << res.victoryDFPText << ' '
106 << res.victoryUpgradeText << ' '
107 << u.Amount() << '.';
109 case Upgrade::STRENGTH:
110 s << res.victorySTRText << ' '
111 << res.victoryUpgradeText << ' '
112 << u.Amount() << '.';
114 case Upgrade::AGILITY:
115 s << res.victoryAGLText << ' '
116 << res.victoryUpgradeText << ' '
117 << u.Amount() << '.';
119 case Upgrade::INTELLIGENCE:
120 s << res.victoryINTText << ' '
121 << res.victoryUpgradeText << ' '
122 << u.Amount() << '.';
125 s << res.victoryGUTText << ' '
126 << res.victoryUpgradeText << ' '
127 << u.Amount() << '.';
129 case Upgrade::MAGIC_RESISTANCE:
130 s << res.victoryMGRText << ' '
131 << res.victoryUpgradeText << ' '
132 << u.Amount() << '.';
134 case Upgrade::LEVEL_NEXT:
135 s << setw(7) << setfill(' ')
136 << left << u.Name() << ' '
137 << res.victoryNextLevelText
138 << ' ' << u.Amount();
141 s << "unknown upgrade type " << u.GetType();
143 lines.push_back(s.str());
146 void VictoryState::OnExitState(SDL_Surface *screen) {
150 void VictoryState::OnResumeState(SDL_Surface *screen) {
154 void VictoryState::OnPauseState(SDL_Surface *screen) {
159 void VictoryState::OnResize(int width, int height) {
160 const Vector<int> offset = parent->ScreenOffset();
162 const Resources &res = parent->Res();
163 const Frame &frame = *res.selectFrame;
165 framePosition = offset + frame.BorderSize();
166 frameSize = Vector<int>(
167 parent->Width() - 2 * frame.BorderWidth(),
168 res.normalFont->CharHeight() * 13);
169 textPosition = framePosition + frame.BorderSize();
173 void VictoryState::HandleEvents(const Input &input) {
174 if (input.JustPressed(Input::ACTION_A)) {
176 timer = GraphicsTimers().StartInterval(150);
177 } else if (input.IsDown(Input::ACTION_A)
178 && timer.JustHit()) {
179 if (timer.Iteration() > 3) {
185 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
187 timer = GraphicsTimers().StartInterval(150);
188 } else if (input.IsDown(Input::SHOULDER_LEFT)
189 && timer.JustHit()) {
190 if (timer.Iteration() > 3) {
198 && !input.IsDown(Input::ACTION_A)
199 && !input.IsDown(Input::SHOULDER_LEFT)) {
203 if (cursor >= int(lines.size())) {
204 Ctrl().PopState(); // pop self
209 void VictoryState::UpdateWorld(Uint32 deltaT) {
213 void VictoryState::Render(SDL_Surface *screen) {
214 parent->RenderBackground(screen);
215 parent->RenderHeroes(screen);
220 void VictoryState::RenderFrame(SDL_Surface *screen) {
221 const Frame &frame = *parent->Res().selectFrame;
222 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
225 void VictoryState::RenderLines(SDL_Surface *screen) {
226 // naive implementation
227 const Font &font = *parent->Res().normalFont;
228 const Vector<int> lineBreak = Vector<int>(
229 0, font.CharHeight() * 5 / 4);
230 const int start = cursor > 7 ? cursor - 8 : 0;
231 Vector<int> position = textPosition;
233 int end = cursor + 1;
236 if (timer.Running() && !stalling) {
238 position += lineBreak;
239 const int correction = timer.IterationElapsed();
240 if (correction > 0) {
242 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
247 if (end > int(lines.size())) end = lines.size();
249 for (int i = start; i < end; ++i) {
250 font.DrawString(lines[i].c_str(), screen, position);
251 position += lineBreak;