NavMeshQuery.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <DetourNavMesh.h>
  10. #include <RecastNavigation/RecastSmartPointer.h>
  11. namespace RecastNavigation
  12. {
  13. //! Holds pointers to Recast navigation mesh objects and the associated mutex.
  14. //! This structure should be used when performing operations on a navigation mesh.
  15. //! In order to access NavMesh or NavMeshQuery objects, use the object LockGuard(NavMeshQuery&).
  16. class NavMeshQuery
  17. {
  18. public:
  19. class LockGuard;
  20. NavMeshQuery(dtNavMesh* navMesh, dtNavMeshQuery* navQuery)
  21. {
  22. m_mesh.reset(navMesh);
  23. m_query.reset(navQuery);
  24. }
  25. //! A lock guard class with accessors for navigation mesh and query objects.
  26. //! A lock is held in place until this object goes out of scope.
  27. //! Release this object as soon as you are done working with the navigation mesh.
  28. class LockGuard
  29. {
  30. public:
  31. //! Grabs a lock on a mutex in @NavMeshQuery
  32. //! @param navMesh navigation mesh to hold on to
  33. explicit LockGuard(NavMeshQuery& navMesh)
  34. : m_lock(navMesh.m_mutex)
  35. , m_mesh(navMesh.m_mesh.get())
  36. , m_query(navMesh.m_query.get())
  37. {
  38. }
  39. //! Navigation mesh accessor.
  40. dtNavMesh* GetNavMesh()
  41. {
  42. return m_mesh;
  43. }
  44. //! Navigation mesh query accessor.
  45. dtNavMeshQuery* GetNavQuery()
  46. {
  47. return m_query;
  48. }
  49. private:
  50. AZStd::lock_guard<AZStd::recursive_mutex> m_lock;
  51. dtNavMesh* m_mesh = nullptr;
  52. dtNavMeshQuery* m_query = nullptr;
  53. AZ_DISABLE_COPY_MOVE(LockGuard);
  54. };
  55. private:
  56. //! Recast navigation mesh object.
  57. RecastPointer<dtNavMesh> m_mesh;
  58. //! Recast navigation query object that can be used to find paths.
  59. RecastPointer<dtNavMeshQuery> m_query;
  60. //! A mutex for accessing and modifying the navigation mesh.
  61. AZStd::recursive_mutex m_mutex;
  62. };
  63. } // namespace RecastNavigation