]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/SelectItem.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SelectItem.cpp
index bd09da664e204891b6a8b3aeeaf86b05e3b174db..e4ccc14527d95ca4ea5bbd2665b4284195500c1c 100644 (file)
@@ -1,10 +1,3 @@
-/*
- * SelectItem.cpp
- *
- *  Created on: Aug 9, 2012
- *      Author: holy
- */
-
 #include "SelectItem.h"
 
 #include "SelectAttackType.h"
 #include "../../app/Application.h"
 #include "../../app/Input.h"
 #include "../../common/Item.h"
-#include "../../geometry/Point.h"
-#include "../../geometry/operators.h"
 #include "../../graphics/Frame.h"
+#include "../../math/Vector.h"
 
 using app::Application;
 using app::Input;
 using common::Item;
-using geometry::Point;
-using geometry::Vector;
+using math::Vector;
 using graphics::Frame;
 
 namespace battle {
 
-void SelectItem::EnterState(Application &c, SDL_Surface *screen) {
-       ctrl = &c;
+SelectItem::SelectItem(Battle *battle, SelectAttackType *parent)
+: battle(battle)
+, parent(parent) {
+
 }
 
-void SelectItem::ExitState(Application &c, SDL_Surface *screen) {
-       ctrl = 0;
+
+void SelectItem::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
 }
 
-void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) {
+void SelectItem::OnExitState(SDL_Surface *screen) {
+
+}
+
+void SelectItem::OnResumeState(SDL_Surface *screen) {
        if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
                battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::ITEM);
-               battle->ActiveHero().GetAttackChoice().SetItem(battle->ItemMenu().Selected());
-               ctrl.PopState();
+               battle->ActiveHero().GetAttackChoice().SetItem(parent->ItemMenu().Selected());
+               Ctrl().PopState();
        }
 }
 
-void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
+void SelectItem::OnPauseState(SDL_Surface *screen) {
 
 }
 
 
-void SelectItem::Resize(int width, int height) {
+void SelectItem::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 SelectItem::HandleEvents(const Input &input) {
        if (input.JustPressed(Input::ACTION_A)) {
-               if (battle->ItemMenu().SelectedIsEnabled()) {
+               if (parent->ItemMenu().SelectedIsEnabled()) {
                        AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
-                       const Item *item(battle->ItemMenu().Selected());
+                       const Item *item(parent->ItemMenu().Selected());
                        ac.Selection().Reset();
                        if (item->GetTargetingMode().TargetsAlly()) {
                                ac.Selection().SelectHeroes();
                        } else {
-                               ac.Selection().SelectEnemies();
+                               ac.Selection().SelectMonsters();
                        }
                        if (item->GetTargetingMode().TargetsAll()) {
                                ac.SetType(AttackChoice::ITEM);
-                               // TODO: remove item from inventory
                                ac.SetItem(item);
                                battle->NextHero();
-                               ctrl->PopState();
+                               Ctrl().PopState();
                        } else {
                                if (item->GetTargetingMode().TargetsSingle()) {
                                        ac.Selection().SetSingle();
                                } else {
                                        ac.Selection().SetMultiple();
                                }
-                               ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().itemTargetCursor));
+                               Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), parent->Res().itemTargetCursor));
                        }
                }
        }
        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->ItemMenu().PreviousRow();
+               parent->ItemMenu().PreviousRow();
        }
        if (input.JustPressed(Input::PAD_RIGHT)) {
-               battle->ItemMenu().NextItem();
+               parent->ItemMenu().NextItem();
        }
        if (input.JustPressed(Input::PAD_DOWN)) {
-               battle->ItemMenu().NextRow();
+               parent->ItemMenu().NextRow();
        }
        if (input.JustPressed(Input::PAD_LEFT)) {
-               battle->ItemMenu().PreviousItem();
+               parent->ItemMenu().PreviousItem();
        }
 }
 
-void SelectItem::UpdateWorld(float deltaT) {
+void SelectItem::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void SelectItem::Render(SDL_Surface *screen) {
        parent->Render(screen);
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
-       RenderFrame(screen, offset);
-       RenderHeadline(screen, offset);
-       RenderMenu(screen, offset);
+       RenderFrame(screen);
+       RenderHeadline(screen);
+       RenderMenu(screen);
 }
 
-void SelectItem::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
-       const Frame *frame(battle->Res().selectFrame);
-       Point<int> position(frame->BorderWidth(), frame->BorderHeight());
-       int width(battle->Width() - 2 * frame->BorderWidth());
-       int height(battle->Res().normalFont->CharHeight() * 13);
-       frame->Draw(screen, position + offset, width, height);
+void SelectItem::RenderFrame(SDL_Surface *screen) {
+       const Frame &frame = *parent->Res().selectFrame;
+       frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
 }
 
-void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
-       const Resources &res(battle->Res());
-       Point<int> position(
-                       2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
-                       2 * res.selectFrame->BorderHeight());
-       res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
+void SelectItem::RenderHeadline(SDL_Surface *screen) {
+       const Resources &res = parent->Res();
+       res.normalFont->DrawString(res.itemMenuHeadline, screen, headlinePosition);
 }
 
-void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
-       const Resources &res(battle->Res());
-       Point<int> position(
-                       2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
-                       2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
-       battle->ItemMenu().Draw(screen, position + offset);
+void SelectItem::RenderMenu(SDL_Surface *screen) {
+       parent->ItemMenu().Draw(screen, menuPosition);
 }
 
 }