]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/VictoryState.cpp
show battle results interactively
[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/GameConfig.h"
9 #include "../../common/GameState.h"
10 #include "../../common/Hero.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         for (std::vector<Hero>::const_iterator
44                         i(battle->HeroesBegin()), end(battle->HeroesEnd());
45                         i != end; ++i) {
46                 if (i->Health() <= 0) continue;
47                 const common::Hero &hero = i->Master();
48                 s.str("");
49                 s << hero.Name() << " next level " << hero.NextLevel();
50                 lines.push_back(s.str());
51         }
52
53         lines.push_back("");
54         s.str("");
55         s << parent->Game().state->money << " gold";
56         lines.push_back(s.str());
57 }
58
59 void VictoryState::OnExitState(SDL_Surface *screen) {
60
61 }
62
63 void VictoryState::OnResumeState(SDL_Surface *screen) {
64
65 }
66
67 void VictoryState::OnPauseState(SDL_Surface *screen) {
68
69 }
70
71
72 void VictoryState::OnResize(int width, int height) {
73         const Vector<int> offset = parent->ScreenOffset();
74
75         const Resources &res = parent->Res();
76         const Frame &frame = *res.selectFrame;
77
78         framePosition = offset + frame.BorderSize();
79         frameSize = Vector<int>(
80                         parent->Width() - 2 * frame.BorderWidth(),
81                         res.normalFont->CharHeight() * 13);
82         textPosition = framePosition + frame.BorderSize();
83 }
84
85
86 void VictoryState::HandleEvents(const Input &input) {
87         if (input.JustPressed(Input::ACTION_A)) {
88                 ++cursor;
89                 timer = GraphicsTimers().StartInterval(150);
90         } else if (input.IsDown(Input::ACTION_A)
91                         && timer.JustHit()
92                         && timer.Iteration() > 3) {
93                 ++cursor;
94         } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
95                 ++cursor;
96                 timer = GraphicsTimers().StartInterval(150);
97         } else if (input.IsDown(Input::SHOULDER_LEFT)
98                         && timer.JustHit()
99                         && timer.Iteration() > 3) {
100                 ++cursor;
101         }
102         if (!input.IsDown(Input::ACTION_A)
103                         && !input.IsDown(Input::SHOULDER_LEFT)) {
104                 timer.Clear();
105         }
106         if (cursor >= int(lines.size())) {
107                 Ctrl().PopState(); // pop self
108         }
109 }
110
111
112 void VictoryState::UpdateWorld(Uint32 deltaT) {
113
114 }
115
116 void VictoryState::Render(SDL_Surface *screen) {
117         parent->RenderBackground(screen);
118         parent->RenderHeroes(screen);
119         RenderFrame(screen);
120         RenderLines(screen);
121 }
122
123 void VictoryState::RenderFrame(SDL_Surface *screen) {
124         const Frame &frame = *parent->Res().selectFrame;
125         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
126 }
127
128 void VictoryState::RenderLines(SDL_Surface *screen) {
129         // naive implementation
130         const Font &font = *parent->Res().normalFont;
131         Vector<int> position = textPosition;
132         for (int i = 0; i <= cursor && i < int(lines.size()); ++i) {
133                 font.DrawString(lines[i].c_str(), screen, position);
134                 position.Y() += font.CharHeight();
135         }
136 }
137
138 }