]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/VictoryState.cpp
relocate scripts
[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 "../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/ColorFade.h"
15 #include "../../graphics/Font.h"
16 #include "../../graphics/Frame.h"
17
18 #include <iomanip>
19 #include <sstream>
20
21 using app::Application;
22 using app::Input;
23 using common::GameState;
24 using common::Upgrade;
25 using graphics::ColorFade;
26 using graphics::Font;
27 using graphics::Frame;
28 using math::Vector;
29 using std::left;
30 using std::right;
31 using std::setfill;
32 using std::setw;
33 using std::string;
34 using std::stringstream;
35 using std::vector;
36
37 namespace battle {
38
39 void VictoryState::OnEnterState(SDL_Surface *screen) {
40         OnResize(screen->w, screen->h);
41         LoadResults();
42 }
43
44 void VictoryState::LoadResults() {
45         const Resources &res = parent->Res();
46         lines.clear();
47
48         stringstream s;
49         s << res.victoryGetsText << ' '
50                         << battle->ExpReward() << ' '
51                         << res.victoryExpText << '.';
52         lines.push_back(s.str());
53
54         s.str("");
55         s << res.victoryGetsText << ' '
56                         << battle->GoldReward() << ' '
57                         << res.victoryGoldText << '.';
58         lines.push_back(s.str());
59
60         lines.push_back("");
61
62         GameState &state = *parent->Game().state;
63         vector<Upgrade> upgrade;
64         battle->ApplyRewards(state, upgrade);
65         for (vector<Upgrade>::const_iterator
66                         i(upgrade.begin()), end(upgrade.end());
67                         i != end; ++i) {
68                 LoadResult(*i, lines);
69         }
70
71         s.str("");
72         s << state.money << ' ' << res.victoryGoldText;
73         string goldStr = s.str();
74
75         lines.push_back("");
76         s.str("");
77         s << right << setw(28) << setfill(' ') << goldStr;
78         lines.push_back(s.str());
79 }
80
81 void VictoryState::LoadResult(
82                 const Upgrade &u,
83                 vector<string> &lines) {
84         const Resources &res = parent->Res();
85         stringstream s;
86         switch (u.GetType()) {
87                 case Upgrade::LEVEL_UP:
88                         s << left << setw(6) << setfill(' ') << u.Name()
89                                         << res.victoryLevelUpText << '.';
90                         break;
91                 case Upgrade::MAX_HEALTH:
92                         s << res.victoryMHPText << ' '
93                                         << res.victoryUpgradeText << ' '
94                                         << u.Amount() << '.';
95                         break;
96                 case Upgrade::MAX_MAGIC:
97                         s << res.victoryMMPText << ' '
98                                         << res.victoryUpgradeText << ' '
99                                         << u.Amount() << '.';
100                         break;
101                 case Upgrade::ATTACK:
102                         s << res.victoryATPText << ' '
103                                         << res.victoryUpgradeText << ' '
104                                         << u.Amount() << '.';
105                         break;
106                 case Upgrade::DEFENSE:
107                         s << res.victoryDFPText << ' '
108                                         << res.victoryUpgradeText << ' '
109                                         << u.Amount() << '.';
110                         break;
111                 case Upgrade::STRENGTH:
112                         s << res.victorySTRText << ' '
113                                         << res.victoryUpgradeText << ' '
114                                         << u.Amount() << '.';
115                         break;
116                 case Upgrade::AGILITY:
117                         s << res.victoryAGLText << ' '
118                                         << res.victoryUpgradeText << ' '
119                                         << u.Amount() << '.';
120                         break;
121                 case Upgrade::INTELLIGENCE:
122                         s << res.victoryINTText << ' '
123                                         << res.victoryUpgradeText << ' '
124                                         << u.Amount() << '.';
125                         break;
126                 case Upgrade::GUT:
127                         s << res.victoryGUTText << ' '
128                                         << res.victoryUpgradeText << ' '
129                                         << u.Amount() << '.';
130                         break;
131                 case Upgrade::MAGIC_RESISTANCE:
132                         s << res.victoryMGRText << ' '
133                                         << res.victoryUpgradeText << ' '
134                                         << u.Amount() << '.';
135                         break;
136                 case Upgrade::LEVEL_NEXT:
137                         s << setw(7) << setfill(' ')
138                                         << left << u.Name() << ' '
139                                         << res.victoryNextLevelText
140                                         << ' ' << u.Amount();
141                         break;
142                 default:
143                         s << "unknown upgrade type " << u.GetType();
144         }
145         lines.push_back(s.str());
146 }
147
148 void VictoryState::OnExitState(SDL_Surface *screen) {
149
150 }
151
152 void VictoryState::OnResumeState(SDL_Surface *screen) {
153
154 }
155
156 void VictoryState::OnPauseState(SDL_Surface *screen) {
157
158 }
159
160
161 void VictoryState::OnResize(int width, int height) {
162         const Vector<int> offset = parent->ScreenOffset();
163
164         const Resources &res = parent->Res();
165         const Frame &frame = *res.selectFrame;
166
167         framePosition = offset + frame.BorderSize();
168         frameSize = Vector<int>(
169                         parent->Width() - 2 * frame.BorderWidth(),
170                         res.normalFont->CharHeight() * 13);
171         textPosition = framePosition + frame.BorderSize();
172 }
173
174
175 void VictoryState::HandleEvents(const Input &input) {
176         if (input.JustPressed(Input::ACTION_A)) {
177                 ++cursor;
178                 timer = GraphicsTimers().StartInterval(150);
179         } else if (input.IsDown(Input::ACTION_A)
180                         && timer.JustHit()) {
181                 if (timer.Iteration() > 3) {
182                         ++cursor;
183                         stalling = false;
184                 } else {
185                         stalling = true;
186                 }
187         } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
188                 ++cursor;
189                 timer = GraphicsTimers().StartInterval(150);
190         } else if (input.IsDown(Input::SHOULDER_LEFT)
191                         && timer.JustHit()) {
192                 if (timer.Iteration() > 3) {
193                         ++cursor;
194                         stalling = false;
195                 } else {
196                         stalling = true;
197                 }
198         }
199         if (timer.JustHit()
200                         && !input.IsDown(Input::ACTION_A)
201                         && !input.IsDown(Input::SHOULDER_LEFT)) {
202                 timer.Clear();
203                 stalling = false;
204         }
205         if (cursor >= int(lines.size())) {
206                 timer.Clear();
207                 ColorFade *fade = new ColorFade(this, 0, 650);
208                 fade->SetLeadInTime(150);
209                 fade->SetDoublePop();
210                 Ctrl().PushState(fade);
211         }
212 }
213
214
215 void VictoryState::UpdateWorld(Uint32 deltaT) {
216
217 }
218
219 void VictoryState::Render(SDL_Surface *screen) {
220         parent->RenderBackground(screen);
221         parent->RenderHeroes(screen);
222         RenderFrame(screen);
223         RenderLines(screen);
224 }
225
226 void VictoryState::RenderFrame(SDL_Surface *screen) {
227         const Frame &frame = *parent->Res().selectFrame;
228         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
229 }
230
231 void VictoryState::RenderLines(SDL_Surface *screen) {
232         // naive implementation
233         const Font &font = *parent->Res().normalFont;
234         const Vector<int> lineBreak = Vector<int>(
235                         0, font.CharHeight() * 5 / 4);
236         int start = cursor > 7 ? cursor - 8 : 0;
237         Vector<int> position = textPosition;
238
239         int end = cursor + 1;
240
241         if (start > 0) {
242                 if (timer.Running() && !stalling) {
243                         --end;
244                         position += lineBreak;
245                         const int correction = timer.IterationElapsed();
246                         if (correction > 0) {
247                                 position.Y() -= lineBreak.Y() * correction / timer.TargetTime();
248                         }
249                 }
250         }
251
252         if (end > int(lines.size())) {
253                 end = lines.size();
254         }
255         if (start > int(lines.size()) - 9) {
256                 start = lines.size() - 9;
257         }
258
259         for (int i = start; i < end; ++i) {
260                 font.DrawString(lines[i].c_str(), screen, position);
261                 position += lineBreak;
262         }
263 }
264
265 }