trigger.h 4.9 KB

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