]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/SelectAttackType.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SelectAttackType.cpp
index 17260928af9455b7a6aaa40468a4cd52276e0da5..d0f1f392973cd19e28a10e8dc500c70088b82f3d 100644 (file)
@@ -1,10 +1,3 @@
-/*
- * SelectAttackType.cpp
- *
- *  Created on: Aug 7, 2012
- *      Author: holy
- */
-
 #include "SelectAttackType.h"
 
 #include "SelectIkari.h"
 #include "../BattleState.h"
 #include "../../app/Application.h"
 #include "../../app/Input.h"
-#include "../../geometry/operators.h"
+#include "../../common/Item.h"
+#include "../../math/Vector.h"
 
 #include <stdexcept>
 
 using app::Application;
 using app::Input;
-using geometry::Point;
-using geometry::Vector;
+using common::Item;
+using math::Vector;
 
 namespace battle {
 
-void SelectAttackType::EnterState(Application &c, SDL_Surface *screen) {
-       ctrl = &c;
+SelectAttackType::SelectAttackType(Battle *battle, BattleState *parent)
+: battle(battle)
+, parent(parent) {
+
 }
 
-void SelectAttackType::ExitState(Application &c, SDL_Surface *screen) {
-       ctrl = 0;
+
+void SelectAttackType::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
 }
 
-void SelectAttackType::ResumeState(Application &ctrl, SDL_Surface *screen) {
-       if (battle->ActiveHeroAttackChoice().Selection().HasSelected()) {
-               battle->ActiveHeroAttackChoice().SetType(battle->GetAttackTypeMenu().Selected());
+void SelectAttackType::OnExitState(SDL_Surface *screen) {
+
+}
+
+void SelectAttackType::OnResumeState(SDL_Surface *screen) {
+       if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
+               battle->ActiveHero().GetAttackChoice().SetType(parent->GetAttackTypeMenu().Selected());
                battle->NextHero();
        }
        if (battle->AttackSelectionDone()) {
                // pass through
-               ctrl.PopState();
+               Ctrl().PopState();
        }
 }
 
-void SelectAttackType::PauseState(Application &ctrl, SDL_Surface *screen) {
+void SelectAttackType::OnPauseState(SDL_Surface *screen) {
 
 }
 
 
-void SelectAttackType::Resize(int width, int height) {
-
+void SelectAttackType::OnResize(int width, int height) {
+       Vector<int> offset(parent->ScreenOffset());
+       Vector<int> position(
+                       (parent->Width() - parent->GetAttackTypeMenu().Width()) / 2,
+                       parent->Height() - parent->GetAttackTypeMenu().Height() - parent->GetAttackTypeMenu().Height() / 2);
+       menuOffset = offset + position;
 }
 
 
 void SelectAttackType::HandleEvents(const Input &input) {
        if (input.IsDown(Input::PAD_UP)) {
-               battle->GetAttackTypeMenu().Select(AttackChoice::MAGIC);
+               parent->GetAttackTypeMenu().Select(AttackChoice::MAGIC);
        } else if (input.IsDown(Input::PAD_RIGHT)) {
-               battle->GetAttackTypeMenu().Select(AttackChoice::DEFEND);
+               parent->GetAttackTypeMenu().Select(AttackChoice::DEFEND);
        } else if (input.IsDown(Input::PAD_DOWN)) {
-               battle->GetAttackTypeMenu().Select(AttackChoice::IKARI);
+               parent->GetAttackTypeMenu().Select(AttackChoice::IKARI);
        } else if (input.IsDown(Input::PAD_LEFT)) {
-               battle->GetAttackTypeMenu().Select(AttackChoice::ITEM);
+               parent->GetAttackTypeMenu().Select(AttackChoice::ITEM);
        } else {
-               battle->GetAttackTypeMenu().Select(AttackChoice::SWORD);
+               parent->GetAttackTypeMenu().Select(AttackChoice::SWORD);
        }
 
+       Hero &hero(battle->ActiveHero());
+       AttackChoice &ac(hero.GetAttackChoice());
        if (input.JustPressed(Input::ACTION_A)) {
-               switch (battle->GetAttackTypeMenu().Selected()) {
+               switch (parent->GetAttackTypeMenu().Selected()) {
                        case AttackChoice::SWORD:
-                               // TODO: detect single/multiple/all attack mode
-                               battle->ActiveHeroAttackChoice().Selection().SetSingle();
-                               battle->ActiveHeroAttackChoice().Selection().Reset();
-                               ctrl->PushState(new SelectTarget(battle, this, &battle->ActiveHeroAttackChoice().Selection(), battle->Res().weaponTargetCursor));
+                               if (hero.HasWeapon()) {
+                                       if (hero.Weapon()->GetTargetingMode().TargetsAll()) {
+                                               ac.SetType(AttackChoice::SWORD);
+                                               battle->NextHero();
+                                               break;
+                                       } else {
+                                               ac.Selection().ReadMode(hero.Weapon()->GetTargetingMode());
+                                       }
+                               } else {
+                                       ac.Selection().SetSingle();
+                               }
+                               ac.Selection().Reset();
+                               Ctrl().PushState(new SelectTarget(battle, this, &ac.Selection(), parent->Res().weaponTargetCursor));
                                break;
                        case AttackChoice::MAGIC:
                                if (battle->ActiveHero().CanUseMagic()) {
-                                       ctrl->PushState(new SelectSpell(battle, this));
+                                       Ctrl().PushState(new SelectSpell(battle, this));
                                }
                                break;
                        case AttackChoice::DEFEND:
-                               battle->ActiveHeroAttackChoice().SetType(AttackChoice::DEFEND);
+                               ac.SetType(AttackChoice::DEFEND);
                                battle->NextHero();
                                break;
                        case AttackChoice::IKARI:
-                               ctrl->PushState(new SelectIkari(battle, this));
+                               Ctrl().PushState(new SelectIkari(battle, this));
                                break;
                        case AttackChoice::ITEM:
-                               ctrl->PushState(new SelectItem(battle, this));
+                               Ctrl().PushState(new SelectItem(battle, this));
                                break;
                        default:
                                throw std::logic_error("selected invalid attack type");
                }
        } else if (input.JustPressed(Input::ACTION_B)) {
-               battle->ActiveHeroAttackChoice().Reset();
+               ac.Reset();
                battle->PreviousHero();
                if (battle->BeforeFirstHero()) {
-                       ctrl->ChangeState(new SelectMoveAction(battle));
+                       Ctrl().ChangeState(new SelectMoveAction(battle, parent));
                } else {
-                       battle->ActiveHeroAttackChoice().Reset();
+                       battle->ActiveHero().GetAttackChoice().Reset();
                }
        }
 
        if (battle->AttackSelectionDone()) {
-               // TODO: switch to battle animation instead
-               ctrl->PopState();
+               Ctrl().PopState();
        }
 }
 
-void SelectAttackType::UpdateWorld(float deltaT) {
+void SelectAttackType::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void SelectAttackType::Render(SDL_Surface *screen) {
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
-       battle->RenderBackground(screen, offset);
-       battle->RenderMonsters(screen, offset);
-       battle->RenderHeroTags(screen, offset);
-       RenderMenu(screen, offset);
-}
-
-void SelectAttackType::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
-       Point<int> position(
-                       (battle->BackgroundWidth() - battle->GetAttackTypeMenu().Width()) / 2,
-                       battle->BackgroundHeight() - battle->GetAttackTypeMenu().Height() - battle->GetAttackTypeMenu().Height() / 2);
-       battle->GetAttackTypeMenu().Render(screen, position + offset);
+       parent->RenderBackground(screen);
+       parent->RenderMonsters(screen);
+       parent->RenderHeroTags(screen);
+       RenderMenu(screen);
+}
+
+void SelectAttackType::RenderMenu(SDL_Surface *screen) {
+       parent->GetAttackTypeMenu().Render(screen, menuOffset);
+}
+
+
+const Resources &SelectAttackType::Res() const {
+       return parent->Res();
+}
+
+graphics::Menu<const Item *> &SelectAttackType::ItemMenu() {
+       return parent->ItemMenu();
+}
+
+const graphics::Menu<const Item *> &SelectAttackType::ItemMenu() const {
+       return parent->ItemMenu();
+}
+
+const HeroTag &SelectAttackType::HeroTagAt(int index) const {
+       return parent->HeroTagAt(index);
+}
+const Vector<int> &SelectAttackType::HeroTagPositionAt(int index) const {
+       return parent->HeroTagPositionAt(index);
+}
+
+const Vector<int> &SelectAttackType::ScreenOffset() const {
+       return parent->ScreenOffset();
+}
+
+int SelectAttackType::Width() const {
+       return parent->Width();
+}
+
+int SelectAttackType::Height() const {
+       return parent->Height();
 }
 
 }