]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
1f68acfa8bf05576202233639e008bc0d31d0c43
[l2e.git] / src / battle / BattleState.cpp
1 #include "BattleState.h"
2
3 #include "PartyLayout.h"
4 #include "states/SelectMoveAction.h"
5 #include "states/PerformAttacks.h"
6 #include "states/VictoryState.h"
7 #include "../app/Application.h"
8 #include "../app/Input.h"
9 #include "../common/GameState.h"
10 #include "../common/Ikari.h"
11 #include "../common/Inventory.h"
12 #include "../common/Item.h"
13 #include "../common/Spell.h"
14 #include "../graphics/Frame.h"
15 #include "../graphics/Sprite.h"
16 #include "../math/Vector.h"
17
18 #include <algorithm>
19 #include <cassert>
20 #include <cstdlib>
21 #include <stdexcept>
22
23 using app::Application;
24 using app::Input;
25 using common::Inventory;
26 using common::Item;
27 using common::Spell;
28 using common::Stats;
29 using math::Vector;
30 using graphics::Menu;
31
32 using std::rand;
33 using std::vector;
34
35 namespace battle {
36
37 void BattleState::AddMonster(const Monster &m) {
38         battle.AddMonster(m);
39 }
40
41 void BattleState::AddHero(const Hero &h) {
42         battle.AddHero(h);
43 }
44
45 void BattleState::SetCapsule(const Capsule &c) {
46         battle.SetCapsule(c);
47 }
48
49
50 void BattleState::OnResize(int w, int h) {
51         offset = Vector<int>(
52                                 (w - background->w) / 2,
53                                 (h - background->h) / 2);
54 }
55
56
57 void BattleState::OnEnterState(SDL_Surface *screen) {
58         for (int i(0); i < 4; ++i) {
59                 Hero &hero = HeroAt(i);
60                 hero.Position() = battle.HeroesLayout().CalculatePosition(i, background->w, background->h);
61                 hero.SpellMenu() = *res->spellMenuProperties;
62                 hero.UpdateSpellMenu();
63                 hero.IkariMenu() = *res->ikariMenuProperties;
64                 hero.UpdateIkariMenu(res);
65                 heroTags[i] = HeroTag(this, i);
66                 smallHeroTags[i] = SmallHeroTag(this, i);
67         }
68
69         battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h);
70
71         for (int i(0); i < battle.NumMonsters(); ++i) {
72                 MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h);
73         }
74
75         int tagHeight(attackTypeMenu.Height());
76         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
77         int xOffset((Width() - 2 * tagWidth) / 2);
78         heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
79         heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
80         heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
81         heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
82
83         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
84         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
85         xOffset = (Width() - 4 * tagWidth) / 2;
86         int yOffset(Height() - tagHeight);
87         smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
88         smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
89         smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
90         smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
91
92         OnResize(screen->w, screen->h);
93
94         itemMenu = *res->itemMenuProperties;
95         LoadInventory();
96         battle.ClearAllAttacks();
97 }
98
99 void BattleState::LoadInventory() {
100         const Inventory &inv(game->state->inventory);
101         itemMenu.Clear();
102         itemMenu.Reserve(inv.MaxItems());
103         for (int i(0); i < inv.MaxItems(); ++i) {
104                 const Item *item(inv.ItemAt(i));
105                 if (item) {
106                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
107                 } else {
108                         itemMenu.AddEmptyEntry();
109                 }
110         }
111 }
112
113 void BattleState::OnExitState(SDL_Surface *screen) {
114
115 }
116
117 void BattleState::OnResumeState(SDL_Surface *screen) {
118         if (ranAway) {
119                 Ctrl().PopState(); // quit the battle scene
120                 return;
121         }
122         if (battle.Victory()) {
123                 if (alreadyPushed) {
124                         Ctrl().PopState();
125                 } else {
126                         Ctrl().PushState(new VictoryState(&battle, this));
127                         alreadyPushed = true;
128                 }
129                 return;
130         }
131         if (battle.Defeat()) {
132                 Ctrl().PopState();
133                 return;
134         }
135         // TODO: this should not push a state while quitting
136         if (battle.AttackSelectionDone()) {
137                 Ctrl().PushState(new PerformAttacks(&battle, this));
138         } else {
139                 Ctrl().PushState(new SelectMoveAction(&battle, this));
140         }
141 }
142
143 void BattleState::OnPauseState(SDL_Surface *screen) {
144
145 }
146
147 void BattleState::HandleEvents(const Input &input) {
148
149 }
150
151 void BattleState::UpdateWorld(Uint32 deltaT) {
152
153 }
154
155 void BattleState::Render(SDL_Surface *screen) {
156         assert(screen);
157         RenderBackground(screen);
158         RenderMonsters(screen);
159 }
160
161 void BattleState::RenderBackground(SDL_Surface *screen) {
162         assert(screen);
163         // black for now
164         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
165         SDL_Rect destRect;
166         destRect.x = offset.X();
167         destRect.y = offset.Y();
168         destRect.w = background->w;
169         destRect.h = background->h;
170         SDL_BlitSurface(background, 0, screen, &destRect);
171 }
172
173 void BattleState::RenderMonsters(SDL_Surface *screen) {
174         assert(screen);
175         for (vector<Monster>::size_type i(0), end(battle.NumMonsters()); i < end; ++i) {
176                 if (battle.MonsterPositionOccupied(i)) {
177                         Monster &monster(battle.MonsterAt(i));
178                         if (monster.GetAnimation().Running()) {
179                                 monster.GetAnimation().DrawCenter(screen, monster.Position() + offset);
180                         } else {
181                                 monster.Sprite()->DrawCenter(screen, monster.Position() + offset);
182                         }
183                 }
184         }
185 }
186
187 void BattleState::RenderHeroes(SDL_Surface *screen) {
188         assert(screen);
189         for (int i(0); i < NumHeroes(); ++i) {
190                 Hero &hero(battle.HeroAt(i));
191                 if (hero.GetAnimation().Running()) {
192                         hero.GetAnimation().DrawCenter(screen, hero.Position() + offset);
193                 } else {
194                         int row(hero.Health() > 0 ? 0 : 2);
195                         hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row);
196                 }
197         }
198 }
199
200 void BattleState::RenderCapsule(SDL_Surface *screen) {
201         const Capsule &capsule(battle.GetCapsule());
202         if (!capsule.Active() || capsule.Health() <= 0) return;
203         if (capsule.GetAnimation().Running()) {
204                 capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset);
205         } else {
206                 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
207         }
208 }
209
210 void BattleState::RenderHeroTags(SDL_Surface *screen) {
211         assert(screen);
212         int tagHeight(attackTypeMenu.Height());
213         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
214
215         for (int i(0); i < battle.NumHeroes(); ++i) {
216                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i));
217         }
218 }
219
220 void BattleState::RenderSmallHeroTags(SDL_Surface *screen) {
221         assert(screen);
222         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
223         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
224
225         SDL_Rect rect;
226         rect.x = offset.X();
227         rect.y = offset.Y() + Height() - tagHeight;
228         rect.w = Width();
229         rect.h = tagHeight;
230         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
231         rect.y += res->normalFont->CharHeight() / 8;
232         rect.h -= res->normalFont->CharHeight() / 4;
233         SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
234
235         for (int i(0); i < battle.NumHeroes(); ++i) {
236                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
237         }
238 }
239
240 }