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