]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/SelectSpell.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SelectSpell.cpp
index 66b55f16d9dfe785a6c60ae5d54af51aaa7d0ed7..2b22e70fcd856e124fac407f047bd682b8aa6ed8 100644 (file)
-/*
- * SelectSpell.cpp
- *
- *  Created on: Aug 8, 2012
- *      Author: holy
- */
-
 #include "SelectSpell.h"
 
 #include "SelectAttackType.h"
 #include "SelectMoveAction.h"
+#include "SelectTarget.h"
 #include "../BattleState.h"
 #include "../../app/Application.h"
 #include "../../app/Input.h"
-#include "../../geometry/operators.h"
-#include "../../geometry/Point.h"
+#include "../../common/Spell.h"
 #include "../../graphics/Frame.h"
+#include "../../math/Vector.h"
 
 using app::Application;
 using app::Input;
-using geometry::Point;
-using geometry::Vector;
+using common::Spell;
+using math::Vector;
 using graphics::Frame;
 
 namespace battle {
 
-void SelectSpell::EnterState(Application &c, SDL_Surface *screen) {
-       ctrl = &c;
+SelectSpell::SelectSpell(Battle *battle, SelectAttackType *parent)
+: battle(battle)
+, parent(parent) {
+
 }
 
-void SelectSpell::ExitState(Application &c, SDL_Surface *screen) {
-       ctrl = 0;
+
+void SelectSpell::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
 }
 
-void SelectSpell::ResumeState(Application &ctrl, SDL_Surface *screen) {
+void SelectSpell::OnExitState(SDL_Surface *screen) {
+
+}
 
+void SelectSpell::OnResumeState(SDL_Surface *screen) {
+       if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
+               battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::MAGIC);
+               battle->ActiveHero().GetAttackChoice().SetSpell(battle->ActiveHero().SpellMenu().Selected());
+               Ctrl().PopState();
+       }
 }
 
-void SelectSpell::PauseState(Application &ctrl, SDL_Surface *screen) {
+void SelectSpell::OnPauseState(SDL_Surface *screen) {
 
 }
 
 
-void SelectSpell::Resize(int width, int height) {
+void SelectSpell::OnResize(int width, int height) {
+       const Vector<int> offset = parent->ScreenOffset();
+
+       const Resources &res = parent->Res();
+       const Frame &frame = *res.selectFrame;
+
+       framePosition = offset + frame.BorderSize();
+       frameSize = Vector<int>(
+                       parent->Width() - 2 * frame.BorderWidth(),
+                       res.normalFont->CharHeight() * 13);
 
+       headlinePosition = offset + Vector<int>(
+                       2 * frame.BorderWidth() + res.normalFont->CharWidth(),
+                       2 * frame.BorderHeight());
+
+       menuPosition = offset + Vector<int>(
+                       2 * frame.BorderWidth() + res.normalFont->CharWidth(),
+                       2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight());
 }
 
 
-void SelectSpell::HandleInput(const Input &input) {
+void SelectSpell::HandleEvents(const Input &input) {
        if (input.JustPressed(Input::ACTION_A)) {
-               // TODO: switch to target select
-               battle->NextHero();
-               ctrl->PopState();
+               if (battle->ActiveHero().SpellMenu().SelectedIsEnabled()) {
+                       AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
+                       const Spell *spell(battle->ActiveHero().SpellMenu().Selected());
+                       ac.Selection().Reset();
+                       if (spell->GetTargetingMode().TargetsAlly()) {
+                               ac.Selection().SelectHeroes();
+                       } else {
+                               ac.Selection().SelectMonsters();
+                       }
+                       if (spell->GetTargetingMode().TargetsAll()) {
+                               ac.SetType(AttackChoice::MAGIC);
+                               ac.SetSpell(spell);
+                               battle->NextHero();
+                               Ctrl().PopState();
+                       } else {
+                               if (spell->GetTargetingMode().TargetsSingle()) {
+                                       ac.Selection().SetSingle();
+                               } else {
+                                       ac.Selection().SetMultiple();
+                               }
+                               Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), parent->Res().magicTargetCursor));
+                       }
+               }
        }
        if (input.JustPressed(Input::ACTION_B)) {
-               ctrl->PopState(); // return control to parent
+               Ctrl().PopState(); // return control to parent
+       }
+       if (input.JustPressed(Input::PAD_UP)) {
+               battle->ActiveHero().SpellMenu().PreviousRow();
+       }
+       if (input.JustPressed(Input::PAD_RIGHT)) {
+               battle->ActiveHero().SpellMenu().NextItem();
+       }
+       if (input.JustPressed(Input::PAD_DOWN)) {
+               battle->ActiveHero().SpellMenu().NextRow();
+       }
+       if (input.JustPressed(Input::PAD_LEFT)) {
+               battle->ActiveHero().SpellMenu().PreviousItem();
        }
 }
 
-void SelectSpell::UpdateWorld(float deltaT) {
+void SelectSpell::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void SelectSpell::Render(SDL_Surface *screen) {
        parent->Render(screen);
-       const Frame &frame(battle->GetSelectFrame());
-       Point<int> position(frame.BorderWidth(), frame.BorderHeight());
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
-       int width(battle->BackgroundWidth() - 2 * frame.BorderWidth());
-       // TODO: replace with font height
-       int height(frame.BorderHeight() * 13);
-       frame.Draw(screen, position + offset, width, height);
+       RenderFrame(screen);
+       RenderHeadline(screen);
+       RenderMenu(screen);
+}
+
+void SelectSpell::RenderFrame(SDL_Surface *screen) {
+       const Frame &frame = *parent->Res().selectFrame;
+       frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
+}
+
+void SelectSpell::RenderHeadline(SDL_Surface *screen) {
+       const Resources &res = parent->Res();
+       res.normalFont->DrawString(res.spellMenuHeadline, screen, headlinePosition);
+}
+
+void SelectSpell::RenderMenu(SDL_Surface *screen) {
+       battle->ActiveHero().SpellMenu().Draw(screen, menuPosition);
 }
 
 }