trigger.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. String mEnterCommand;
  69. String mLeaveCommand;
  70. String mTickCommand;
  71. static const U32 CMD_SIZE = 1024;
  72. void onUnmount(SceneObject* obj,S32 node);
  73. protected:
  74. enum TriggerUpdateBits
  75. {
  76. TransformMask = Parent::NextFreeMask << 0,
  77. PolyMask = Parent::NextFreeMask << 1,
  78. EnterCmdMask = Parent::NextFreeMask << 2,
  79. LeaveCmdMask = Parent::NextFreeMask << 3,
  80. TickCmdMask = Parent::NextFreeMask << 4,
  81. NextFreeMask = Parent::NextFreeMask << 5,
  82. };
  83. static bool smRenderTriggers;
  84. bool testObject(GameBase* enter);
  85. void processTick(const Move *move);
  86. void interpolateTick(F32 delta);
  87. void buildConvex(const Box3F& box, Convex* convex);
  88. static bool setEnterCmd(void *object, const char *index, const char *data);
  89. static bool setLeaveCmd(void *object, const char *index, const char *data);
  90. static bool setTickCmd(void *object, const char *index, const char *data);
  91. public:
  92. Trigger();
  93. ~Trigger();
  94. // SimObject
  95. DECLARE_CONOBJECT(Trigger);
  96. DECLARE_CALLBACK( void, onAdd, ( U32 objectId ) );
  97. DECLARE_CALLBACK( void, onRemove, ( U32 objectId ) );
  98. static void consoleInit();
  99. static void initPersistFields();
  100. void testObjects();
  101. bool onAdd();
  102. void onRemove();
  103. void onDeleteNotify(SimObject*);
  104. void inspectPostApply();
  105. // NetObject
  106. U32 packUpdate (NetConnection *conn, U32 mask, BitStream* stream);
  107. void unpackUpdate(NetConnection *conn, BitStream* stream);
  108. // SceneObject
  109. void setTransform(const MatrixF &mat);
  110. void prepRenderImage( SceneRenderState* state );
  111. // GameBase
  112. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  113. // Trigger
  114. void setTriggerPolyhedron(const Polyhedron&);
  115. virtual void potentialEnterObject(GameBase*);
  116. U32 getNumTriggeringObjects() const;
  117. GameBase* getObject(const U32);
  118. const Vector<GameBase*>& getObjects() const { return mObjects; }
  119. void renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  120. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  121. };
  122. inline U32 Trigger::getNumTriggeringObjects() const
  123. {
  124. return mObjects.size();
  125. }
  126. inline GameBase* Trigger::getObject(const U32 index)
  127. {
  128. AssertFatal(index < getNumTriggeringObjects(), "Error, out of range object index");
  129. return mObjects[index];
  130. }
  131. #endif // _H_TRIGGER