CCNavMeshObstacle.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCNAV_MESH_OBSTACLE_H__
  22. #define __CCNAV_MESH_OBSTACLE_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_NAVMESH
  25. #include "2d/CCComponent.h"
  26. #include "base/CCRef.h"
  27. #include "math/Vec3.h"
  28. #include "recast/Detour/DetourNavMesh.h"
  29. #include "recast/DetourTileCache/DetourTileCache.h"
  30. NS_CC_BEGIN
  31. /**
  32. * @addtogroup 3d
  33. * @{
  34. */
  35. /** @brief NavMeshObstacle: The code wrapping of dtTileCacheObstacle, use component mode. */
  36. class CC_DLL NavMeshObstacle : public Component
  37. {
  38. friend class NavMesh;
  39. public:
  40. enum NavMeshObstacleSyncFlag
  41. {
  42. NONE = 0,
  43. NODE_TO_OBSTACLE = 1,
  44. OBSTACLE_TO_NODE = 2,
  45. NODE_AND_NODE = NODE_TO_OBSTACLE | OBSTACLE_TO_NODE,
  46. };
  47. /**
  48. Create obstacle, shape is cylinder
  49. @param radius The radius of obstacle.
  50. @param height The height of obstacle.
  51. */
  52. static NavMeshObstacle* create(float radius, float height);
  53. static const std::string& getNavMeshObstacleComponentName();
  54. virtual void onEnter() override;
  55. virtual void onExit() override;
  56. /** Set radius of obstacle */
  57. void setRadius(float radius);
  58. /** Get radius of obstacle */
  59. float getRadius() const { return _radius; }
  60. /** Set height of obstacle */
  61. void setHeight(float height);
  62. /** Get height of obstacle */
  63. float getHeight() const { return _height; }
  64. /**
  65. * synchronization between node and obstacle is time consuming, you can skip some synchronization using this function
  66. */
  67. void setSyncFlag(const NavMeshObstacleSyncFlag &flag) { _syncFlag = flag; }
  68. NavMeshObstacleSyncFlag getSyncFlag() const { return _syncFlag; }
  69. /** synchronize parameter to obstacle. */
  70. void syncToObstacle();
  71. /** synchronize parameter to node. */
  72. void syncToNode();
  73. CC_CONSTRUCTOR_ACCESS:
  74. NavMeshObstacle();
  75. virtual ~NavMeshObstacle();
  76. bool initWith(float radius, float height);
  77. private:
  78. void addTo(dtTileCache *tileCache);
  79. void removeFrom(dtTileCache *tileCache);
  80. void preUpdate(float delta);
  81. void postUpdate(float delta);
  82. private:
  83. float _radius;
  84. float _height;
  85. NavMeshObstacleSyncFlag _syncFlag;
  86. dtObstacleRef _obstacleID;
  87. dtTileCache *_tileCache;
  88. };
  89. /** @} */
  90. NS_CC_END
  91. #endif //CC_USE_NAVMESH
  92. #endif // __CCNAV_MESH_OBSTACLE_H__