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