Issue #351: Refactoring Action class

This commit is contained in:
Marc Zinnschlag 2012-07-27 12:00:10 +02:00
parent 281e15f58e
commit 78fe6fdce5
25 changed files with 71 additions and 56 deletions

View file

@ -40,7 +40,7 @@ namespace MWClass
physics.insertObjectPhysics(ptr, model); physics.insertObjectPhysics(ptr, model);
} }
} }
std::string Door::getModel(const MWWorld::Ptr &ptr) const std::string Door::getModel(const MWWorld::Ptr &ptr) const
{ {
MWWorld::LiveCellRef<ESM::Door> *ref = MWWorld::LiveCellRef<ESM::Door> *ref =
@ -97,18 +97,18 @@ namespace MWClass
if (ref->ref.teleport) if (ref->ref.teleport)
{ {
// teleport door // teleport door
/// \todo remove this if clause once ActionTeleport can also support other actors
if (MWBase::Environment::get().getWorld()->getPlayer().getPlayer()==actor) if (MWBase::Environment::get().getWorld()->getPlayer().getPlayer()==actor)
{ {
// the player is using the door // the player is using the door
// The reason this is not 3D is that it would get interrupted when you teleport // The reason this is not 3D is that it would get interrupted when you teleport
MWBase::Environment::get().getSoundManager()->playSound(openSound, 1.0, 1.0); MWBase::Environment::get().getSoundManager()->playSound(openSound, 1.0, 1.0);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTeleportPlayer (ref->ref.destCell, ref->ref.doorDest)); new MWWorld::ActionTeleport (ref->ref.destCell, ref->ref.doorDest));
} }
else else
{ {
// another NPC or a creature is using the door // another NPC or a creature is using the door
// TODO return action for teleporting other NPC/creature
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }
} }

View file

@ -40,7 +40,7 @@ namespace MWClass
physics.insertObjectPhysics(ptr, model); physics.insertObjectPhysics(ptr, model);
} }
} }
std::string Potion::getModel(const MWWorld::Ptr &ptr) const std::string Potion::getModel(const MWWorld::Ptr &ptr) const
{ {
MWWorld::LiveCellRef<ESM::Potion> *ref = MWWorld::LiveCellRef<ESM::Potion> *ref =
@ -158,7 +158,7 @@ namespace MWClass
MWWorld::Ptr actor = MWBase::Environment::get().getWorld()->getPlayer().getPlayer(); MWWorld::Ptr actor = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionApply (actor, ref->base->mId, actor)); new MWWorld::ActionApply (actor, ref->base->mId));
} }
MWWorld::Ptr MWWorld::Ptr
@ -170,4 +170,3 @@ namespace MWClass
return MWWorld::Ptr(&cell.potions.insert(*ref), &cell); return MWWorld::Ptr(&cell.potions.insert(*ref), &cell);
} }
} }

View file

@ -3,9 +3,11 @@
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwinput/inputmanager.hpp" #include "../mwinput/inputmanager.hpp"
#include "../mwsound/soundmanager.hpp" #include "../mwsound/soundmanager.hpp"
#include "../mwworld/actiontake.hpp" #include "../mwworld/actiontake.hpp"
#include "../mwworld/player.hpp"
#include "formatting.hpp" #include "formatting.hpp"
#include "window_manager.hpp" #include "window_manager.hpp"
@ -99,7 +101,7 @@ void BookWindow::onTakeButtonClicked (MyGUI::Widget* _sender)
MWBase::Environment::get().getSoundManager()->playSound ("Item Book Up", 1.0, 1.0, MWSound::Play_NoTrack); MWBase::Environment::get().getSoundManager()->playSound ("Item Book Up", 1.0, 1.0, MWSound::Play_NoTrack);
MWWorld::ActionTake take(mBook); MWWorld::ActionTake take(mBook);
take.execute(); take.execute (MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
mWindowManager.removeGuiMode(GM_Book); mWindowManager.removeGuiMode(GM_Book);
} }

View file

@ -175,7 +175,7 @@ namespace MWGui
boost::shared_ptr<MWWorld::Action> action = MWWorld::Class::get(ptr).use(ptr); boost::shared_ptr<MWWorld::Action> action = MWWorld::Class::get(ptr).use(ptr);
action->execute(); action->execute (MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
// this is necessary for books/scrolls: if they are already in the player's inventory, // this is necessary for books/scrolls: if they are already in the player's inventory,
// the "Take" button should not be visible. // the "Take" button should not be visible.

View file

@ -1,8 +1,10 @@
#include "scrollwindow.hpp" #include "scrollwindow.hpp"
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwinput/inputmanager.hpp" #include "../mwinput/inputmanager.hpp"
#include "../mwworld/actiontake.hpp" #include "../mwworld/actiontake.hpp"
#include "../mwworld/player.hpp"
#include "../mwsound/soundmanager.hpp" #include "../mwsound/soundmanager.hpp"
#include "formatting.hpp" #include "formatting.hpp"
@ -63,7 +65,7 @@ void ScrollWindow::onTakeButtonClicked (MyGUI::Widget* _sender)
MWBase::Environment::get().getSoundManager()->playSound ("Item Book Up", 1.0, 1.0, MWSound::Play_NoTrack); MWBase::Environment::get().getSoundManager()->playSound ("Item Book Up", 1.0, 1.0, MWSound::Play_NoTrack);
MWWorld::ActionTake take(mScroll); MWWorld::ActionTake take(mScroll);
take.execute(); take.execute (MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
mWindowManager.removeGuiMode(GM_Scroll); mWindowManager.removeGuiMode(GM_Scroll);
} }

View file

@ -10,6 +10,7 @@
#include "../mwbase/world.hpp" #include "../mwbase/world.hpp"
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/player.hpp"
#include "../mwgui/window_manager.hpp" #include "../mwgui/window_manager.hpp"
@ -236,7 +237,7 @@ namespace MWScript
if (!mAction.get()) if (!mAction.get())
throw std::runtime_error ("activation failed, because no action to perform"); throw std::runtime_error ("activation failed, because no action to perform");
mAction->execute(); mAction->execute (MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
mActivationHandled = true; mActivationHandled = true;
} }

View file

@ -0,0 +1,11 @@
#include "action.hpp"
MWWorld::Action::Action() {}
MWWorld::Action::~Action() {}
void MWWorld::Action::execute (const Ptr& actor)
{
executeImp (actor);
}

View file

@ -3,6 +3,8 @@
namespace MWWorld namespace MWWorld
{ {
class Ptr;
/// \brief Abstract base for actions /// \brief Abstract base for actions
class Action class Action
{ {
@ -10,13 +12,15 @@ namespace MWWorld
Action (const Action& action); Action (const Action& action);
Action& operator= (const Action& action); Action& operator= (const Action& action);
virtual void executeImp (const Ptr& actor) = 0;
public: public:
Action() {} Action();
virtual ~Action() {} virtual ~Action();
virtual void execute() = 0; void execute (const Ptr& actor);
}; };
} }

View file

@ -5,7 +5,7 @@
namespace MWWorld namespace MWWorld
{ {
void ActionAlchemy::execute() void ActionAlchemy::executeImp (const Ptr& actor)
{ {
MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Alchemy); MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Alchemy);
} }

View file

@ -7,8 +7,7 @@ namespace MWWorld
{ {
class ActionAlchemy : public Action class ActionAlchemy : public Action
{ {
public: virtual void executeImp (const Ptr& actor);
virtual void execute ();
}; };
} }

View file

@ -5,24 +5,24 @@
namespace MWWorld namespace MWWorld
{ {
ActionApply::ActionApply (const Ptr& target, const std::string& id, const Ptr& actor) ActionApply::ActionApply (const Ptr& target, const std::string& id)
: mTarget (target), mId (id), mActor (actor) : mTarget (target), mId (id)
{} {}
void ActionApply::execute() void ActionApply::executeImp (const Ptr& actor)
{ {
MWWorld::Class::get (mTarget).apply (mTarget, mId, mActor); MWWorld::Class::get (mTarget).apply (mTarget, mId, actor);
} }
ActionApplyWithSkill::ActionApplyWithSkill (const Ptr& target, const std::string& id, ActionApplyWithSkill::ActionApplyWithSkill (const Ptr& target, const std::string& id,
const Ptr& actor, int skillIndex, int usageType) int skillIndex, int usageType)
: mTarget (target), mId (id), mActor (actor), mSkillIndex (skillIndex), mUsageType (usageType) : mTarget (target), mId (id), mSkillIndex (skillIndex), mUsageType (usageType)
{} {}
void ActionApplyWithSkill::execute() void ActionApplyWithSkill::executeImp (const Ptr& actor)
{ {
if (MWWorld::Class::get (mTarget).apply (mTarget, mId, mActor) && mUsageType!=-1) if (MWWorld::Class::get (mTarget).apply (mTarget, mId, actor) && mUsageType!=-1)
MWWorld::Class::get (mTarget).skillUsageSucceeded (mActor, mSkillIndex, mUsageType); MWWorld::Class::get (mTarget).skillUsageSucceeded (actor, mSkillIndex, mUsageType);
} }
} }

View file

@ -13,29 +13,27 @@ namespace MWWorld
{ {
Ptr mTarget; Ptr mTarget;
std::string mId; std::string mId;
Ptr mActor;
virtual void executeImp (const Ptr& actor);
public: public:
ActionApply (const Ptr& target, const std::string& id, const Ptr& actor); ActionApply (const Ptr& target, const std::string& id);
virtual void execute();
}; };
class ActionApplyWithSkill : public Action class ActionApplyWithSkill : public Action
{ {
Ptr mTarget; Ptr mTarget;
std::string mId; std::string mId;
Ptr mActor;
int mSkillIndex; int mSkillIndex;
int mUsageType; int mUsageType;
virtual void executeImp (const Ptr& actor);
public: public:
ActionApplyWithSkill (const Ptr& target, const std::string& id, const Ptr& actor, ActionApplyWithSkill (const Ptr& target, const std::string& id,
int skillIndex, int usageType); int skillIndex, int usageType);
virtual void execute();
}; };
} }

View file

@ -13,7 +13,7 @@ namespace MWWorld
{ {
} }
void ActionEquip::execute () void ActionEquip::executeImp (const Ptr& actor)
{ {
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer(); MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
MWWorld::InventoryStore& invStore = MWWorld::Class::get(player).getInventoryStore(player); MWWorld::InventoryStore& invStore = MWWorld::Class::get(player).getInventoryStore(player);

View file

@ -10,11 +10,11 @@ namespace MWWorld
{ {
Ptr mObject; Ptr mObject;
virtual void executeImp (const Ptr& actor);
public: public:
/// @param item to equip /// @param item to equip
ActionEquip (const Ptr& object); ActionEquip (const Ptr& object);
virtual void execute ();
}; };
} }

View file

@ -15,7 +15,7 @@ namespace MWWorld
mContainer = container; mContainer = container;
} }
void ActionOpen::execute () void ActionOpen::executeImp (const MWWorld::Ptr& actor)
{ {
if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory)) if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))
return; return;

View file

@ -12,10 +12,11 @@ namespace MWWorld
{ {
Ptr mContainer; Ptr mContainer;
virtual void executeImp (const MWWorld::Ptr& actor);
public: public:
ActionOpen (const Ptr& container); ActionOpen (const Ptr& container);
///< \param The Container the Player has activated. ///< \param The Container the Player has activated.
virtual void execute ();
}; };
} }

View file

@ -11,7 +11,7 @@ namespace MWWorld
{ {
} }
void ActionRead::execute () void ActionRead::executeImp (const MWWorld::Ptr& actor)
{ {
LiveCellRef<ESM::Book> *ref = mObject.get<ESM::Book>(); LiveCellRef<ESM::Book> *ref = mObject.get<ESM::Book>();

View file

@ -10,11 +10,11 @@ namespace MWWorld
{ {
Ptr mObject; // book or scroll to read Ptr mObject; // book or scroll to read
virtual void executeImp (const MWWorld::Ptr& actor);
public: public:
/// @param book or scroll to read /// @param book or scroll to read
ActionRead (const Ptr& object); ActionRead (const Ptr& object);
virtual void execute ();
}; };
} }

View file

@ -13,7 +13,7 @@ namespace MWWorld
{ {
ActionTake::ActionTake (const MWWorld::Ptr& object) : mObject (object) {} ActionTake::ActionTake (const MWWorld::Ptr& object) : mObject (object) {}
void ActionTake::execute() void ActionTake::executeImp (const Ptr& actor)
{ {
if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory)) if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))
return; return;

View file

@ -10,11 +10,11 @@ namespace MWWorld
{ {
MWWorld::Ptr mObject; MWWorld::Ptr mObject;
virtual void executeImp (const Ptr& actor);
public: public:
ActionTake (const MWWorld::Ptr& object); ActionTake (const MWWorld::Ptr& object);
virtual void execute();
}; };
} }

View file

@ -9,7 +9,7 @@ namespace MWWorld
{ {
ActionTalk::ActionTalk (const Ptr& actor) : mActor (actor) {} ActionTalk::ActionTalk (const Ptr& actor) : mActor (actor) {}
void ActionTalk::execute() void ActionTalk::executeImp (const Ptr& actor)
{ {
MWBase::Environment::get().getDialogueManager()->startDialogue (mActor); MWBase::Environment::get().getDialogueManager()->startDialogue (mActor);
} }

View file

@ -10,12 +10,12 @@ namespace MWWorld
{ {
Ptr mActor; Ptr mActor;
virtual void executeImp (const Ptr& actor);
public: public:
ActionTalk (const Ptr& actor); ActionTalk (const Ptr& actor);
///< \param actor The actor the player is talking to ///< \param actor The actor the player is talking to
virtual void execute();
}; };
} }

View file

@ -6,12 +6,12 @@
namespace MWWorld namespace MWWorld
{ {
ActionTeleportPlayer::ActionTeleportPlayer (const std::string& cellName, ActionTeleport::ActionTeleport (const std::string& cellName,
const ESM::Position& position) const ESM::Position& position)
: mCellName (cellName), mPosition (position) : mCellName (cellName), mPosition (position)
{} {}
void ActionTeleportPlayer::execute() void ActionTeleport::executeImp (const Ptr& actor)
{ {
if (mCellName.empty()) if (mCellName.empty())
MWBase::Environment::get().getWorld()->changeToExteriorCell (mPosition); MWBase::Environment::get().getWorld()->changeToExteriorCell (mPosition);

View file

@ -9,17 +9,17 @@
namespace MWWorld namespace MWWorld
{ {
class ActionTeleportPlayer : public Action class ActionTeleport : public Action
{ {
std::string mCellName; std::string mCellName;
ESM::Position mPosition; ESM::Position mPosition;
virtual void executeImp (const Ptr& actor);
public: public:
ActionTeleportPlayer (const std::string& cellName, const ESM::Position& position); ActionTeleport (const std::string& cellName, const ESM::Position& position);
///< If cellName is empty, an exterior cell is asumed. ///< If cellName is empty, an exterior cell is asumed.
virtual void execute();
}; };
} }

View file

@ -8,9 +8,7 @@ namespace MWWorld
/// \brief Action: do nothing /// \brief Action: do nothing
class NullAction : public Action class NullAction : public Action
{ {
public: virtual void executeImp (const Ptr& actor) {}
virtual void execute() {}
}; };
} }