trigger.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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() override;
  51. static void initPersistFields();
  52. void packData (BitStream* stream) override;
  53. void unpackData(BitStream* stream) override;
  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 mTripIf;
  72. String mEnterCommand;
  73. String mLeaveCommand;
  74. String mTickCommand;
  75. static const U32 CMD_SIZE = 1024;
  76. void onUnmount(SceneObject* obj,S32 node) override;
  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) override;
  93. void interpolateTick(F32 delta) override;
  94. void buildConvex(const Box3F& box, Convex* convex) override;
  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_CATEGORY("Volume");
  104. DECLARE_CALLBACK( void, onAdd, ( U32 objectId ) );
  105. DECLARE_CALLBACK( void, onRemove, ( U32 objectId ) );
  106. static void consoleInit();
  107. static void initPersistFields();
  108. void testObjects();
  109. bool onAdd() override;
  110. void onRemove() override;
  111. void onDeleteNotify(SimObject*) override;
  112. void inspectPostApply() override;
  113. // NetObject
  114. U32 packUpdate (NetConnection *conn, U32 mask, BitStream* stream) override;
  115. void unpackUpdate(NetConnection *conn, BitStream* stream) override;
  116. // SceneObject
  117. void setTransform(const MatrixF &mat) override;
  118. void prepRenderImage( SceneRenderState* state ) override;
  119. // GameBase
  120. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  121. // Trigger
  122. void setTriggerPolyhedron(const Polyhedron&);
  123. virtual void potentialEnterObject(GameBase*);
  124. U32 getNumTriggeringObjects() const;
  125. GameBase* getObject(const U32);
  126. const Vector<GameBase*>& getObjects() const { return mObjects; }
  127. void renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  128. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info) override;
  129. };
  130. inline U32 Trigger::getNumTriggeringObjects() const
  131. {
  132. return mObjects.size();
  133. }
  134. inline GameBase* Trigger::getObject(const U32 index)
  135. {
  136. AssertFatal(index < getNumTriggeringObjects(), "Error, out of range object index");
  137. return mObjects[index];
  138. }
  139. #endif // _H_TRIGGER