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 attackChoices.resize(heroes.size());
52 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
53 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
57 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
61 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
62 // TODO: check for victory, defeat or run
63 // reset attack choices
65 attackChoices.clear();
66 attackChoices.resize(heroes.size());
67 ctrl.PushState(new SelectMoveAction(this));
70 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
75 void BattleState::HandleInput(const Input &input) {
79 void BattleState::UpdateWorld(float deltaT) {
83 void BattleState::Render(SDL_Surface *screen) {
84 Vector<int> offset(CalculateScreenOffset(screen));
86 RenderBackground(screen, offset);
87 RenderMonsters(screen, offset);
88 // RenderHeroes(screen, offset);
89 RenderHeroTags(screen, offset);
92 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
94 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
96 destRect.x = offset.X();
97 destRect.y = offset.Y();
98 destRect.w = background->w;
99 destRect.h = background->h;
100 SDL_BlitSurface(background, 0, screen, &destRect);
103 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
104 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
105 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
109 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
110 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
111 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
115 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
116 int tagHeight(attackTypeMenu.Height());
117 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
118 int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
120 Point<int> tagPosition[4];
121 tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
122 tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
123 tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
124 tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
126 for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
127 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);