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/Hero.h"
12 #include "../../math/Vector.h"
13 #include "../../graphics/Font.h"
14 #include "../../graphics/Frame.h"
18 using app::Application;
21 using graphics::Frame;
28 void VictoryState::OnEnterState(SDL_Surface *screen) {
29 OnResize(screen->w, screen->h);
33 void VictoryState::LoadResults() {
37 s << "Gets " << battle->ExpReward() << " EXP";
38 lines.push_back(s.str());
41 s << "Gets " << battle->GoldReward() << " gold";
42 lines.push_back(s.str());
46 for (std::vector<Hero>::const_iterator
47 i(battle->HeroesBegin()), end(battle->HeroesEnd());
49 if (i->Health() <= 0) continue;
50 const common::Hero &hero = i->Master();
52 s << hero.Name() << " next level " << hero.NextLevel();
53 lines.push_back(s.str());
56 if (battle->HasCapsule()) {
57 const Capsule &capsule = battle->GetCapsule();
58 if (capsule.Health() > 0) {
60 s << capsule.Name() << " next level " << capsule.Master().NextLevel();
61 lines.push_back(s.str());
67 s << parent->Game().state->money << " gold";
68 lines.push_back(s.str());
71 void VictoryState::OnExitState(SDL_Surface *screen) {
75 void VictoryState::OnResumeState(SDL_Surface *screen) {
79 void VictoryState::OnPauseState(SDL_Surface *screen) {
84 void VictoryState::OnResize(int width, int height) {
85 const Vector<int> offset = parent->ScreenOffset();
87 const Resources &res = parent->Res();
88 const Frame &frame = *res.selectFrame;
90 framePosition = offset + frame.BorderSize();
91 frameSize = Vector<int>(
92 parent->Width() - 2 * frame.BorderWidth(),
93 res.normalFont->CharHeight() * 13);
94 textPosition = framePosition + frame.BorderSize();
98 void VictoryState::HandleEvents(const Input &input) {
99 if (input.JustPressed(Input::ACTION_A)) {
101 timer = GraphicsTimers().StartInterval(150);
102 } else if (input.IsDown(Input::ACTION_A)
103 && timer.JustHit()) {
104 if (timer.Iteration() > 3) {
110 } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
112 timer = GraphicsTimers().StartInterval(150);
113 } else if (input.IsDown(Input::SHOULDER_LEFT)
114 && timer.JustHit()) {
115 if (timer.Iteration() > 3) {
123 && !input.IsDown(Input::ACTION_A)
124 && !input.IsDown(Input::SHOULDER_LEFT)) {
128 if (cursor >= int(lines.size())) {
129 Ctrl().PopState(); // pop self
134 void VictoryState::UpdateWorld(Uint32 deltaT) {
138 void VictoryState::Render(SDL_Surface *screen) {
139 parent->RenderBackground(screen);
140 parent->RenderHeroes(screen);
145 void VictoryState::RenderFrame(SDL_Surface *screen) {
146 const Frame &frame = *parent->Res().selectFrame;
147 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
150 void VictoryState::RenderLines(SDL_Surface *screen) {
151 // naive implementation
152 const Font &font = *parent->Res().normalFont;
153 const Vector<int> lineBreak = Vector<int>(
154 0, font.CharHeight() * 5 / 4);
155 const int start = cursor > 7 ? cursor - 8 : 0;
156 Vector<int> position = textPosition;
158 int end = cursor + 1;
161 if (timer.Running() && !stalling) {
163 position += lineBreak;
164 const int correction = timer.IterationElapsed();
165 if (correction > 0) {
167 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
172 if (end > int(lines.size())) end = lines.size();
174 for (int i = start; i < end; ++i) {
175 font.DrawString(lines[i].c_str(), screen, position);
176 position += lineBreak;