4 * Created on: Aug 5, 2012
8 #include "BattleState.h"
10 #include "PartyLayout.h"
11 #include "../app/Application.h"
12 #include "../app/Input.h"
13 #include "../geometry/operators.h"
14 #include "../graphics/Sprite.h"
18 using app::Application;
20 using geometry::Point;
21 using geometry::Vector;
27 void BattleState::AddMonster(const Monster &m) {
28 if (monsters.size() >= monstersLayout->NumPositions()) {
29 throw std::overflow_error("too many monsters for layout");
31 monsters.push_back(m);
34 void BattleState::AddHero(const Hero &h) {
35 if (heroes.size() >= heroesLayout->NumPositions()) {
36 throw std::overflow_error("too many heroes for layout");
42 void BattleState::Resize(int w, int h) {
47 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
48 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
49 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
50 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
51 heroTags.push_back(HeroTag(&heroes[i], HeroTag::Alignment((i + 1) % 2)));
55 void BattleState::ExitState() {
60 void BattleState::HandleInput(const Input &input) {
61 attackTypeMenu.ReadInput(input);
64 void BattleState::UpdateWorld(float deltaT) {
68 void BattleState::Render(SDL_Surface *screen) {
70 (screen->w - background->w) / 2,
71 (screen->h - background->h) / 2);
73 RenderBackground(screen, offset);
74 RenderMonsters(screen, offset);
75 // RenderHeroes(screen, offset);
76 RenderHeroTags(screen, offset);
77 RenderAttackTypeMenu(screen, offset);
80 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
82 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
84 destRect.x = offset.X();
85 destRect.y = offset.Y();
86 destRect.w = background->w;
87 destRect.h = background->h;
88 SDL_BlitSurface(background, 0, screen, &destRect);
91 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
92 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
93 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
97 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
98 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
99 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
103 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
104 int uiHeight(background->h / 2), uiOffset(uiHeight);
105 int tagHeight((uiHeight - attackTypeMenu.IconHeight()) / 2);
106 int tagWidth((background->w - attackTypeMenu.IconWidth()) / 2);
108 Point<int> tagPosition[4];
109 tagPosition[0] = Point<int>(0, uiOffset);
110 tagPosition[1] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset);
111 tagPosition[2] = Point<int>(0, uiOffset + tagHeight + attackTypeMenu.IconHeight());
112 tagPosition[3] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset + tagHeight + attackTypeMenu.IconHeight());
114 for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
115 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset);
119 void BattleState::RenderAttackTypeMenu(SDL_Surface *screen, const Vector<int> &offset) {
121 (background->w - attackTypeMenu.Width()) / 2,
122 (background->h * 3 / 4) - (attackTypeMenu.Height() / 2));
123 attackTypeMenu.Render(screen, position + offset);