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() {
40 s << "Gets " << battle->ExpReward() << " EXP";
41 lines.push_back(s.str());
44 s << "Gets " << battle->GoldReward() << " gold";
45 lines.push_back(s.str());
49 GameState &state = *parent->Game().state;
50 vector<Upgrade> upgrade;
51 battle->ApplyRewards(state, upgrade);
52 for (std::vector<Upgrade>::const_iterator
53 i(upgrade.begin()), end(upgrade.end());
55 LoadResult(*i, lines);
60 s << state.money << " gold";
61 lines.push_back(s.str());
64 void VictoryState::LoadResult(
66 vector<string> &lines) {
68 switch (u.GetType()) {
69 case Upgrade::LEVEL_UP:
70 s << u.Name() << " levels up.";
72 case Upgrade::MAX_HEALTH:
73 s << "Max. HP increases by " << u.Amount();
75 case Upgrade::MAX_MAGIC:
76 s << "Max. MP increases by " << u.Amount();
79 s << "ATP increases by " << u.Amount();
81 case Upgrade::DEFENSE:
82 s << "DFP increases by " << u.Amount();
84 case Upgrade::STRENGTH:
85 s << "STR increases by " << u.Amount();
87 case Upgrade::AGILITY:
88 s << "AGL increases by " << u.Amount();
90 case Upgrade::INTELLIGENCE:
91 s << "INT increases by " << u.Amount();
94 s << "GUT increases by " << u.Amount();
96 case Upgrade::MAGIC_RESISTANCE:
97 s << "MGR increases by " << u.Amount();
99 case Upgrade::LEVEL_NEXT:
100 s << u.Name() << " next level " << u.Amount();
103 s << "unknown upgrade type " << u.GetType();
105 lines.push_back(s.str());
108 void VictoryState::OnExitState(SDL_Surface *screen) {
112 void VictoryState::OnResumeState(SDL_Surface *screen) {
116 void VictoryState::OnPauseState(SDL_Surface *screen) {
121 void VictoryState::OnResize(int width, int height) {
122 const Vector<int> offset = parent->ScreenOffset();
124 const Resources &res = parent->Res();
125 const Frame &frame = *res.selectFrame;
127 framePosition = offset + frame.BorderSize();
128 frameSize = Vector<int>(
129 parent->Width() - 2 * frame.BorderWidth(),
130 res.normalFont->CharHeight() * 13);
131 textPosition = framePosition + frame.BorderSize();
135 void VictoryState::HandleEvents(const Input &input) {
136 if (input.JustPressed(Input::ACTION_A)) {
138 timer = GraphicsTimers().StartInterval(150);
139 } else if (input.IsDown(Input::ACTION_A)
140 && timer.JustHit()) {
141 if (timer.Iteration() > 3) {
147 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
149 timer = GraphicsTimers().StartInterval(150);
150 } else if (input.IsDown(Input::SHOULDER_LEFT)
151 && timer.JustHit()) {
152 if (timer.Iteration() > 3) {
160 && !input.IsDown(Input::ACTION_A)
161 && !input.IsDown(Input::SHOULDER_LEFT)) {
165 if (cursor >= int(lines.size())) {
166 Ctrl().PopState(); // pop self
171 void VictoryState::UpdateWorld(Uint32 deltaT) {
175 void VictoryState::Render(SDL_Surface *screen) {
176 parent->RenderBackground(screen);
177 parent->RenderHeroes(screen);
182 void VictoryState::RenderFrame(SDL_Surface *screen) {
183 const Frame &frame = *parent->Res().selectFrame;
184 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
187 void VictoryState::RenderLines(SDL_Surface *screen) {
188 // naive implementation
189 const Font &font = *parent->Res().normalFont;
190 const Vector<int> lineBreak = Vector<int>(
191 0, font.CharHeight() * 5 / 4);
192 const int start = cursor > 7 ? cursor - 8 : 0;
193 Vector<int> position = textPosition;
195 int end = cursor + 1;
198 if (timer.Running() && !stalling) {
200 position += lineBreak;
201 const int correction = timer.IterationElapsed();
202 if (correction > 0) {
204 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
209 if (end > int(lines.size())) end = lines.size();
211 for (int i = start; i < end; ++i) {
212 font.DrawString(lines[i].c_str(), screen, position);
213 position += lineBreak;