4 * Created on: Aug 5, 2012
8 #include "BattleState.h"
10 #include "PartyLayout.h"
11 #include "../geometry/operators.h"
12 #include "../graphics/Sprite.h"
16 using app::Application;
17 using geometry::Point;
18 using geometry::Vector;
24 void BattleState::AddMonster(const Monster &m) {
25 if (monsters.size() >= monstersLayout->NumPositions()) {
26 throw std::overflow_error("too many monsters for layout");
28 monsters.push_back(m);
31 void BattleState::AddHero(const Hero &h) {
32 if (heroes.size() >= heroesLayout->NumPositions()) {
33 throw std::overflow_error("too many heroes for layout");
39 void BattleState::Resize(int w, int h) {
44 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
45 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
46 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
49 void BattleState::ExitState() {
54 void BattleState::HandleEvent(const SDL_Event &) {
58 void BattleState::UpdateWorld(float deltaT) {
62 void BattleState::Render(SDL_Surface *screen) {
64 (screen->w - background->w) / 2,
65 (screen->h - background->h) / 2);
67 RenderBackground(screen, offset);
68 RenderMonsters(screen, offset);
69 RenderHeroes(screen, offset);
72 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
74 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
76 destRect.x = offset.X();
77 destRect.y = offset.Y();
78 destRect.w = background->w;
79 destRect.h = background->h;
80 SDL_BlitSurface(background, 0, screen, &destRect);
83 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
84 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
85 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
89 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
90 for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
91 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);