4 * Created on: Aug 5, 2012
8 #include "BattleState.h"
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"
25 using app::Application;
27 using common::Inventory;
30 using geometry::Point;
31 using geometry::Vector;
38 void BattleState::AddMonster(const Monster &m) {
39 if (monsters.size() >= monstersLayout->NumPositions()) {
40 throw std::overflow_error("too many monsters for layout");
42 monsters.push_back(m);
45 void BattleState::AddHero(const Hero &h) {
46 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
47 throw std::overflow_error("too many heroes for layout");
49 heroes[numHeroes] = h;
53 void BattleState::SwapHeroes(int lhs, int rhs) {
54 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
55 std::swap(heroes[lhs], heroes[rhs]);
59 void BattleState::Resize(int w, int h) {
64 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
65 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
66 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
67 for (int i(0); i < 4; ++i) {
68 spellMenus[i] = res->spellMenuPrototype;
70 ikariMenus[i] = res->ikariMenuPrototype;
72 heroTags[i] = HeroTag(this, i);
75 int tagHeight(attackTypeMenu.Height());
76 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
77 int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
78 heroTagPositions[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
79 heroTagPositions[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
80 heroTagPositions[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
81 heroTagPositions[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
83 itemMenu = res->itemMenuPrototype;
87 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
88 spellMenus[index].Clear();
89 spellMenus[index].Reserve(HeroAt(index).Spells().size());
90 for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
91 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
92 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
96 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
97 ikariMenus[index].Clear();
98 ikariMenus[index].Reserve(6);
100 if (HeroAt(index).HasWeapon()) {
101 ikariMenus[index].Add(
102 HeroAt(index).Weapon()->Name(),
103 HeroAt(index).Weapon(),
104 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
107 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
109 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
112 if (HeroAt(index).HasArmor()) {
113 ikariMenus[index].Add(
114 HeroAt(index).Armor()->Name(),
115 HeroAt(index).Armor(),
116 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
119 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
121 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
124 if (HeroAt(index).HasShield()) {
125 ikariMenus[index].Add(
126 HeroAt(index).Shield()->Name(),
127 HeroAt(index).Shield(),
128 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
131 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
133 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
136 if (HeroAt(index).HasHelmet()) {
137 ikariMenus[index].Add(
138 HeroAt(index).Helmet()->Name(),
139 HeroAt(index).Helmet(),
140 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
143 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
145 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
148 if (HeroAt(index).HasRing()) {
149 ikariMenus[index].Add(
150 HeroAt(index).Ring()->Name(),
151 HeroAt(index).Ring(),
152 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
155 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
157 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
160 if (HeroAt(index).HasJewel()) {
161 ikariMenus[index].Add(
162 HeroAt(index).Jewel()->Name(),
163 HeroAt(index).Jewel(),
164 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
167 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
169 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
173 void BattleState::LoadInventory() {
174 const Inventory &inv(*res->inventory);
176 itemMenu.Reserve(inv.MaxItems());
177 for (int i(0); i < inv.MaxItems(); ++i) {
178 const Item *item(inv.ItemAt(i));
180 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
182 itemMenu.AddEmptyEntry();
188 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
192 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
193 // TODO: check for victory or defeat
195 ctrl.PopState(); // quit the battle scene
198 if (AttackSelectionDone()) {
199 ctrl.PushState(new PerformAttacks(this));
201 ctrl.PushState(new SelectMoveAction(this));
205 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
210 void BattleState::ClearAllAttacks() {
212 for (int i(0); i < numHeroes; ++i) {
213 attackChoices[i] = AttackChoice(this);
218 void BattleState::HandleEvents(const Input &input) {
222 void BattleState::UpdateWorld(float deltaT) {
226 void BattleState::Render(SDL_Surface *screen) {
227 Vector<int> offset(CalculateScreenOffset(screen));
229 RenderBackground(screen, offset);
230 RenderMonsters(screen, offset);
231 // RenderHeroes(screen, offset);
232 RenderHeroTags(screen, offset);
235 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
237 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
239 destRect.x = offset.X();
240 destRect.y = offset.Y();
241 destRect.w = background->w;
242 destRect.h = background->h;
243 SDL_BlitSurface(background, 0, screen, &destRect);
246 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
247 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
248 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
252 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
253 for (int i(0); i < numHeroes; ++i) {
254 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset, 0, 1);
258 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
259 int tagHeight(attackTypeMenu.Height());
260 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
262 for (int i(0); i < numHeroes; ++i) {
263 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);