]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/VictoryState.cpp
include capsule in victory messages
[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/Hero.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 graphics::Font;
21 using graphics::Frame;
22 using math::Vector;
23 using std::string;
24 using std::vector;
25
26 namespace battle {
27
28 void VictoryState::OnEnterState(SDL_Surface *screen) {
29         OnResize(screen->w, screen->h);
30         LoadResults();
31 }
32
33 void VictoryState::LoadResults() {
34         lines.clear();
35
36         std::stringstream s;
37         s << "Gets " << battle->ExpReward() << " EXP";
38         lines.push_back(s.str());
39
40         s.str("");
41         s << "Gets " << battle->GoldReward() << " gold";
42         lines.push_back(s.str());
43
44         lines.push_back("");
45
46         for (std::vector<Hero>::const_iterator
47                         i(battle->HeroesBegin()), end(battle->HeroesEnd());
48                         i != end; ++i) {
49                 if (i->Health() <= 0) continue;
50                 const common::Hero &hero = i->Master();
51                 s.str("");
52                 s << hero.Name() << " next level " << hero.NextLevel();
53                 lines.push_back(s.str());
54         }
55
56         if (battle->HasCapsule()) {
57                 const Capsule &capsule = battle->GetCapsule();
58                 if (capsule.Health() > 0) {
59                         s.str("");
60                         s << capsule.Name() << " next level " << capsule.Master().NextLevel();
61                         lines.push_back(s.str());
62                 }
63         }
64
65         lines.push_back("");
66         s.str("");
67         s << parent->Game().state->money << " gold";
68         lines.push_back(s.str());
69 }
70
71 void VictoryState::OnExitState(SDL_Surface *screen) {
72
73 }
74
75 void VictoryState::OnResumeState(SDL_Surface *screen) {
76
77 }
78
79 void VictoryState::OnPauseState(SDL_Surface *screen) {
80
81 }
82
83
84 void VictoryState::OnResize(int width, int height) {
85         const Vector<int> offset = parent->ScreenOffset();
86
87         const Resources &res = parent->Res();
88         const Frame &frame = *res.selectFrame;
89
90         framePosition = offset + frame.BorderSize();
91         frameSize = Vector<int>(
92                         parent->Width() - 2 * frame.BorderWidth(),
93                         res.normalFont->CharHeight() * 13);
94         textPosition = framePosition + frame.BorderSize();
95 }
96
97
98 void VictoryState::HandleEvents(const Input &input) {
99         if (input.JustPressed(Input::ACTION_A)) {
100                 ++cursor;
101                 timer = GraphicsTimers().StartInterval(150);
102         } else if (input.IsDown(Input::ACTION_A)
103                         && timer.JustHit()
104                         && timer.Iteration() > 3) {
105                 ++cursor;
106         } else if (input.JustPressed(Input::SHOULDER_LEFT)) {
107                 ++cursor;
108                 timer = GraphicsTimers().StartInterval(150);
109         } else if (input.IsDown(Input::SHOULDER_LEFT)
110                         && timer.JustHit()
111                         && timer.Iteration() > 3) {
112                 ++cursor;
113         }
114         if (!input.IsDown(Input::ACTION_A)
115                         && !input.IsDown(Input::SHOULDER_LEFT)) {
116                 timer.Clear();
117         }
118         if (cursor >= int(lines.size())) {
119                 Ctrl().PopState(); // pop self
120         }
121 }
122
123
124 void VictoryState::UpdateWorld(Uint32 deltaT) {
125
126 }
127
128 void VictoryState::Render(SDL_Surface *screen) {
129         parent->RenderBackground(screen);
130         parent->RenderHeroes(screen);
131         RenderFrame(screen);
132         RenderLines(screen);
133 }
134
135 void VictoryState::RenderFrame(SDL_Surface *screen) {
136         const Frame &frame = *parent->Res().selectFrame;
137         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
138 }
139
140 void VictoryState::RenderLines(SDL_Surface *screen) {
141         // naive implementation
142         const Font &font = *parent->Res().normalFont;
143         Vector<int> position = textPosition;
144         for (int i = 0; i <= cursor && i < int(lines.size()); ++i) {
145                 font.DrawString(lines[i].c_str(), screen, position);
146                 position.Y() += font.CharHeight();
147         }
148 }
149
150 }