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