]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added item select state
[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 real spell menu entries
56                 spellMenus.back().Add("Reset    : 0", 0, false);
57                 spellMenus.back().Add("Strong   : 3", 0);
58                 spellMenus.back().Add("Stronger : 8", 0);
59                 spellMenus.back().Add("Champion :16", 0);
60                 spellMenus.back().Add("Rally    :10", 0);
61                 spellMenus.back().Add("Escape   : 8", 0, false);
62                 spellMenus.back().Add("Valor    :30", 0);
63                 spellMenus.back().Add("Poison   : 2", 0);
64                 spellMenus.back().Add("Warp     : 8", 0, false);
65                 spellMenus.back().Add("Release  : 2", 0);
66                 spellMenus.back().Add("Waken    : 4", 0);
67                 spellMenus.back().Add("Light    : 0", 0, false);
68                 spellMenus.back().Add("Fake     : 4", 0);
69                 spellMenus.back().Add("Trick    : 5", 0);
70                 spellMenus.back().Add("Flash    : 5", 0);
71                 spellMenus.back().Add("Fireball : 6", 0);
72                 spellMenus.back().Add("Vortex   : 7", 0);
73                 spellMenus.back().Add("Blizzard : 8", 0);
74                 spellMenus.back().Add("Spark    : 3", 0);
75                 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
76         }
77         itemMenu = res->itemMenuPrototype;
78         itemMenu.Add("Antidote    : 9", 0);
79         itemMenu.Add("Magic jar   : 4", 0);
80         itemMenu.Add("Miracle     : 4", 0);
81         itemMenu.Add("Hi-Potion   : 6", 0);
82         itemMenu.Add("Hi-Magic    : 7", 0);
83         itemMenu.Add("Regain      : 4", 0);
84         itemMenu.Add("Power potion: 4", 0, false);
85         itemMenu.Add("Life potion : 1", 0, false);
86         itemMenu.Add("Escape      : 2", 0, false);
87         itemMenu.Add("Power gourd : 3", 0);
88         itemMenu.Add("Mystery pin : 2", 0);
89 }
90
91 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
92
93 }
94
95 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
96         // TODO: check for victory, defeat or run
97         // reset attack choices
98         activeHero = -1;
99         attackChoices.clear();
100         attackChoices.resize(heroes.size());
101         ctrl.PushState(new SelectMoveAction(this));
102 }
103
104 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
105
106 }
107
108
109 void BattleState::HandleInput(const Input &input) {
110
111 }
112
113 void BattleState::UpdateWorld(float deltaT) {
114
115 }
116
117 void BattleState::Render(SDL_Surface *screen) {
118         Vector<int> offset(CalculateScreenOffset(screen));
119
120         RenderBackground(screen, offset);
121         RenderMonsters(screen, offset);
122 //      RenderHeroes(screen, offset);
123         RenderHeroTags(screen, offset);
124 }
125
126 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
127         // black for now
128         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
129         SDL_Rect destRect;
130         destRect.x = offset.X();
131         destRect.y = offset.Y();
132         destRect.w = background->w;
133         destRect.h = background->h;
134         SDL_BlitSurface(background, 0, screen, &destRect);
135 }
136
137 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
138         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
139                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
140         }
141 }
142
143 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
144         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
145                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
146         }
147 }
148
149 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
150         int tagHeight(attackTypeMenu.Height());
151         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
152         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
153
154         Point<int> tagPosition[4];
155         tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
156         tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
157         tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
158         tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
159
160         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
161                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);
162         }
163 }
164
165 }