]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/VictoryState.cpp
64318e0157d87b50afbdd75334ad22647c8a983f
[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/Upgrade.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 common::GameState;
21 using common::Upgrade;
22 using graphics::Font;
23 using graphics::Frame;
24 using math::Vector;
25 using std::string;
26 using std::vector;
27
28 namespace battle {
29
30 void VictoryState::OnEnterState(SDL_Surface *screen) {
31         OnResize(screen->w, screen->h);
32         LoadResults();
33 }
34
35 void VictoryState::LoadResults() {
36         lines.clear();
37
38         std::stringstream s;
39         s << "Gets " << battle->ExpReward() << " EXP";
40         lines.push_back(s.str());
41
42         s.str("");
43         s << "Gets " << battle->GoldReward() << " gold";
44         lines.push_back(s.str());
45
46         lines.push_back("");
47
48         GameState &state = *parent->Game().state;
49         vector<Upgrade> upgrade;
50         battle->ApplyRewards(state, upgrade);
51         for (std::vector<Upgrade>::const_iterator
52                         i(upgrade.begin()), end(upgrade.end());
53                         i != end; ++i) {
54                 LoadResult(*i, lines);
55         }
56
57         lines.push_back("");
58         s.str("");
59         s << state.money << " gold";
60         lines.push_back(s.str());
61 }
62
63 void VictoryState::LoadResult(
64                 const Upgrade &u,
65                 vector<string> &lines) {
66         std::stringstream s;
67         switch (u.GetType()) {
68                 case Upgrade::LEVEL_UP:
69                         s << u.Name() << " levels up.";
70                         break;
71                 case Upgrade::MAX_HEALTH:
72                         s << "Max. HP increases by " << u.Amount();
73                         break;
74                 case Upgrade::MAX_MAGIC:
75                         s << "Max. MP increases by " << u.Amount();
76                         break;
77                 case Upgrade::ATTACK:
78                         s << "ATK increases by " << u.Amount();
79                         break;
80                 case Upgrade::DEFENSE:
81                         s << "DFP increases by " << u.Amount();
82                         break;
83                 case Upgrade::STRENGTH:
84                         s << "STR increases by " << u.Amount();
85                         break;
86                 case Upgrade::AGILITY:
87                         s << "AGL increases by " << u.Amount();
88                         break;
89                 case Upgrade::INTELLIGENCE:
90                         s << "INT increases by " << u.Amount();
91                         break;
92                 case Upgrade::GUT:
93                         s << "GUT increases by " << u.Amount();
94                         break;
95                 case Upgrade::MAGIC_RSISTANCE:
96                         s << "MGR increases by " << u.Amount();
97                         break;
98                 case Upgrade::LEVEL_NEXT:
99                         s << u.Name() << " next level " << u.Amount();
100                         break;
101                 default:
102                         s << "unknown upgrade type " << u.GetType();
103         }
104         lines.push_back(s.str());
105 }
106
107 void VictoryState::OnExitState(SDL_Surface *screen) {
108
109 }
110
111 void VictoryState::OnResumeState(SDL_Surface *screen) {
112
113 }
114
115 void VictoryState::OnPauseState(SDL_Surface *screen) {
116
117 }
118
119
120 void VictoryState::OnResize(int width, int height) {
121         const Vector<int> offset = parent->ScreenOffset();
122
123         const Resources &res = parent->Res();
124         const Frame &frame = *res.selectFrame;
125
126         framePosition = offset + frame.BorderSize();
127         frameSize = Vector<int>(
128                         parent->Width() - 2 * frame.BorderWidth(),
129                         res.normalFont->CharHeight() * 13);
130         textPosition = framePosition + frame.BorderSize();
131 }
132
133
134 void VictoryState::HandleEvents(const Input &input) {
135         if (input.JustPressed(Input::ACTION_A)) {
136                 ++cursor;
137                 timer = GraphicsTimers().StartInterval(150);
138         } else if (input.IsDown(Input::ACTION_A)
139                         && timer.JustHit()) {
140                 if (timer.Iteration() > 3) {
141                         ++cursor;
142                         stalling = false;
143                 } else {
144                         stalling = true;
145                 }
146         } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
147                 ++cursor;
148                 timer = GraphicsTimers().StartInterval(150);
149         } else if (input.IsDown(Input::SHOULDER_LEFT)
150                         && timer.JustHit()) {
151                 if (timer.Iteration() > 3) {
152                         ++cursor;
153                         stalling = false;
154                 } else {
155                         stalling = true;
156                 }
157         }
158         if (timer.JustHit()
159                         && !input.IsDown(Input::ACTION_A)
160                         && !input.IsDown(Input::SHOULDER_LEFT)) {
161                 timer.Clear();
162                 stalling = false;
163         }
164         if (cursor >= int(lines.size())) {
165                 Ctrl().PopState(); // pop self
166         }
167 }
168
169
170 void VictoryState::UpdateWorld(Uint32 deltaT) {
171
172 }
173
174 void VictoryState::Render(SDL_Surface *screen) {
175         parent->RenderBackground(screen);
176         parent->RenderHeroes(screen);
177         RenderFrame(screen);
178         RenderLines(screen);
179 }
180
181 void VictoryState::RenderFrame(SDL_Surface *screen) {
182         const Frame &frame = *parent->Res().selectFrame;
183         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
184 }
185
186 void VictoryState::RenderLines(SDL_Surface *screen) {
187         // naive implementation
188         const Font &font = *parent->Res().normalFont;
189         const Vector<int> lineBreak = Vector<int>(
190                         0, font.CharHeight() * 5 / 4);
191         const int start = cursor > 7 ? cursor - 8 : 0;
192         Vector<int> position = textPosition;
193
194         int end = cursor + 1;
195
196         if (start > 0) {
197                 if (timer.Running() && !stalling) {
198                         --end;
199                         position += lineBreak;
200                         const int correction = timer.IterationElapsed();
201                         if (correction > 0) {
202                 //              ++start;
203                                 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
204                         }
205                 }
206         }
207
208         if (end > int(lines.size())) end = lines.size();
209
210         for (int i = start; i < end; ++i) {
211                 font.DrawString(lines[i].c_str(), screen, position);
212                 position += lineBreak;
213         }
214 }
215
216 }