]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added some information about menus
[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                 spellMenus.push_back(res->spellMenuPrototype);
55                 // TODO: insert spell menu entries
56                 ikariMenus.push_back(res->ikariMenuPrototype);
57                 // TODO: insert ikari menu entries
58                 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
59         }
60         // TODO: insert item menu entries
61         itemMenu = res->itemMenuPrototype;
62 }
63
64 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
65
66 }
67
68 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
69         // TODO: check for victory, defeat or run
70         // reset attack choices
71         activeHero = -1;
72         attackChoices.clear();
73         attackChoices.resize(heroes.size());
74         ctrl.PushState(new SelectMoveAction(this));
75 }
76
77 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
78
79 }
80
81
82 void BattleState::HandleInput(const Input &input) {
83
84 }
85
86 void BattleState::UpdateWorld(float deltaT) {
87
88 }
89
90 void BattleState::Render(SDL_Surface *screen) {
91         Vector<int> offset(CalculateScreenOffset(screen));
92
93         RenderBackground(screen, offset);
94         RenderMonsters(screen, offset);
95 //      RenderHeroes(screen, offset);
96         RenderHeroTags(screen, offset);
97 }
98
99 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
100         // black for now
101         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
102         SDL_Rect destRect;
103         destRect.x = offset.X();
104         destRect.y = offset.Y();
105         destRect.w = background->w;
106         destRect.h = background->h;
107         SDL_BlitSurface(background, 0, screen, &destRect);
108 }
109
110 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
111         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
112                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
113         }
114 }
115
116 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
117         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
118                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
119         }
120 }
121
122 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
123         int tagHeight(attackTypeMenu.Height());
124         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
125         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
126
127         Point<int> tagPosition[4];
128         tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
129         tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
130         tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
131         tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
132
133         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
134                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);
135         }
136 }
137
138 }