]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added battle move menu
[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 "../app/Application.h"
12 #include "../app/Input.h"
13 #include "../geometry/operators.h"
14 #include "../graphics/Sprite.h"
15
16 #include <stdexcept>
17
18 using app::Application;
19 using app::Input;
20 using geometry::Point;
21 using geometry::Vector;
22
23 using std::vector;
24
25 namespace battle {
26
27 void BattleState::AddMonster(const Monster &m) {
28         if (monsters.size() >= monstersLayout->NumPositions()) {
29                 throw std::overflow_error("too many monsters for layout");
30         }
31         monsters.push_back(m);
32 }
33
34 void BattleState::AddHero(const Hero &h) {
35         if (heroes.size() >= heroesLayout->NumPositions()) {
36                 throw std::overflow_error("too many heroes for layout");
37         }
38         heroes.push_back(h);
39 }
40
41
42 void BattleState::Resize(int w, int h) {
43
44 }
45
46
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)));
52         }
53 }
54
55 void BattleState::ExitState() {
56
57 }
58
59
60 void BattleState::HandleInput(const Input &input) {
61         if (moveChoice == -1) {
62                 if (input.IsDown(Input::PAD_UP)) {
63                         moveMenu.Select(MoveMenu::CHANGE);
64                 } else if (input.IsDown(Input::PAD_DOWN)) {
65                         moveMenu.Select(MoveMenu::RUN);
66                 } else {
67                         moveMenu.Select(MoveMenu::ATTACK);
68                 }
69
70                 if (input.JustPressed(Input::ACTION_A)) {
71                         moveChoice = moveMenu.Selected();
72                 }
73         } else {
74                 if (input.IsDown(Input::PAD_UP)) {
75                         attackTypeMenu.Select(AttackTypeMenu::MAGIC);
76                 } else if (input.IsDown(Input::PAD_RIGHT)) {
77                         attackTypeMenu.Select(AttackTypeMenu::DEFEND);
78                 } else if (input.IsDown(Input::PAD_DOWN)) {
79                         attackTypeMenu.Select(AttackTypeMenu::IKARI);
80                 } else if (input.IsDown(Input::PAD_LEFT)) {
81                         attackTypeMenu.Select(AttackTypeMenu::ITEM);
82                 } else {
83                         attackTypeMenu.Select(AttackTypeMenu::SWORD);
84                 }
85
86                 if (input.JustPressed(Input::ACTION_A)) {
87                         activeHero = (activeHero + 1) % 4;
88                 }
89         }
90 }
91
92 void BattleState::UpdateWorld(float deltaT) {
93
94 }
95
96 void BattleState::Render(SDL_Surface *screen) {
97         Vector<int> offset(
98                         (screen->w - background->w) / 2,
99                         (screen->h - background->h) / 2);
100
101         RenderBackground(screen, offset);
102         RenderMonsters(screen, offset);
103 //      RenderHeroes(screen, offset);
104         RenderHeroTags(screen, offset);
105         if (moveChoice == -1) {
106                 RenderMoveMenu(screen, offset);
107         } else {
108                 RenderAttackTypeMenu(screen, offset);
109         }
110 }
111
112 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
113         // black for now
114         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
115         SDL_Rect destRect;
116         destRect.x = offset.X();
117         destRect.y = offset.Y();
118         destRect.w = background->w;
119         destRect.h = background->h;
120         SDL_BlitSurface(background, 0, screen, &destRect);
121 }
122
123 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
124         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
125                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
126         }
127 }
128
129 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
130         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
131                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
132         }
133 }
134
135 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
136         int uiHeight(background->h / 2), uiOffset(uiHeight);
137         int tagHeight((uiHeight - attackTypeMenu.IconHeight()) / 2);
138         int tagWidth((background->w - attackTypeMenu.IconWidth()) / 2);
139
140         Point<int> tagPosition[4];
141         tagPosition[0] = Point<int>(0, uiOffset);
142         tagPosition[1] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset);
143         tagPosition[2] = Point<int>(0, uiOffset + tagHeight + attackTypeMenu.IconHeight());
144         tagPosition[3] = Point<int>(tagWidth + attackTypeMenu.IconWidth(), uiOffset + tagHeight + attackTypeMenu.IconHeight());
145
146         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
147                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, i == activeHero);
148         }
149 }
150
151 void BattleState::RenderAttackTypeMenu(SDL_Surface *screen, const Vector<int> &offset) {
152         Point<int> position(
153                         (background->w - attackTypeMenu.Width()) / 2,
154                         (background->h * 3 / 4) - (attackTypeMenu.Height() / 2));
155         attackTypeMenu.Render(screen, position + offset);
156 }
157
158 void BattleState::RenderMoveMenu(SDL_Surface *screen, const Vector<int> &offset) {
159         Point<int> position(
160                         (background->w - moveMenu.Width()) / 2,
161                         (background->h * 3 / 4) - (moveMenu.Height() / 2));
162         moveMenu.Render(screen, position + offset);
163 }
164
165 }