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 "../geometry/operators.h"
15 #include "../graphics/Sprite.h"
19 using app::Application;
21 using geometry::Point;
22 using geometry::Vector;
29 void BattleState::AddMonster(const Monster &m) {
30 if (monsters.size() >= monstersLayout->NumPositions()) {
31 throw std::overflow_error("too many monsters for layout");
33 monsters.push_back(m);
36 void BattleState::AddHero(const Hero &h) {
37 if (heroes.size() >= heroesLayout->NumPositions()) {
38 throw std::overflow_error("too many heroes for layout");
44 void BattleState::Resize(int w, int h) {
49 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
50 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
51 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
52 attackChoices.resize(heroes.size());
53 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
54 spellMenus.push_back(res->spellMenuPrototype);
55 // TODO: insert spell menu entries
56 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
58 // TODO: insert item menu entries
59 itemMenu = res->itemMenuPrototype;
62 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
66 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
67 // TODO: check for victory, defeat or run
68 // reset attack choices
70 attackChoices.clear();
71 attackChoices.resize(heroes.size());
72 ctrl.PushState(new SelectMoveAction(this));
75 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
80 void BattleState::HandleInput(const Input &input) {
84 void BattleState::UpdateWorld(float deltaT) {
88 void BattleState::Render(SDL_Surface *screen) {
89 Vector<int> offset(CalculateScreenOffset(screen));
91 RenderBackground(screen, offset);
92 RenderMonsters(screen, offset);
93 // RenderHeroes(screen, offset);
94 RenderHeroTags(screen, offset);
97 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
99 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
101 destRect.x = offset.X();
102 destRect.y = offset.Y();
103 destRect.w = background->w;
104 destRect.h = background->h;
105 SDL_BlitSurface(background, 0, screen, &destRect);
108 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
109 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
110 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
114 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
115 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
116 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
120 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
121 int tagHeight(attackTypeMenu.Height());
122 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
123 int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
125 Point<int> tagPosition[4];
126 tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
127 tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
128 tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
129 tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
131 for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
132 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);