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;
28 void BattleState::AddMonster(const Monster &m) {
29 if (monsters.size() >= monstersLayout->NumPositions()) {
30 throw std::overflow_error("too many monsters for layout");
32 monsters.push_back(m);
35 void BattleState::AddHero(const Hero &h) {
36 if (heroes.size() >= heroesLayout->NumPositions()) {
37 throw std::overflow_error("too many heroes for layout");
43 void BattleState::Resize(int w, int h) {
48 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
49 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
50 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
51 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
52 heroTags.push_back(HeroTag(&heroes[i], HeroTag::Alignment((i + 1) % 2)));
56 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
60 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
61 // reset attack choices
63 attackChoices.clear();
64 attackChoices.resize(heroes.size());
65 ctrl.PushState(new SelectMoveAction(this));
68 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
73 void BattleState::HandleInput(const Input &input) {
77 void BattleState::UpdateWorld(float deltaT) {
81 void BattleState::Render(SDL_Surface *screen) {
82 Vector<int> offset(CalculateScreenOffset(screen));
84 RenderBackground(screen, offset);
85 RenderMonsters(screen, offset);
86 // RenderHeroes(screen, offset);
87 RenderHeroTags(screen, offset);
90 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
92 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
94 destRect.x = offset.X();
95 destRect.y = offset.Y();
96 destRect.w = background->w;
97 destRect.h = background->h;
98 SDL_BlitSurface(background, 0, screen, &destRect);
101 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
102 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
103 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
107 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
108 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
109 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
113 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
114 int uiHeight(BackgroundHeight() / 2), uiOffset(uiHeight);
115 int tagHeight((uiHeight - attackTypeMenu.IconHeight()) / 2);
116 int tagWidth((BackgroundWidth() - attackTypeMenu.IconWidth()) / 2);
118 Point<int> tagPosition[4];
119 tagPosition[0] = Point<int>(0, uiOffset);
120 tagPosition[1] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset);
121 tagPosition[2] = Point<int>(0, uiOffset + tagHeight + attackTypeMenu.IconHeight());
122 tagPosition[3] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset + tagHeight + attackTypeMenu.IconHeight());
124 for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
125 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);