Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / MapProjectManager.cs
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Godot;
  6.  
  7. public static class MapProjectManager
  8. {
  9. public class MapGroupInfo
  10. {
  11. /// <summary>
  12. /// 组名称
  13. /// </summary>
  14. public string Name;
  15. /// <summary>
  16. /// 组路径
  17. /// </summary>
  18. public string FullPath;
  19. /// <summary>
  20. /// 当前组所在的文件夹
  21. /// </summary>
  22. public string RootPath;
  23. }
  24.  
  25. public class MapRoomInfo
  26. {
  27. /// <summary>
  28. /// 房间名称
  29. /// </summary>
  30. public string Name;
  31. /// <summary>
  32. /// 组名称
  33. /// </summary>
  34. public string Group;
  35. /// <summary>
  36. /// 房间类型
  37. /// </summary>
  38. public DungeonRoomType RoomType;
  39. /// <summary>
  40. /// 文件夹路径
  41. /// </summary>
  42. public string FullPath;
  43. /// <summary>
  44. /// 预览图片
  45. /// </summary>
  46. public string PrevImage;
  47. /// <summary>
  48. /// 当前组所在的文件夹
  49. /// </summary>
  50. public string RootPath;
  51. }
  52. /// <summary>
  53. /// 扫描路径
  54. /// </summary>
  55. public static readonly List<string> ScannerPaths = new List<string>();
  56. /// <summary>
  57. /// 组列表数据
  58. /// </summary>
  59. public static readonly Dictionary<string, MapGroupInfo> GroupData = new Dictionary<string, MapGroupInfo>();
  60.  
  61. private static bool _init;
  62. public static void Init()
  63. {
  64. if (_init)
  65. {
  66. return;
  67. }
  68.  
  69. _init = true;
  70. #if TOOLS
  71. ScannerPaths.Add(GameConfig.RoomTileDir);
  72. #endif
  73. }
  74.  
  75. /// <summary>
  76. /// 刷新组数据
  77. /// </summary>
  78. public static void RefreshMapGroup()
  79. {
  80. GroupData.Clear();
  81. foreach (var path in ScannerPaths)
  82. {
  83. if (Directory.Exists(path))
  84. {
  85. var info = new DirectoryInfo(path);
  86. var directoryInfos = info.GetDirectories();
  87. foreach (var directoryInfo in directoryInfos)
  88. {
  89. var projectInfo = new MapGroupInfo();
  90. projectInfo.Name = directoryInfo.Name;
  91. projectInfo.FullPath = directoryInfo.FullName;
  92. projectInfo.RootPath = info.FullName;
  93. GroupData.TryAdd(projectInfo.FullPath, projectInfo);
  94. }
  95. }
  96. else
  97. {
  98. GD.PrintErr("刷新地图组时发现不存在的路径: " + path);
  99. }
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// 根据路径加载房间
  105. /// </summary>
  106. public static MapRoomInfo[] LoadRoom(string rootPath, string groupName)
  107. {
  108. var path = rootPath + "\\" + groupName;
  109. if (!Directory.Exists(path))
  110. {
  111. GD.PrintErr("加载地牢房间时发现不存在的路径: " + path);
  112. return new MapRoomInfo[0];
  113. }
  114.  
  115. var list = new List<MapRoomInfo>();
  116. var dir = new DirectoryInfo(path);
  117. var roomTypes = Enum.GetValues<DungeonRoomType>();
  118. foreach (var dungeonRoomType in roomTypes)
  119. {
  120. LoadRoomByType(list, dir, rootPath, groupName, dungeonRoomType);
  121. }
  122.  
  123. return list.ToArray();
  124. }
  125.  
  126. private static void LoadRoomByType(List<MapRoomInfo> list, DirectoryInfo dir, string rootPath, string groupName, DungeonRoomType roomType)
  127. {
  128. var typeName = DungeonManager.DungeonRoomTypeToString(roomType);
  129. var path = dir.FullName + "\\" + typeName;
  130. if (Directory.Exists(path))
  131. {
  132. var tempDir = new DirectoryInfo(path);
  133. var directoryInfos = tempDir.GetDirectories();
  134. foreach (var directoryInfo in directoryInfos)
  135. {
  136. if (directoryInfo.GetFiles().Length > 0)
  137. {
  138. var room = new MapRoomInfo();
  139. room.Name = directoryInfo.Name;
  140. room.FullPath = directoryInfo.FullName;
  141. room.RoomType = roomType;
  142. room.Group = groupName;
  143. room.RootPath = rootPath;
  144. list.Add(room);
  145. }
  146. }
  147. }
  148. }
  149. }