]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added rough implementation of a 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 "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 using graphics::Menu;
24
25 using std::vector;
26
27 namespace battle {
28
29 void BattleState::AddMonster(const Monster &m) {
30         if (monsters.size() >= monstersLayout->NumPositions()) {
31                 throw std::overflow_error("too many monsters for layout");
32         }
33         monsters.push_back(m);
34 }
35
36 void BattleState::AddHero(const Hero &h) {
37         if (heroes.size() >= heroesLayout->NumPositions()) {
38                 throw std::overflow_error("too many heroes for layout");
39         }
40         heroes.push_back(h);
41 }
42
43
44 void BattleState::Resize(int w, int h) {
45
46 }
47
48
49 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
50         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
51         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
52         attackChoices.resize(heroes.size());
53         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
54                 // TODO: extract menu dimensions to resources
55                 Menu</* Spell */ void *> spellMenu(res->normalFont, 12, 6, 8, 2, 32);
56                 spellMenu.Add("Strong   : 3", 0);
57                 spellMenu.Add("Stronger : 8", 0);
58                 spellMenu.Add("Champion :16", 0);
59                 spellMenu.Add("Rally    :10", 0);
60                 spellMenu.Add("Valor    :30", 0);
61                 spellMenus.push_back(spellMenu);
62                 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
63         }
64 }
65
66 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
67
68 }
69
70 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
71         // TODO: check for victory, defeat or run
72         // reset attack choices
73         activeHero = -1;
74         attackChoices.clear();
75         attackChoices.resize(heroes.size());
76         ctrl.PushState(new SelectMoveAction(this));
77 }
78
79 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
80
81 }
82
83
84 void BattleState::HandleInput(const Input &input) {
85
86 }
87
88 void BattleState::UpdateWorld(float deltaT) {
89
90 }
91
92 void BattleState::Render(SDL_Surface *screen) {
93         Vector<int> offset(CalculateScreenOffset(screen));
94
95         RenderBackground(screen, offset);
96         RenderMonsters(screen, offset);
97 //      RenderHeroes(screen, offset);
98         RenderHeroTags(screen, offset);
99 }
100
101 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
102         // black for now
103         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
104         SDL_Rect destRect;
105         destRect.x = offset.X();
106         destRect.y = offset.Y();
107         destRect.w = background->w;
108         destRect.h = background->h;
109         SDL_BlitSurface(background, 0, screen, &destRect);
110 }
111
112 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
113         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
114                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
115         }
116 }
117
118 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
119         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
120                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
121         }
122 }
123
124 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
125         int tagHeight(attackTypeMenu.Height());
126         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
127         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
128
129         Point<int> tagPosition[4];
130         tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
131         tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
132         tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
133         tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
134
135         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
136                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);
137         }
138 }
139
140 }