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 "../geometry/operators.h"
17 #include "../graphics/Sprite.h"
21 using app::Application;
23 using common::Inventory;
25 using geometry::Point;
26 using geometry::Vector;
33 void BattleState::AddMonster(const Monster &m) {
34 if (monsters.size() >= monstersLayout->NumPositions()) {
35 throw std::overflow_error("too many monsters for layout");
37 monsters.push_back(m);
40 void BattleState::AddHero(const Hero &h) {
41 if (heroes.size() >= heroesLayout->NumPositions()) {
42 throw std::overflow_error("too many heroes for layout");
48 void BattleState::Resize(int w, int h) {
53 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
54 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
55 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
56 attackChoices.resize(heroes.size());
57 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
58 spellMenus.push_back(res->spellMenuPrototype);
59 // TODO: insert spell menu entries
60 ikariMenus.push_back(res->ikariMenuPrototype);
61 // TODO: insert ikari menu entries
62 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
64 // TODO: insert item menu entries
65 itemMenu = res->itemMenuPrototype;
69 void BattleState::LoadInventory() {
70 const Inventory &inv(*res->inventory);
72 itemMenu.Reserve(inv.MaxItems());
73 for (int i(0); i < inv.MaxItems(); ++i) {
74 const Item *item(inv.ItemAt(i));
76 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
78 itemMenu.AddEmptyEntry();
83 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
87 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
88 // TODO: check for victory, defeat or run
89 // reset attack choices
91 attackChoices.clear();
92 attackChoices.resize(heroes.size());
93 ctrl.PushState(new SelectMoveAction(this));
96 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
101 void BattleState::HandleInput(const Input &input) {
105 void BattleState::UpdateWorld(float deltaT) {
109 void BattleState::Render(SDL_Surface *screen) {
110 Vector<int> offset(CalculateScreenOffset(screen));
112 RenderBackground(screen, offset);
113 RenderMonsters(screen, offset);
114 // RenderHeroes(screen, offset);
115 RenderHeroTags(screen, offset);
118 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
120 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
122 destRect.x = offset.X();
123 destRect.y = offset.Y();
124 destRect.w = background->w;
125 destRect.h = background->h;
126 SDL_BlitSurface(background, 0, screen, &destRect);
129 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
130 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
131 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
135 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
136 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
137 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
141 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
142 int tagHeight(attackTypeMenu.Height());
143 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
144 int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
146 Point<int> tagPosition[4];
147 tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
148 tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
149 tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
150 tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
152 for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
153 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);