Streamline trap code

pull/1/head
Allofich 9 years ago
parent 35a23c3b49
commit cb621939fd

@ -17,7 +17,7 @@ MWWorld::Action::Action (bool keepSound, const Ptr& target) : mKeepSound (keepSo
MWWorld::Action::~Action() {} MWWorld::Action::~Action() {}
void MWWorld::Action::execute (const Ptr& actor, float distanceToObject) void MWWorld::Action::execute (const Ptr& actor, float distanceToFacedObject)
{ {
if(!mSoundId.empty()) if(!mSoundId.empty())
{ {
@ -41,8 +41,8 @@ void MWWorld::Action::execute (const Ptr& actor, float distanceToObject)
); );
} }
} }
if (distanceToObject != 0) if (mTarget.getCellRef().getTrap() != "")
executeImp(actor, distanceToObject); executeImp(actor, distanceToFacedObject);
else else
executeImp(actor); executeImp(actor);
} }

@ -37,7 +37,7 @@ namespace MWWorld
virtual bool isNullAction() { return false; } virtual bool isNullAction() { return false; }
///< Is running this action a no-op? (default false) ///< Is running this action a no-op? (default false)
void execute (const Ptr& actor, float distanceToObject = 0); void execute (const Ptr& actor, float distanceToObject = -1);
void setSound (const std::string& id); void setSound (const std::string& id);
void setSoundOffset(float offset); void setSoundOffset(float offset);

@ -7,36 +7,28 @@
namespace MWWorld namespace MWWorld
{ {
// actor activated object without telekinesis, so trap will hit
void ActionTrap::executeImp(const Ptr &actor)
{
osg::Vec3f actorPosition(actor.getRefData().getPosition().asVec3());
MWMechanics::CastSpell cast(mTrapSource, actor);
cast.mHitPosition = actorPosition;
cast.cast(mSpellId);
mTrapSource.getCellRef().setTrap("");
}
// actor activated object with telekinesis, so trap may or may not hit
void ActionTrap::executeImp(const Ptr &actor, float distance) void ActionTrap::executeImp(const Ptr &actor, float distance)
{ {
osg::Vec3f actorPosition(actor.getRefData().getPosition().asVec3());
osg::Vec3f trapPosition(mTrapSource.getRefData().getPosition().asVec3()); osg::Vec3f trapPosition(mTrapSource.getRefData().getPosition().asVec3());
float activationDistance = MWBase::Environment::get().getWorld()->getMaxActivationDistance(); float trapRange = MWBase::Environment::get().getWorld()->getMaxActivationDistance();
// Note: can't just detonate the trap at the trapped object's location and use the blast // Note: can't just detonate the trap at the trapped object's location and use the blast
// radius, because for most trap spells this is 1 foot, much less than the activation distance. // radius, because for most trap spells this is 1 foot, much less than the activation distance.
// Using activation distance as the trap range. // Using activation distance as the trap range.
if (distance < activationDistance) // actor activated object within range of trap if (distance > trapRange) // actor activated object outside range of trap
executeImp(actor);
else // actor activated object outside range of trap
{ {
MWMechanics::CastSpell cast(mTrapSource, mTrapSource); MWMechanics::CastSpell cast(mTrapSource, mTrapSource);
cast.mHitPosition = trapPosition; cast.mHitPosition = trapPosition;
cast.cast(mSpellId);
}
else // actor activated object within range of trap
{
MWMechanics::CastSpell cast(mTrapSource, actor);
cast.mHitPosition = actorPosition;
cast.cast(mSpellId); cast.cast(mSpellId);
mTrapSource.getCellRef().setTrap("");
} }
mTrapSource.getCellRef().setTrap("");
} }
} }

@ -13,11 +13,7 @@ namespace MWWorld
std::string mSpellId; std::string mSpellId;
MWWorld::Ptr mTrapSource; MWWorld::Ptr mTrapSource;
/// Activating trapped object without telekinesis active or within trap range virtual void executeImp (const Ptr& actor, float distanceToObject = -1);
virtual void executeImp (const Ptr& actor);
/// Activating trapped object with telekinesis active
virtual void executeImp (const Ptr& actor, float distanceToObject);
public: public:
@ -25,7 +21,7 @@ namespace MWWorld
/// @param actor Actor that activated the trap /// @param actor Actor that activated the trap
/// @param trapSource /// @param trapSource
ActionTrap (const Ptr& actor, const std::string& spellId, const Ptr& trapSource) ActionTrap (const Ptr& actor, const std::string& spellId, const Ptr& trapSource)
: Action(false, actor), mSpellId(spellId), mTrapSource(trapSource) {} : Action(false, trapSource), mSpellId(spellId), mTrapSource(trapSource) {}
}; };
} }

@ -3205,12 +3205,7 @@ namespace MWWorld
if (object.getRefData().activate()) if (object.getRefData().activate())
{ {
boost::shared_ptr<MWWorld::Action> action = (object.getClass().activate(object, actor)); boost::shared_ptr<MWWorld::Action> action = (object.getClass().activate(object, actor));
// If the player is opening a trap with telekinesis on, use the raycast-derived distance to check if the trap should hit action->execute (actor, mDistanceToFacedObject);
if (object.getCellRef().getTrap() != "" && actor == getPlayerPtr() && mPlayer->getPlayer().getClass().getCreatureStats(mPlayer->getPlayer()).getMagicEffects()
.get(ESM::MagicEffect::Telekinesis).getMagnitude() > 0)
action->execute (actor, mDistanceToFacedObject);
else // Otherwise just activate, and if it's trapped it will always hit
action->execute (actor);
} }
} }

Loading…
Cancel
Save