]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added small hero tags (used in battle animation and run state)
[l2e.git] / src / battle / BattleState.cpp
1 /*
2  * BattleState.cpp
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #include "BattleState.h"
9
10 #include "PartyLayout.h"
11 #include "states/SelectMoveAction.h"
12 #include "states/PerformAttacks.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/Ikari.h"
16 #include "../common/Inventory.h"
17 #include "../common/Item.h"
18 #include "../common/Spell.h"
19 #include "../geometry/operators.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Sprite.h"
22
23 #include <algorithm>
24 #include <stdexcept>
25
26 using app::Application;
27 using app::Input;
28 using common::Inventory;
29 using common::Item;
30 using common::Spell;
31 using geometry::Point;
32 using geometry::Vector;
33 using graphics::Menu;
34
35 using std::vector;
36
37 namespace battle {
38
39 void BattleState::AddMonster(const Monster &m) {
40         if (monsters.size() >= monstersLayout->NumPositions()) {
41                 throw std::overflow_error("too many monsters for layout");
42         }
43         monsters.push_back(m);
44 }
45
46 void BattleState::AddHero(const Hero &h) {
47         if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
48                 throw std::overflow_error("too many heroes for layout");
49         }
50         heroes[numHeroes] = h;
51         ++numHeroes;
52 }
53
54 void BattleState::SwapHeroes(int lhs, int rhs) {
55         if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
56         std::swap(heroes[lhs], heroes[rhs]);
57 }
58
59
60 void BattleState::Resize(int w, int h) {
61
62 }
63
64
65 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
66         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
67         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
68         for (int i(0); i < 4; ++i) {
69                 spellMenus[i] = res->spellMenuPrototype;
70                 LoadSpellMenu(i);
71                 ikariMenus[i] = res->ikariMenuPrototype;
72                 LoadIkariMenu(i);
73                 heroTags[i] = HeroTag(this, i);
74                 smallHeroTags[i] = SmallHeroTag(this, i);
75         }
76
77         int tagHeight(attackTypeMenu.Height());
78         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
79         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
80         heroTagPositions[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
81         heroTagPositions[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
82         heroTagPositions[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
83         heroTagPositions[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
84
85         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
86         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
87         xOffset = (BackgroundWidth() - 4 * tagWidth) / 2;
88         int yOffset(BackgroundHeight() - tagHeight);
89         smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
90         smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
91         smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
92         smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
93
94         itemMenu = res->itemMenuPrototype;
95         LoadInventory();
96 }
97
98 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
99         spellMenus[index].Clear();
100         spellMenus[index].Reserve(HeroAt(index).Spells().size());
101         for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
102                 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
103                 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
104         }
105 }
106
107 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
108         ikariMenus[index].Clear();
109         ikariMenus[index].Reserve(6);
110
111         if (HeroAt(index).HasWeapon()) {
112                 ikariMenus[index].Add(
113                                 HeroAt(index).Weapon()->Name(),
114                                 HeroAt(index).Weapon(),
115                                 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
116                                 res->weaponMenuIcon,
117                                 0,
118                                 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
119         } else {
120                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
121         }
122
123         if (HeroAt(index).HasArmor()) {
124                 ikariMenus[index].Add(
125                                 HeroAt(index).Armor()->Name(),
126                                 HeroAt(index).Armor(),
127                                 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
128                                 res->armorMenuIcon,
129                                 0,
130                                 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
131         } else {
132                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
133         }
134
135         if (HeroAt(index).HasShield()) {
136                 ikariMenus[index].Add(
137                                 HeroAt(index).Shield()->Name(),
138                                 HeroAt(index).Shield(),
139                                 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
140                                 res->shieldMenuIcon,
141                                 0,
142                                 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
143         } else {
144                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
145         }
146
147         if (HeroAt(index).HasHelmet()) {
148                 ikariMenus[index].Add(
149                                 HeroAt(index).Helmet()->Name(),
150                                 HeroAt(index).Helmet(),
151                                 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
152                                 res->helmetMenuIcon,
153                                 0,
154                                 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
155         } else {
156                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
157         }
158
159         if (HeroAt(index).HasRing()) {
160                 ikariMenus[index].Add(
161                                 HeroAt(index).Ring()->Name(),
162                                 HeroAt(index).Ring(),
163                                 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
164                                 res->ringMenuIcon,
165                                 0,
166                                 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
167         } else {
168                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
169         }
170
171         if (HeroAt(index).HasJewel()) {
172                 ikariMenus[index].Add(
173                                 HeroAt(index).Jewel()->Name(),
174                                 HeroAt(index).Jewel(),
175                                 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
176                                 res->jewelMenuIcon,
177                                 0,
178                                 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
179         } else {
180                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
181         }
182 }
183
184 void BattleState::LoadInventory() {
185         const Inventory &inv(*res->inventory);
186         itemMenu.Clear();
187         itemMenu.Reserve(inv.MaxItems());
188         for (int i(0); i < inv.MaxItems(); ++i) {
189                 const Item *item(inv.ItemAt(i));
190                 if (item) {
191                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
192                 } else {
193                         itemMenu.AddEmptyEntry();
194                 }
195         }
196         ClearAllAttacks();
197 }
198
199 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
200
201 }
202
203 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
204         // TODO: check for victory or defeat
205         if (ranAway) {
206                 ctrl.PopState(); // quit the battle scene
207                 return;
208         }
209         if (AttackSelectionDone()) {
210                 ctrl.PushState(new PerformAttacks(this));
211         } else {
212                 ctrl.PushState(new SelectMoveAction(this));
213         }
214 }
215
216 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
217
218 }
219
220
221 void BattleState::ClearAllAttacks() {
222         activeHero = -1;
223         for (int i(0); i < numHeroes; ++i) {
224                 attackChoices[i] = AttackChoice(this);
225         }
226 }
227
228
229 void BattleState::HandleEvents(const Input &input) {
230
231 }
232
233 void BattleState::UpdateWorld(float deltaT) {
234
235 }
236
237 void BattleState::Render(SDL_Surface *screen) {
238         Vector<int> offset(CalculateScreenOffset(screen));
239         RenderBackground(screen, offset);
240         RenderMonsters(screen, offset);
241 }
242
243 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
244         // black for now
245         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
246         SDL_Rect destRect;
247         destRect.x = offset.X();
248         destRect.y = offset.Y();
249         destRect.w = background->w;
250         destRect.h = background->h;
251         SDL_BlitSurface(background, 0, screen, &destRect);
252 }
253
254 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
255         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
256                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
257         }
258 }
259
260 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
261         for (int i(0); i < numHeroes; ++i) {
262                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset, 0, 1);
263         }
264 }
265
266 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
267         int tagHeight(attackTypeMenu.Height());
268         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
269
270         for (int i(0); i < numHeroes; ++i) {
271                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
272         }
273 }
274
275 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
276         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
277         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
278
279         SDL_Rect rect;
280         rect.x = offset.X();
281         rect.y = offset.Y() + BackgroundHeight() - tagHeight;
282         rect.w = BackgroundWidth();
283         rect.h = tagHeight;
284         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
285         rect.y += res->normalFont->CharHeight() / 8;
286         rect.h -= res->normalFont->CharHeight() / 4;
287         SDL_FillRect(screen, &rect, res->heroesBgColor);
288
289         for (int i(0); i < numHeroes; ++i) {
290                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
291         }
292 }
293
294 }