RecastPathComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections.Generic;
  2. using ET;
  3. namespace ET
  4. {
  5. public class RecastPathAwakeSystem: AwakeSystem<RecastPathComponent>
  6. {
  7. public override void Awake(RecastPathComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class RecastPathComponent: Entity
  13. {
  14. /// <summary>
  15. /// 5v5地图的Nav数据路径
  16. /// </summary>
  17. public const string Moba5V5MapNavDataPath = "../Config/RecastNavData/solo_navmesh.bin";
  18. /// <summary>
  19. /// 寻路处理者(可用于拓展多线程,参考A*插件)
  20. /// key为地图id,value为具体处理者
  21. /// </summary>
  22. public Dictionary<int, RecastPathProcessor> m_RecastPathProcessorDic = new Dictionary<int, RecastPathProcessor>();
  23. /// <summary>
  24. /// 初始化寻路引擎
  25. /// </summary>
  26. public void Awake()
  27. {
  28. RecastInterface.Init();
  29. //TODO 先直接在这里强行初始化地图
  30. LoadMapNavData(10001, Moba5V5MapNavDataPath.ToCharArray());
  31. // // 读取体素数据
  32. // VoxelFile = new VoxelFile();
  33. }
  34. /// <summary>
  35. /// 寻路
  36. /// </summary>
  37. public void SearchPath(int mapId, RecastPath recastPath)
  38. {
  39. GetRecastPathProcessor(mapId).CalculatePath(recastPath);
  40. }
  41. public RecastPathProcessor GetRecastPathProcessor(int mapId)
  42. {
  43. if (this.m_RecastPathProcessorDic.TryGetValue(mapId, out var recastPathProcessor))
  44. {
  45. return recastPathProcessor;
  46. }
  47. else
  48. {
  49. Log.Error($"未找到地图id为{mapId}的recastPathProcessor");
  50. return null;
  51. }
  52. }
  53. /// <summary>
  54. /// 加载一个Map的数据
  55. /// </summary>
  56. public void LoadMapNavData(int mapId, char[] navDataPath)
  57. {
  58. if (m_RecastPathProcessorDic.ContainsKey(mapId))
  59. {
  60. Log.Warning($"已存在Id为{mapId}的地图Nav数据,请勿重复加载!");
  61. return;
  62. }
  63. if (RecastInterface.LoadMap(mapId, navDataPath))
  64. {
  65. RecastPathProcessor recastPathProcessor = EntityFactory.Create<RecastPathProcessor>(this.domain);
  66. recastPathProcessor.MapId = mapId;
  67. m_RecastPathProcessorDic[mapId] = recastPathProcessor;
  68. Log.Info($"加载Id为{mapId}的地图Nav数据成功!");
  69. }
  70. }
  71. /// <summary>
  72. /// 卸载地图数据
  73. /// </summary>
  74. /// <param name="mapId">地图Id</param>
  75. public void UnLoadMapNavData(int mapId)
  76. {
  77. if (!m_RecastPathProcessorDic.ContainsKey(mapId))
  78. {
  79. Log.Warning($"不存在Id为{mapId}的地图Nav数据,无法进行卸载!");
  80. return;
  81. }
  82. m_RecastPathProcessorDic[mapId].Dispose();
  83. m_RecastPathProcessorDic.Remove(mapId);
  84. if (RecastInterface.FreeMap(mapId))
  85. {
  86. Log.Info($"地图: {mapId} 释放成功");
  87. }
  88. else
  89. {
  90. Log.Info($"地图: {mapId} 释放失败");
  91. }
  92. }
  93. public override void Dispose()
  94. {
  95. if (this.IsDisposed)
  96. {
  97. return;
  98. }
  99. base.Dispose();
  100. RecastInterface.Fini();
  101. }
  102. }
  103. }