]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added Frame class for drawing bordered windows
[l2e.git] / src / battle / BattleState.cpp
1 /*
2  * BattleState.cpp
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #include "BattleState.h"
9
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"
16
17 #include <stdexcept>
18
19 using app::Application;
20 using app::Input;
21 using geometry::Point;
22 using geometry::Vector;
23
24 using std::vector;
25
26 namespace battle {
27
28 void BattleState::AddMonster(const Monster &m) {
29         if (monsters.size() >= monstersLayout->NumPositions()) {
30                 throw std::overflow_error("too many monsters for layout");
31         }
32         monsters.push_back(m);
33 }
34
35 void BattleState::AddHero(const Hero &h) {
36         if (heroes.size() >= heroesLayout->NumPositions()) {
37                 throw std::overflow_error("too many heroes for layout");
38         }
39         heroes.push_back(h);
40 }
41
42
43 void BattleState::Resize(int w, int h) {
44
45 }
46
47
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], heroTagFrame, activeHeroTagFrame, HeroTag::Alignment((i + 1) % 2)));
54         }
55 }
56
57 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
58
59 }
60
61 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
62         // reset attack choices
63         activeHero = -1;
64         attackChoices.clear();
65         attackChoices.resize(heroes.size());
66         ctrl.PushState(new SelectMoveAction(this));
67 }
68
69 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
70
71 }
72
73
74 void BattleState::HandleInput(const Input &input) {
75
76 }
77
78 void BattleState::UpdateWorld(float deltaT) {
79
80 }
81
82 void BattleState::Render(SDL_Surface *screen) {
83         Vector<int> offset(CalculateScreenOffset(screen));
84
85         RenderBackground(screen, offset);
86         RenderMonsters(screen, offset);
87 //      RenderHeroes(screen, offset);
88         RenderHeroTags(screen, offset);
89 }
90
91 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
92         // black for now
93         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
94         SDL_Rect destRect;
95         destRect.x = offset.X();
96         destRect.y = offset.Y();
97         destRect.w = background->w;
98         destRect.h = background->h;
99         SDL_BlitSurface(background, 0, screen, &destRect);
100 }
101
102 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
103         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
104                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
105         }
106 }
107
108 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
109         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
110                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
111         }
112 }
113
114 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
115         int uiHeight(BackgroundHeight() / 2), uiOffset(uiHeight);
116         int tagHeight((uiHeight - attackTypeMenu.IconHeight()) / 2);
117         int tagWidth((BackgroundWidth() - attackTypeMenu.IconWidth()) / 2);
118
119         Point<int> tagPosition[4];
120         tagPosition[0] = Point<int>(0, uiOffset);
121         tagPosition[1] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset);
122         tagPosition[2] = Point<int>(0, uiOffset + tagHeight + attackTypeMenu.IconHeight());
123         tagPosition[3] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset + tagHeight + attackTypeMenu.IconHeight());
124
125         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
126                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);
127         }
128 }
129
130 }