trigger.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _H_TRIGGER
  23. #define _H_TRIGGER
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef _MBOX_H_
  28. #include "math/mBox.h"
  29. #endif
  30. #ifndef _EARLYOUTPOLYLIST_H_
  31. #include "collision/earlyOutPolyList.h"
  32. #endif
  33. #ifndef _MPOLYHEDRON_H_
  34. #include "math/mPolyhedron.h"
  35. #endif
  36. class Convex;
  37. class PhysicsBody;
  38. class Trigger;
  39. DefineConsoleType( TypeTriggerPolyhedron, Polyhedron )
  40. struct TriggerData: public GameBaseData {
  41. typedef GameBaseData Parent;
  42. public:
  43. S32 tickPeriodMS;
  44. bool isClientSide;
  45. TriggerData();
  46. DECLARE_CONOBJECT(TriggerData);
  47. DECLARE_CALLBACK( void, onEnterTrigger, ( Trigger* trigger, GameBase* obj ) );
  48. DECLARE_CALLBACK( void, onTickTrigger, ( Trigger* trigger ) );
  49. DECLARE_CALLBACK( void, onLeaveTrigger, ( Trigger* trigger, GameBase* obj ) );
  50. bool onAdd();
  51. static void initPersistFields();
  52. virtual void packData (BitStream* stream);
  53. virtual void unpackData(BitStream* stream);
  54. };
  55. class Trigger : public GameBase
  56. {
  57. typedef GameBase Parent;
  58. /// Trigger polyhedron with *outward* facing normals and CCW ordered
  59. /// vertices.
  60. Polyhedron mTriggerPolyhedron;
  61. EarlyOutPolyList mClippedList;
  62. Vector<GameBase*> mObjects;
  63. PhysicsBody *mPhysicsRep;
  64. TriggerData* mDataBlock;
  65. U32 mLastThink;
  66. U32 mCurrTick;
  67. Convex *mConvexList;
  68. bool mTripOnce;
  69. bool mTripped;
  70. S32 mTrippedBy;
  71. String mTripCondition;
  72. String mEnterCommand;
  73. String mLeaveCommand;
  74. String mTickCommand;
  75. static const U32 CMD_SIZE = 1024;
  76. void onUnmount(SceneObject* obj,S32 node);
  77. protected:
  78. enum TriggerUpdateBits
  79. {
  80. TransformMask = Parent::NextFreeMask << 0,
  81. PolyMask = Parent::NextFreeMask << 1,
  82. EnterCmdMask = Parent::NextFreeMask << 2,
  83. LeaveCmdMask = Parent::NextFreeMask << 3,
  84. TickCmdMask = Parent::NextFreeMask << 4,
  85. NextFreeMask = Parent::NextFreeMask << 5,
  86. };
  87. static bool smRenderTriggers;
  88. bool testObject(GameBase* enter);
  89. bool testTrippable();
  90. bool testCondition();
  91. bool evalCmD(String*);
  92. void processTick(const Move *move);
  93. void interpolateTick(F32 delta);
  94. void buildConvex(const Box3F& box, Convex* convex);
  95. static bool setEnterCmd(void *object, const char *index, const char *data);
  96. static bool setLeaveCmd(void *object, const char *index, const char *data);
  97. static bool setTickCmd(void *object, const char *index, const char *data);
  98. public:
  99. Trigger();
  100. ~Trigger();
  101. // SimObject
  102. DECLARE_CONOBJECT(Trigger);
  103. DECLARE_CALLBACK( void, onAdd, ( U32 objectId ) );
  104. DECLARE_CALLBACK( void, onRemove, ( U32 objectId ) );
  105. static void consoleInit();
  106. static void initPersistFields();
  107. void testObjects();
  108. bool onAdd();
  109. void onRemove();
  110. void onDeleteNotify(SimObject*);
  111. void inspectPostApply();
  112. // NetObject
  113. U32 packUpdate (NetConnection *conn, U32 mask, BitStream* stream);
  114. void unpackUpdate(NetConnection *conn, BitStream* stream);
  115. // SceneObject
  116. void setTransform(const MatrixF &mat);
  117. void prepRenderImage( SceneRenderState* state );
  118. // GameBase
  119. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  120. // Trigger
  121. void setTriggerPolyhedron(const Polyhedron&);
  122. virtual void potentialEnterObject(GameBase*);
  123. U32 getNumTriggeringObjects() const;
  124. GameBase* getObject(const U32);
  125. const Vector<GameBase*>& getObjects() const { return mObjects; }
  126. void renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  127. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  128. };
  129. inline U32 Trigger::getNumTriggeringObjects() const
  130. {
  131. return mObjects.size();
  132. }
  133. inline GameBase* Trigger::getObject(const U32 index)
  134. {
  135. AssertFatal(index < getNumTriggeringObjects(), "Error, out of range object index");
  136. return mObjects[index];
  137. }
  138. #endif // _H_TRIGGER