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