4 * Created on: Aug 5, 2012
8 #include "BattleState.h"
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"
24 using app::Application;
26 using common::Inventory;
29 using geometry::Point;
30 using geometry::Vector;
37 void BattleState::AddMonster(const Monster &m) {
38 if (monsters.size() >= monstersLayout->NumPositions()) {
39 throw std::overflow_error("too many monsters for layout");
41 monsters.push_back(m);
44 void BattleState::AddHero(const Hero &h) {
45 if (heroes.size() >= heroesLayout->NumPositions()) {
46 throw std::overflow_error("too many heroes for layout");
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]);
57 void BattleState::Resize(int w, int h) {
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);
68 ikariMenus.push_back(res->ikariMenuPrototype);
70 heroTags[i] = HeroTag(this, i);
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);
81 itemMenu = res->itemMenuPrototype;
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());
94 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
95 ikariMenus[index].Clear();
96 ikariMenus[index].Reserve(6);
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(),
105 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
107 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
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(),
117 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
119 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
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(),
129 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
131 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
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(),
141 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
143 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
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(),
153 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
155 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
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(),
165 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
167 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
171 void BattleState::LoadInventory() {
172 const Inventory &inv(*res->inventory);
174 itemMenu.Reserve(inv.MaxItems());
175 for (int i(0); i < inv.MaxItems(); ++i) {
176 const Item *item(inv.ItemAt(i));
178 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
180 itemMenu.AddEmptyEntry();
185 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
189 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
190 // TODO: check for victory, defeat or run
191 // reset attack choices
193 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
194 attackChoices[i] = AttackChoice(this);
196 ctrl.PushState(new SelectMoveAction(this));
199 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
204 void BattleState::HandleInput(const Input &input) {
208 void BattleState::UpdateWorld(float deltaT) {
212 void BattleState::Render(SDL_Surface *screen) {
213 Vector<int> offset(CalculateScreenOffset(screen));
215 RenderBackground(screen, offset);
216 RenderMonsters(screen, offset);
217 // RenderHeroes(screen, offset);
218 RenderHeroTags(screen, offset);
221 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
223 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
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);
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);
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);
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);
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);