Trigger.h 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _TRIGGER_H_
  23. #define _TRIGGER_H_
  24. #ifndef _SCENE_OBJECT_H_
  25. #include "2d/sceneobject/SceneObject.h"
  26. #endif
  27. #ifndef _HASHTABLE_H_
  28. #include "collection/hashTable.h"
  29. #endif
  30. ///-----------------------------------------------------------------------------
  31. /// Trigger 2D.
  32. ///-----------------------------------------------------------------------------
  33. class Trigger : public SceneObject
  34. {
  35. typedef SceneObject Parent;
  36. private:
  37. /// Callback Options.
  38. bool mEnterCallback;
  39. bool mStayCallback;
  40. bool mLeaveCallback;
  41. /// Object Mapping Database.
  42. typedef VectorPtr<SceneObject*> collideCallbackType;
  43. collideCallbackType mEnterColliders;
  44. collideCallbackType mLeaveColliders;
  45. public:
  46. Trigger();
  47. virtual ~Trigger() {};
  48. /// Engine.
  49. virtual bool onAdd();
  50. static void initPersistFields();
  51. /// Integration.
  52. virtual void preIntegrate( const F32 totalTime, const F32 elapsedTime, DebugStats *pDebugStats );
  53. virtual void integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  54. /// Rendering.
  55. virtual bool shouldRender( void ) const { return false; }
  56. /// Contact processing.
  57. virtual void onBeginCollision( const TickContact& tickContact );
  58. virtual void onEndCollision( const TickContact& tickContact );
  59. virtual void setGatherContacts( const bool gatherContacts ) { } // Suppress changing contact gathering.
  60. /// Cloning.
  61. virtual void copyTo(SimObject* object);
  62. /// Callback Management.
  63. inline void setEnterCallback(bool enter = true) { mEnterCallback = enter; };
  64. inline void setStayCallback(bool stay = true) { mStayCallback = stay; };
  65. inline void setLeaveCallback(bool leave = true) { mLeaveCallback = leave; };
  66. inline bool getEnterCallback() { return mEnterCallback; };
  67. inline bool getStayCallback() { return mStayCallback; };
  68. inline bool getLeaveCallback() { return mLeaveCallback; };
  69. /// Declare Console Object.
  70. DECLARE_CONOBJECT( Trigger );
  71. protected:
  72. /// Callback Management.
  73. static bool setEnterCallback(void* obj, const char* data) { static_cast<Trigger*>(obj)->setEnterCallback(dAtob(data)); return false; };
  74. static bool writeEnterCallback( void* obj, StringTableEntry pFieldName ) {return static_cast<Trigger*>(obj)->mEnterCallback == false; }
  75. static bool setStayCallback(void* obj, const char* data) { static_cast<Trigger*>(obj)->setStayCallback(dAtob(data)); return false; };
  76. static bool writeStayCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Trigger*>(obj)->mStayCallback == true; }
  77. static bool setLeaveCallback(void* obj, const char* data) { static_cast<Trigger*>(obj)->setLeaveCallback(dAtob(data)); return false; };
  78. static bool writeLeaveCallback( void* obj, StringTableEntry pFieldName ) {return static_cast<Trigger*>(obj)->mLeaveCallback == false; }
  79. };
  80. #endif // _TRIGGER_H_