123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
- // Copyright (C) 2015 Faust Logic, Inc.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- #include <typeinfo>
- #include "afx/arcaneFX.h"
- #include "afx/afxEffectDefs.h"
- #include "afx/afxEffectWrapper.h"
- #include "afx/afxChoreographer.h"
- #include "afx/ce/afxCollisionEvent.h"
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- // afxEA_CollisionEvent
- class afxEA_CollisionEvent : public afxEffectWrapper, ShapeBase::CollisionEventCallback
- {
- typedef afxEffectWrapper Parent;
- afxCollisionEventData* script_data;
- ShapeBase* shape;
- U32 trigger_mask;
- bool triggered;
- void do_runtime_substitutions();
- void set_shape(ShapeBase*);
- public:
- /*C*/ afxEA_CollisionEvent();
- /*D*/ ~afxEA_CollisionEvent();
- virtual void ea_set_datablock(SimDataBlock*);
- virtual bool ea_start();
- virtual bool ea_update(F32 dt);
- virtual void ea_finish(bool was_stopped);
- virtual void collisionNotify(SceneObject* obj0, SceneObject* obj1, const VectorF& vel);
- virtual void onDeleteNotify(SimObject*);
- };
- //~~~~~~~~~~~~~~~~~~~~//
- afxEA_CollisionEvent::afxEA_CollisionEvent()
- {
- script_data = 0;
- shape = 0;
- trigger_mask = 0;
- triggered = false;
- }
- afxEA_CollisionEvent::~afxEA_CollisionEvent()
- {
- if (shape)
- clearNotify(shape);
- if (script_data && script_data->isTempClone())
- delete script_data;
- script_data = 0;
- }
- void afxEA_CollisionEvent::ea_set_datablock(SimDataBlock* db)
- {
- script_data = dynamic_cast<afxCollisionEventData*>(db);
- }
- bool afxEA_CollisionEvent::ea_start()
- {
- if (!script_data)
- {
- Con::errorf("afxEA_CollisionEvent::ea_start() -- missing or incompatible datablock.");
- return false;
- }
- do_runtime_substitutions();
- if (script_data->gen_trigger && script_data->trigger_bit < 32)
- trigger_mask = 1 << script_data->trigger_bit;
- else
- trigger_mask = 0;
- triggered = false;
- return true;
- }
- bool afxEA_CollisionEvent::ea_update(F32 dt)
- {
- afxConstraint* pos_constraint = getPosConstraint();
- set_shape((pos_constraint) ? dynamic_cast<ShapeBase*>(pos_constraint->getSceneObject()) : 0);
- if (mChoreographer && trigger_mask != 0)
- {
- if (triggered)
- {
- mChoreographer->setTriggerMask(trigger_mask | mChoreographer->getTriggerMask());
- triggered = false;
- }
- else
- {
- mChoreographer->setTriggerMask(~trigger_mask & mChoreographer->getTriggerMask());
- }
- }
- return true;
- }
- void afxEA_CollisionEvent::ea_finish(bool was_stopped)
- {
- set_shape(0);
- }
- void afxEA_CollisionEvent::do_runtime_substitutions()
- {
- // only clone the datablock if there are substitutions
- if (script_data->getSubstitutionCount() > 0)
- {
- // clone the datablock and perform substitutions
- afxCollisionEventData* orig_db = script_data;
- script_data = new afxCollisionEventData(*orig_db, true);
- orig_db->performSubstitutions(script_data, mChoreographer, mGroup_index);
- }
- }
- void afxEA_CollisionEvent::set_shape(ShapeBase* new_shape)
- {
- if (shape == new_shape)
- return;
- if (shape)
- {
- shape->unregisterCollisionCallback(this);
- clearNotify(shape);
- }
-
- shape = new_shape;
- if (shape)
- {
- deleteNotify(shape);
- shape->registerCollisionCallback(this);
- }
- }
- void afxEA_CollisionEvent::collisionNotify(SceneObject* obj0, SceneObject* obj1, const VectorF& vel)
- {
- if (obj0 != shape || !mChoreographer || !mChoreographer->getDataBlock())
- return;
- if (script_data->method_name != ST_NULLSTRING)
- {
- char *arg_buf = Con::getArgBuffer(64);
- dSprintf(arg_buf, 256, "%g %g %g", vel.x, vel.y, vel.z);
- // CALL SCRIPT afxChoreographerData::method(%spell, %obj0, %obj1, %velocity)
- Con::executef(mChoreographer->getDataBlock(), script_data->method_name,
- mChoreographer->getIdString(),
- (obj0) ? obj0->getIdString() : "",
- (obj1) ? obj1->getIdString() : "",
- arg_buf,
- script_data->script_data);
- }
- if (!triggered && trigger_mask != 0)
- triggered = true;
- }
- void afxEA_CollisionEvent::onDeleteNotify(SimObject* obj)
- {
- if (obj == shape)
- {
- shape->unregisterCollisionCallback(this);
- shape = 0;
- }
- Parent::onDeleteNotify(obj);
- }
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- class afxEA_CollisionEventDesc : public afxEffectAdapterDesc, public afxEffectDefs
- {
- static afxEA_CollisionEventDesc desc;
- public:
- virtual bool testEffectType(const SimDataBlock*) const;
- virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
- virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
- virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
- virtual bool isPositional(const afxEffectWrapperData*) const { return false; }
- virtual afxEffectWrapper* create() const { return new afxEA_CollisionEvent; }
- };
- afxEA_CollisionEventDesc afxEA_CollisionEventDesc::desc;
- bool afxEA_CollisionEventDesc::testEffectType(const SimDataBlock* db) const
- {
- return (typeid(afxCollisionEventData) == typeid(*db));
- }
- bool afxEA_CollisionEventDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
- {
- return (timing.lifetime < 0);
- }
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
|