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/Frame.h"
21 #include "../graphics/Sprite.h"
26 using app::Application;
28 using common::Inventory;
31 using geometry::Point;
32 using geometry::Vector;
39 void BattleState::AddMonster(const Monster &m) {
40 if (monsters.size() >= monstersLayout->NumPositions()) {
41 throw std::overflow_error("too many monsters for layout");
43 monsters.push_back(m);
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");
50 heroes[numHeroes] = h;
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]);
60 void BattleState::Resize(int w, int h) {
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;
71 ikariMenus[i] = res->ikariMenuPrototype;
73 heroTags[i] = HeroTag(this, i);
74 smallHeroTags[i] = SmallHeroTag(this, i);
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);
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);
94 itemMenu = res->itemMenuPrototype;
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());
107 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
108 ikariMenus[index].Clear();
109 ikariMenus[index].Reserve(6);
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(),
118 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
120 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
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(),
130 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
132 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
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(),
142 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
144 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
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(),
154 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
156 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
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(),
166 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
168 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
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(),
178 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
180 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
184 void BattleState::LoadInventory() {
185 const Inventory &inv(*res->inventory);
187 itemMenu.Reserve(inv.MaxItems());
188 for (int i(0); i < inv.MaxItems(); ++i) {
189 const Item *item(inv.ItemAt(i));
191 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
193 itemMenu.AddEmptyEntry();
199 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
203 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
204 // TODO: check for victory or defeat
206 ctrl.PopState(); // quit the battle scene
209 if (AttackSelectionDone()) {
210 ctrl.PushState(new PerformAttacks(this));
212 ctrl.PushState(new SelectMoveAction(this));
216 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
221 void BattleState::ClearAllAttacks() {
223 for (int i(0); i < numHeroes; ++i) {
224 attackChoices[i] = AttackChoice(this);
229 void BattleState::HandleEvents(const Input &input) {
233 void BattleState::UpdateWorld(float deltaT) {
237 void BattleState::Render(SDL_Surface *screen) {
238 Vector<int> offset(CalculateScreenOffset(screen));
239 RenderBackground(screen, offset);
240 RenderMonsters(screen, offset);
243 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
245 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
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);
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);
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);
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);
270 for (int i(0); i < numHeroes; ++i) {
271 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
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);
281 rect.y = offset.Y() + BackgroundHeight() - tagHeight;
282 rect.w = BackgroundWidth();
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);
289 for (int i(0); i < numHeroes; ++i) {
290 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);