CCNavMesh.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_H__
  22. #define __CCNAV_MESH_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_NAVMESH
  25. #include "base/CCRef.h"
  26. #include "math/Vec3.h"
  27. #include "recast/Detour/DetourNavMesh.h"
  28. #include "recast/Detour/DetourNavMeshQuery.h"
  29. #include "recast/DetourCrowd/DetourCrowd.h"
  30. #include "recast/DetourTileCache/DetourTileCache.h"
  31. #include <string>
  32. #include <vector>
  33. #include "navmesh/CCNavMeshAgent.h"
  34. #include "navmesh/CCNavMeshDebugDraw.h"
  35. #include "navmesh/CCNavMeshObstacle.h"
  36. #include "navmesh/CCNavMeshUtils.h"
  37. NS_CC_BEGIN
  38. /**
  39. * @addtogroup 3d
  40. * @{
  41. */
  42. class Renderer;
  43. /** @brief NavMesh: The NavMesh information container, include mesh, tileCache, and so on. */
  44. class CC_DLL NavMesh : public Ref
  45. {
  46. public:
  47. /**
  48. Create navmesh
  49. @param navFilePath The NavMesh File path.
  50. @param geomFilePath The geometry File Path,include offmesh information,etc.
  51. */
  52. static NavMesh* create(const std::string &navFilePath, const std::string &geomFilePath);
  53. /** update navmesh. */
  54. void update(float dt);
  55. /** Internal method, the updater of debug drawing, need called each frame. */
  56. void debugDraw(Renderer* renderer);
  57. /** Enable debug draw or disable. */
  58. void setDebugDrawEnable(bool enable);
  59. /** Check enabled debug draw. */
  60. bool isDebugDrawEnabled() const;
  61. /** add a agent to navmesh. */
  62. void addNavMeshAgent(NavMeshAgent *agent);
  63. /** remove a agent from navmesh. */
  64. void removeNavMeshAgent(NavMeshAgent *agent);
  65. /** add a obstacle to navmesh. */
  66. void addNavMeshObstacle(NavMeshObstacle *obstacle);
  67. /** remove a obstacle from navmesh. */
  68. void removeNavMeshObstacle(NavMeshObstacle *obstacle);
  69. /**
  70. find a path on navmesh
  71. @param start The start search position in world coordinate system.
  72. @param end The end search position in world coordinate system.
  73. @param pathPoints the key points of path.
  74. */
  75. void findPath(const Vec3 &start, const Vec3 &end, std::vector<Vec3> &pathPoints);
  76. CC_CONSTRUCTOR_ACCESS:
  77. NavMesh();
  78. virtual ~NavMesh();
  79. protected:
  80. bool initWithFilePath(const std::string &navFilePath, const std::string &geomFilePath);
  81. bool read();
  82. bool loadNavMeshFile();
  83. bool loadGeomFile();
  84. void dtDraw();
  85. void drawAgents();
  86. void drawObstacles();
  87. void drawOffMeshConnections();
  88. protected:
  89. dtNavMesh *_navMesh;
  90. dtNavMeshQuery *_navMeshQuery;
  91. dtCrowd *_crowed;
  92. dtTileCache *_tileCache;
  93. LinearAllocator *_allocator;
  94. FastLZCompressor *_compressor;
  95. MeshProcess *_meshProcess;
  96. GeomData *_geomData;
  97. std::vector<NavMeshAgent*> _agentList;
  98. std::vector<NavMeshObstacle*> _obstacleList;
  99. NavMeshDebugDraw _debugDraw;
  100. std::string _navFilePath;
  101. std::string _geomFilePath;
  102. bool _isDebugDrawEnabled;
  103. };
  104. /** @} */
  105. NS_CC_END
  106. #endif //CC_USE_NAVMESH
  107. #endif // __CCNAV_MESH_H__