]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/VictoryState.cpp
831f08eef002b268f50c6c831ce235fa926195cc
[l2e.git] / src / battle / states / VictoryState.cpp
1 #include "VictoryState.h"
2
3 #include "../Battle.h"
4 #include "../BattleState.h"
5 #include "../Hero.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"
15
16 #include <sstream>
17
18 using app::Application;
19 using app::Input;
20 using graphics::Font;
21 using graphics::Frame;
22 using math::Vector;
23 using std::string;
24 using std::vector;
25
26 namespace battle {
27
28 void VictoryState::OnEnterState(SDL_Surface *screen) {
29         OnResize(screen->w, screen->h);
30         LoadResults();
31 }
32
33 void VictoryState::LoadResults() {
34         lines.clear();
35
36         std::stringstream s;
37         s << "Gets " << battle->ExpReward() << " EXP";
38         lines.push_back(s.str());
39
40         s.str("");
41         s << "Gets " << battle->GoldReward() << " gold";
42         lines.push_back(s.str());
43
44         lines.push_back("");
45
46         for (std::vector<Hero>::const_iterator
47                         i(battle->HeroesBegin()), end(battle->HeroesEnd());
48                         i != end; ++i) {
49                 if (i->Health() <= 0) continue;
50                 const common::Hero &hero = i->Master();
51                 s.str("");
52                 s << hero.Name() << " next level " << hero.NextLevel();
53                 lines.push_back(s.str());
54         }
55
56         if (battle->HasCapsule()) {
57                 const Capsule &capsule = battle->GetCapsule();
58                 if (capsule.Health() > 0) {
59                         s.str("");
60                         s << capsule.Name() << " next level " << capsule.Master().NextLevel();
61                         lines.push_back(s.str());
62                 }
63         }
64
65         lines.push_back("");
66         s.str("");
67         s << parent->Game().state->money << " gold";
68         lines.push_back(s.str());
69 }
70
71 void VictoryState::OnExitState(SDL_Surface *screen) {
72
73 }
74
75 void VictoryState::OnResumeState(SDL_Surface *screen) {
76
77 }
78
79 void VictoryState::OnPauseState(SDL_Surface *screen) {
80
81 }
82
83
84 void VictoryState::OnResize(int width, int height) {
85         const Vector<int> offset = parent->ScreenOffset();
86
87         const Resources &res = parent->Res();
88         const Frame &frame = *res.selectFrame;
89
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();
95 }
96
97
98 void VictoryState::HandleEvents(const Input &input) {
99         if (input.JustPressed(Input::ACTION_A)) {
100                 ++cursor;
101                 timer = GraphicsTimers().StartInterval(150);
102         } else if (input.IsDown(Input::ACTION_A)
103                         && timer.JustHit()) {
104                 if (timer.Iteration() > 3) {
105                         ++cursor;
106                         stalling = false;
107                 } else {
108                         stalling = true;
109                 }
110         } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
111                 ++cursor;
112                 timer = GraphicsTimers().StartInterval(150);
113         } else if (input.IsDown(Input::SHOULDER_LEFT)
114                         && timer.JustHit()) {
115                 if (timer.Iteration() > 3) {
116                         ++cursor;
117                         stalling = false;
118                 } else {
119                         stalling = true;
120                 }
121         }
122         if (timer.JustHit()
123                         && !input.IsDown(Input::ACTION_A)
124                         && !input.IsDown(Input::SHOULDER_LEFT)) {
125                 timer.Clear();
126                 stalling = false;
127         }
128         if (cursor >= int(lines.size())) {
129                 Ctrl().PopState(); // pop self
130         }
131 }
132
133
134 void VictoryState::UpdateWorld(Uint32 deltaT) {
135
136 }
137
138 void VictoryState::Render(SDL_Surface *screen) {
139         parent->RenderBackground(screen);
140         parent->RenderHeroes(screen);
141         RenderFrame(screen);
142         RenderLines(screen);
143 }
144
145 void VictoryState::RenderFrame(SDL_Surface *screen) {
146         const Frame &frame = *parent->Res().selectFrame;
147         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
148 }
149
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;
157
158         int end = cursor + 1;
159
160         if (start > 0) {
161                 if (timer.Running() && !stalling) {
162                         position += lineBreak;
163                         const int correction = timer.IterationElapsed();
164                         if (correction > 0) {
165                 //              ++start;
166                                 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
167                         } else {
168                                 --end;
169                         }
170                 }
171         }
172
173         if (end > int(lines.size())) end = lines.size();
174
175         for (int i = start; i < end; ++i) {
176                 font.DrawString(lines[i].c_str(), screen, position);
177                 position += lineBreak;
178         }
179 }
180
181 }