]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectItem.cpp
made battle's heroes into an array
[l2e.git] / src / battle / states / SelectItem.cpp
1 /*
2  * SelectItem.cpp
3  *
4  *  Created on: Aug 9, 2012
5  *      Author: holy
6  */
7
8 #include "SelectItem.h"
9
10 #include "SelectAttackType.h"
11 #include "SelectTarget.h"
12 #include "../BattleState.h"
13 #include "../../app/Application.h"
14 #include "../../app/Input.h"
15 #include "../../common/Item.h"
16 #include "../../geometry/Point.h"
17 #include "../../geometry/operators.h"
18 #include "../../graphics/Frame.h"
19
20 using app::Application;
21 using app::Input;
22 using common::Item;
23 using geometry::Point;
24 using geometry::Vector;
25 using graphics::Frame;
26
27 namespace battle {
28
29 void SelectItem::EnterState(Application &c, SDL_Surface *screen) {
30         ctrl = &c;
31 }
32
33 void SelectItem::ExitState(Application &c, SDL_Surface *screen) {
34         ctrl = 0;
35 }
36
37 void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) {
38         if (battle->ActiveHeroTargets().HasSelected()) {
39                 battle->SetAttackType(AttackChoice::ITEM);
40                 battle->ActiveHeroAttackChoice().SetItem(battle->GetItemMenu().Selected());
41                 ctrl.PopState();
42         }
43 }
44
45 void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
46
47 }
48
49
50 void SelectItem::Resize(int width, int height) {
51
52 }
53
54
55 void SelectItem::HandleEvents(const Input &input) {
56         if (input.JustPressed(Input::ACTION_A)) {
57                 if (battle->GetItemMenu().SelectedIsEnabled()) {
58                         const Item *item(battle->GetItemMenu().Selected());
59                         battle->ActiveHeroTargets().Reset();
60                         if (item->GetTargetingMode().TargetsAlly()) {
61                                 battle->ActiveHeroTargets().SelectHeroes();
62                         } else {
63                                 battle->ActiveHeroTargets().SelectEnemies();
64                         }
65                         if (item->GetTargetingMode().TargetsAll()) {
66                                 battle->SetAttackType(AttackChoice::ITEM);
67                                 // TODO: remove item from inventory
68                                 battle->ActiveHeroAttackChoice().SetItem(item);
69                                 battle->NextHero();
70                                 ctrl->PopState();
71                         } else {
72                                 if (item->GetTargetingMode().TargetsSingle()) {
73                                         battle->ActiveHeroTargets().SetSingle();
74                                 } else {
75                                         battle->ActiveHeroTargets().SetMultiple();
76                                 }
77                                 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().itemTargetCursor));
78                         }
79                 }
80         }
81         if (input.JustPressed(Input::ACTION_B)) {
82                 ctrl->PopState(); // return control to parent
83         }
84         if (input.JustPressed(Input::PAD_UP)) {
85                 battle->GetItemMenu().PreviousRow();
86         }
87         if (input.JustPressed(Input::PAD_RIGHT)) {
88                 battle->GetItemMenu().NextItem();
89         }
90         if (input.JustPressed(Input::PAD_DOWN)) {
91                 battle->GetItemMenu().NextRow();
92         }
93         if (input.JustPressed(Input::PAD_LEFT)) {
94                 battle->GetItemMenu().PreviousItem();
95         }
96 }
97
98 void SelectItem::UpdateWorld(float deltaT) {
99
100 }
101
102 void SelectItem::Render(SDL_Surface *screen) {
103         parent->Render(screen);
104         Vector<int> offset(battle->CalculateScreenOffset(screen));
105         RenderFrame(screen, offset);
106         RenderHeadline(screen, offset);
107         RenderMenu(screen, offset);
108 }
109
110 void SelectItem::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
111         const Frame *frame(battle->Res().selectFrame);
112         Point<int> position(frame->BorderWidth(), frame->BorderHeight());
113         int width(battle->BackgroundWidth() - 2 * frame->BorderWidth());
114         int height(battle->Res().normalFont->CharHeight() * 13);
115         frame->Draw(screen, position + offset, width, height);
116 }
117
118 void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
119         const Resources &res(battle->Res());
120         Point<int> position(
121                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
122                         2 * res.selectFrame->BorderHeight());
123         res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
124 }
125
126 void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
127         const Resources &res(battle->Res());
128         Point<int> position(
129                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
130                         2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
131         battle->GetItemMenu().Draw(screen, position + offset);
132 }
133
134 }