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