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/Inventory.h"
15 #include "../common/Item.h"
16 #include "../common/Spell.h"
17 #include "../geometry/operators.h"
18 #include "../graphics/Sprite.h"
22 using app::Application;
24 using common::Inventory;
27 using geometry::Point;
28 using geometry::Vector;
35 void BattleState::AddMonster(const Monster &m) {
36 if (monsters.size() >= monstersLayout->NumPositions()) {
37 throw std::overflow_error("too many monsters for layout");
39 monsters.push_back(m);
42 void BattleState::AddHero(const Hero &h) {
43 if (heroes.size() >= heroesLayout->NumPositions()) {
44 throw std::overflow_error("too many heroes for layout");
50 void BattleState::Resize(int w, int h) {
55 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
56 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
57 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
58 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
59 spellMenus.push_back(res->spellMenuPrototype);
61 ikariMenus.push_back(res->ikariMenuPrototype);
62 // TODO: insert ikari menu entries
63 heroTags[i] = HeroTag(&heroes[i], attackChoices + i, res, HeroTag::Alignment((i + 1) % 2));
66 int tagHeight(attackTypeMenu.Height());
67 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
68 int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
69 heroTagPositions[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
70 heroTagPositions[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
71 heroTagPositions[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
72 heroTagPositions[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
74 // TODO: insert item menu entries
75 itemMenu = res->itemMenuPrototype;
79 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
80 spellMenus[index].Clear();
81 spellMenus[index].Reserve(HeroAt(index).Spells().size());
82 for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
83 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
84 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
88 void BattleState::LoadInventory() {
89 const Inventory &inv(*res->inventory);
91 itemMenu.Reserve(inv.MaxItems());
92 for (int i(0); i < inv.MaxItems(); ++i) {
93 const Item *item(inv.ItemAt(i));
95 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
97 itemMenu.AddEmptyEntry();
102 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
106 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
107 // TODO: check for victory, defeat or run
108 // reset attack choices
110 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
111 attackChoices[i] = AttackChoice(this);
113 ctrl.PushState(new SelectMoveAction(this));
116 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
121 void BattleState::HandleInput(const Input &input) {
125 void BattleState::UpdateWorld(float deltaT) {
129 void BattleState::Render(SDL_Surface *screen) {
130 Vector<int> offset(CalculateScreenOffset(screen));
132 RenderBackground(screen, offset);
133 RenderMonsters(screen, offset);
134 // RenderHeroes(screen, offset);
135 RenderHeroTags(screen, offset);
138 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
140 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
142 destRect.x = offset.X();
143 destRect.y = offset.Y();
144 destRect.w = background->w;
145 destRect.h = background->h;
146 SDL_BlitSurface(background, 0, screen, &destRect);
149 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
150 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
151 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
155 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
156 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
157 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
161 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
162 int tagHeight(attackTypeMenu.Height());
163 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
165 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
166 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);