]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
reworked PerformAttacks
[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/Animation.h"
21 #include "../../graphics/Font.h"
22 #include "../../graphics/Frame.h"
23
24 #include <cstring>
25
26 using app::Application;
27 using app::Input;
28 using geometry::Point;
29 using geometry::Vector;
30
31 namespace battle {
32
33 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
34         ctrl = &c;
35         battle->WriteOrder(order);
36 }
37
38 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
39         ctrl = 0;
40 }
41
42 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
43
44 }
45
46 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
47
48 }
49
50
51 void PerformAttacks::Resize(int width, int height) {
52
53 }
54
55
56 void PerformAttacks::HandleEvents(const Input &input) {
57         if (titleBarTimer.Running() || (moveAnimation && moveAnimation->Running())) return;
58
59         if (moveAnimation) moveAnimation->Stop();
60         titleBarTimer.Clear();
61
62         ++cursor;
63
64         while (cursor < int(order.size())) {
65                 if (order[cursor].isMonster) {
66                         if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
67                 } else {
68                         if (battle->HeroAt(order[cursor].index).Health() > 0) break;
69                 }
70                 ++cursor;
71         }
72
73         if (cursor >= int(order.size())) {
74                 battle->ClearAllAttacks();
75                 ctrl->PopState();
76                 return;
77         }
78
79         if (order[cursor].isMonster) {
80                 const Monster &monster(battle->MonsterAt(order[cursor].index));
81                 titleBarText = monster.Name();
82                 moveAnimation = 0;
83         } else {
84                 Hero &hero(battle->HeroAt(order[cursor].index));
85                 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
86
87                 switch (ac.GetType()) {
88                         case AttackChoice::SWORD:
89                                 titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
90                                 moveAnimation = hero.AttackAnimation();
91                                 break;
92                         case AttackChoice::MAGIC:
93                                 titleBarText = ac.GetSpell()->Name();
94                                 moveAnimation = hero.SpellAnimation();
95                                 break;
96                         case AttackChoice::DEFEND:
97                                 titleBarText = "Defends.";
98                                 moveAnimation = 0;
99                                 break;
100                         case AttackChoice::IKARI:
101                                 if (ac.GetItem()->HasIkari()) {
102                                         titleBarText = ac.GetItem()->GetIkari()->Name();
103                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
104                                                 moveAnimation = hero.SpellAnimation();
105                                         } else {
106                                                 moveAnimation = hero.AttackAnimation();
107                                         }
108                                 }
109                                 break;
110                         case AttackChoice::ITEM:
111                                 titleBarText = ac.GetItem()->Name();
112                                 moveAnimation = 0;
113                                 break;
114                         case AttackChoice::UNDECIDED:
115                                 titleBarText = "UNDECIDED";
116                                 moveAnimation = 0;
117                                 break;
118                 }
119         }
120
121         titleBarTimer = GraphicsTimers().StartCountdown(1500);
122         if (moveAnimation) moveAnimation->Start(*this);
123 }
124
125
126 void PerformAttacks::UpdateWorld(float deltaT) {
127
128 }
129
130 void PerformAttacks::Render(SDL_Surface *screen) {
131         Vector<int> offset(battle->CalculateScreenOffset(screen));
132         battle->RenderBackground(screen, offset);
133         battle->RenderMonsters(screen, offset);
134         battle->RenderHeroes(screen, offset);
135         battle->RenderSmallHeroTags(screen, offset);
136         RenderTitleBar(screen, offset);
137 }
138
139 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
140         if (!titleBarText || !titleBarTimer.Running()) return;
141
142         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
143         battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->BackgroundWidth(), height);
144
145         Point<int> textPosition(
146                         (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
147                         battle->Res().titleFrame->BorderHeight());
148         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
149 }
150
151 }