1 #include "SelectTarget.h"
3 #include "SelectAttackType.h"
4 #include "../BattleState.h"
5 #include "../../app/Application.h"
6 #include "../../app/Input.h"
7 #include "../../math/Vector.h"
9 using app::Application;
16 SelectTarget::SelectTarget(
18 SelectAttackType *parent,
19 TargetSelection *selection,
20 const graphics::Sprite *cursorIcon)
23 , selection(selection)
24 , cursorIcon(cursorIcon)
30 void SelectTarget::OnEnterState(SDL_Surface *screen) {
31 OnResize(screen->w, screen->h);
34 void SelectTarget::OnExitState(SDL_Surface *screen) {
38 void SelectTarget::OnResumeState(SDL_Surface *screen) {
42 void SelectTarget::OnPauseState(SDL_Surface *screen) {
47 void SelectTarget::OnResize(int width, int height) {
48 Vector<int> offset(parent->ScreenOffset());
49 cursorOffset = Vector<int>(cursorIcon->Width() / -2, cursorIcon->Height()) + offset;
50 // offset the indicator by 1/8th to the right and top
51 indicatorOffset = cursorOffset + Vector<int>(cursorIcon->Width() / 8, cursorIcon->Height() / -8);
53 monsterPositions.clear();
54 monsterPositions.reserve(battle->NumMonsters());
55 for (int i(0), end(battle->NumMonsters()); i < end; ++i) {
56 monsterPositions.push_back(battle->MonsterAt(i).Position());
59 heroPositions.clear();
60 heroPositions.reserve(battle->NumHeroes());
61 for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
62 heroPositions.push_back(parent->HeroTagPositionAt(i) + parent->HeroTagAt(i).HeroOffset());
67 void SelectTarget::HandleEvents(const Input &input) {
68 if (input.JustPressed(Input::ACTION_A)) {
69 if (selection->CurrentIsSelected()) {
70 Ctrl().PopState(); // return control to parent
73 if (selection->SelectSingle()) {
74 Ctrl().PopState(); // return control to parent
78 if (input.JustPressed(Input::ACTION_B)) {
79 if (selection->CurrentIsSelected()) {
80 selection->Unselect();
82 selection->UnselectAll();
83 Ctrl().PopState(); // return control to parent
87 if (input.JustPressed(Input::PAD_UP)) {
90 if (input.JustPressed(Input::PAD_RIGHT)) {
91 selection->MoveRight();
93 if (input.JustPressed(Input::PAD_DOWN)) {
94 selection->MoveDown();
96 if (input.JustPressed(Input::PAD_LEFT)) {
97 selection->MoveLeft();
101 void SelectTarget::UpdateWorld(Uint32 deltaT) {
105 void SelectTarget::Render(SDL_Surface *screen) {
106 parent->Render(screen);
107 RenderCursors(screen);
110 void SelectTarget::RenderCursors(SDL_Surface *screen) {
111 vector<Vector<int> > &positions = selection->TargetsMonsters()
116 for (vector<Vector<int> >::size_type i(0), end(positions.size());
118 if (selection->IsSelected(i)) {
119 cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset);
123 flipFlop = !flipFlop;
124 cursorIcon->DrawTopRight(screen, positions[selection->Current()] + cursorOffset);