]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
c34e1144cdc6e4e54de9ca9ec1b6130bb6dafbd6
[l2e.git] / src / battle / states / PerformAttacks.cpp
1 /*
2  * PerformAttacks.cpp
3  *
4  *  Created on: Aug 10, 2012
5  *      Author: holy
6  */
7
8 #include "PerformAttacks.h"
9
10 #include "../BattleState.h"
11 #include "../Hero.h"
12 #include "../Monster.h"
13 #include "../../app/Application.h"
14 #include "../../app/Input.h"
15 #include "../../common/Ikari.h"
16 #include "../../common/Item.h"
17 #include "../../common/Spell.h"
18 #include "../../geometry/operators.h"
19 #include "../../geometry/Point.h"
20 #include "../../graphics/Font.h"
21 #include "../../graphics/Frame.h"
22 #include "../../graphics/SimpleAnimation.h"
23
24 #include <cstring>
25
26 using app::Application;
27 using app::Input;
28 using geometry::Point;
29 using geometry::Vector;
30 using graphics::SimpleAnimation;
31
32 namespace battle {
33
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
35         ctrl = &c;
36         // TODO: push battle animation if enemy is faster
37 }
38
39 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
40         ctrl = 0;
41 }
42
43 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
44         fakeMoveTimer = GraphicsTimers().StartCountdown(850);
45 }
46
47 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
48
49 }
50
51
52 void PerformAttacks::Resize(int width, int height) {
53
54 }
55
56
57 void PerformAttacks::HandleEvents(const Input &input) {
58         if (fakeMoveTimer.JustHit()) {
59                 if (monsters) {
60                         if (titleBarText) {
61                                 titleBarText = 0;
62                                 ++cursor;
63                                 while (cursor < battle->MaxMonsters() && !battle->MonsterPositionOccupied(cursor)) {
64                                         ++cursor;
65                                 }
66                                 if (cursor >= battle->MaxMonsters()) {
67                                         battle->ClearAllAttacks();
68                                         ctrl->PopState();
69                                 }
70                         } else {
71                                 if (cursor == 0) {
72                                         battle->HeroAnimationAt(battle->NumHeroes() - 1).Stop();
73                                 }
74                                 titleBarText = battle->MonsterAt(cursor).Name();
75                         }
76                 } else {
77                         const AttackChoice &ac(battle->AttackChoiceAt(cursor));
78                         if (titleBarText) {
79                                 titleBarText = 0;
80
81                                 switch (ac.GetType()) {
82                                         case AttackChoice::SWORD:
83                                                 battle->HeroAnimationAt(cursor) = SimpleAnimation(
84                                                                 battle->HeroAt(cursor).Sprite(),
85                                                                 battle->HeroAt(cursor).AttackFrameTime(),
86                                                                 battle->HeroAt(cursor).AttackFrames(), 2);
87                                                 break;
88                                         case AttackChoice::MAGIC:
89                                                 battle->HeroAnimationAt(cursor) = SimpleAnimation(
90                                                                 battle->HeroAt(cursor).Sprite(),
91                                                                 battle->HeroAt(cursor).SpellFrameTime(),
92                                                                 battle->HeroAt(cursor).SpellFrames(), 3);
93                                                 break;
94                                         case AttackChoice::DEFEND:
95                                                 break;
96                                         case AttackChoice::IKARI:
97                                                 if (ac.GetItem()->HasIkari()) {
98                                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
99                                                                 battle->HeroAnimationAt(cursor) = SimpleAnimation(
100                                                                                 battle->HeroAt(cursor).Sprite(),
101                                                                                 battle->HeroAt(cursor).SpellFrameTime(),
102                                                                                 battle->HeroAt(cursor).SpellFrames(), 3);
103                                                         } else {
104                                                                 battle->HeroAnimationAt(cursor) = SimpleAnimation(
105                                                                                 battle->HeroAt(cursor).Sprite(),
106                                                                                 battle->HeroAt(cursor).AttackFrameTime(),
107                                                                                 battle->HeroAt(cursor).AttackFrames(), 2);
108                                                         }
109                                                 }
110                                                 break;
111                                         case AttackChoice::ITEM:
112                                                 break;
113                                         case AttackChoice::UNDECIDED:
114                                                 break;
115                                 }
116                                 battle->HeroAnimationAt(cursor).Start(*this);
117
118                                 ++cursor;
119                                 if (cursor == battle->NumHeroes()) {
120                                         cursor = 0;
121                                         monsters = true;
122                                 }
123                         } else {
124                                 if (cursor > 0) {
125                                         battle->HeroAnimationAt(cursor - 1).Stop();
126                                 }
127                                 switch (ac.GetType()) {
128                                         case AttackChoice::SWORD:
129                                                 titleBarText = battle->HeroAt(cursor).HasWeapon() ? battle->HeroAt(cursor).Weapon()->Name() : "Gauntlet";
130                                                 break;
131                                         case AttackChoice::MAGIC:
132                                                 titleBarText = ac.GetSpell()->Name();
133                                                 break;
134                                         case AttackChoice::DEFEND:
135                                                 titleBarText = "Defends.";
136                                                 break;
137                                         case AttackChoice::IKARI:
138                                                 titleBarText = ac.GetItem()->HasIkari() ? ac.GetItem()->GetIkari()->Name() : "No Ikari?";
139                                                 break;
140                                         case AttackChoice::ITEM:
141                                                 titleBarText = ac.GetItem()->Name();
142                                                 break;
143                                         case AttackChoice::UNDECIDED:
144                                                 titleBarText = "WTF???";
145                                                 break;
146                                 }
147                         }
148                 }
149         }
150 }
151
152
153 void PerformAttacks::UpdateWorld(float deltaT) {
154
155 }
156
157 void PerformAttacks::Render(SDL_Surface *screen) {
158         Vector<int> offset(battle->CalculateScreenOffset(screen));
159         battle->RenderBackground(screen, offset);
160         battle->RenderMonsters(screen, offset);
161         battle->RenderHeroes(screen, offset);
162         battle->RenderSmallHeroTags(screen, offset);
163         RenderTitleBar(screen, offset);
164 }
165
166 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
167         if (!titleBarText) return;
168
169         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
170         battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->BackgroundWidth(), height);
171
172         Point<int> textPosition(
173                         (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
174                         battle->Res().titleFrame->BorderHeight());
175         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
176 }
177
178 }