Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / generator / DungeonRoomGenerator.cs
@小李xl 小李xl on 17 Jul 2023 8 KB 保存房间大小位置数据
  1.  
  2. #if TOOLS
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using Godot;
  10.  
  11. namespace Generator;
  12.  
  13. /// <summary>
  14. /// 地牢房间数据生成器
  15. /// </summary>
  16. public static class DungeonRoomGenerator
  17. {
  18. /// <summary>
  19. /// 根据名称在编辑器中创建地牢的预制房间, open 表示创建完成后是否在编辑器中打开这个房间
  20. /// </summary>
  21. public static bool CreateDungeonRoom(string groupName, string roomType, string roomName, bool open = false)
  22. {
  23. try
  24. {
  25. var path = GameConfig.RoomTileDir + groupName + "/" + roomType;
  26. if (!Directory.Exists(path))
  27. {
  28. Directory.CreateDirectory(path);
  29. }
  30. //创建场景资源
  31. var prefabFile = path + "/" + roomName + ".tscn";
  32. var prefabResPath = "res://" + prefabFile;
  33. if (!Directory.Exists(GameConfig.RoomTileDir))
  34. {
  35. Directory.CreateDirectory(GameConfig.RoomTileDir);
  36. }
  37. //加载脚本资源
  38. // 这段代码在 Godot4.0.1rc1中会报错, 应该是个bug
  39. // var scriptRes = GD.Load<CSharpScript>("res://src/framework/map/DungeonRoomTemplate.cs");
  40. // var tileMap = new TileMap();
  41. // tileMap.Name = roomName;
  42. // tileMap.SetScript(scriptRes);
  43. // var scene = new PackedScene();
  44. // scene.Pack(tileMap); //报错在这一行, 报的是访问了被销毁的资源 scriptRes
  45. // ResourceSaver.Save(scene, prefabResPath);
  46. //临时处理
  47. {
  48. var tscnCode = $"[gd_scene load_steps=2 format=3]\n" +
  49. $"\n" +
  50. $"[ext_resource type=\"Script\" path=\"res://src/framework/map/DungeonRoomTemplate.cs\" id=\"dungeonRoomTemplate\"]\n" +
  51. $"\n" +
  52. $"[node name=\"{roomName}\" type=\"TileMap\"]\n" +
  53. $"format = 2\n" +
  54. $"script = ExtResource(\"dungeonRoomTemplate\")\n";
  55. File.WriteAllText(prefabFile, tscnCode);
  56. //重新保存一遍, 以让编辑器生成资源Id
  57. var scene = GD.Load<PackedScene>(prefabResPath);
  58. ResourceSaver.Save(scene, prefabResPath);
  59. }
  60.  
  61. //打包房间配置
  62. GenerateRoomConfig();
  63. //生成 UiManager_Methods.cs 代码
  64. UiManagerMethodsGenerator.Generate();
  65. //打开房间
  66. if (open)
  67. {
  68. Plugin.Plugin.Instance.GetEditorInterface().OpenSceneFromPath(prefabResPath);
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. GD.PrintErr(e.ToString());
  74. return false;
  75. }
  76. return true;
  77. }
  78. /// <summary>
  79. /// 执行生成 RoomConfig.json 操作, 返回是否执行成功
  80. /// </summary>
  81. public static bool GenerateRoomConfig()
  82. {
  83. GD.Print("生成RoomConfig.json流程得改");
  84. // try
  85. // {
  86. // //地图路径
  87. // var tileDir = GameConfig.RoomTileDir;
  88. // //地图描述数据路径
  89. // var tileDataDir = GameConfig.RoomTileDataDir;
  90. //
  91. // var tileGroup = new DirectoryInfo(tileDir).GetDirectories();
  92. // var tileDataGroup = new DirectoryInfo(tileDataDir).GetDirectories();
  93. //
  94. // //所有地图列表
  95. // var map = new Dictionary<string, FileInfo>();
  96. //
  97. // //地图场景
  98. // foreach (var groupDir in tileGroup)
  99. // {
  100. // var groupName = groupDir.Name;
  101. // var typeDirArray = groupDir.GetDirectories();
  102. // //遍历枚举, 获取指定路径文件
  103. // foreach (DungeonRoomType roomType in Enum.GetValues(typeof(DungeonRoomType)))
  104. // {
  105. // var typeName = DungeonManager.DungeonRoomTypeToString(roomType);
  106. //
  107. // //收集所有文件名称
  108. // var tempFileDataInfos = typeDirArray.FirstOrDefault(dirInfo => dirInfo.Name == typeName);
  109. // if (tempFileDataInfos != null)
  110. // {
  111. // foreach (var fileInfo in tempFileDataInfos.GetFiles())
  112. // {
  113. // if (fileInfo.Extension == ".tscn")
  114. // {
  115. // var pathInfo = new FileInfo(groupName, roomType, typeName, ResourceManager.RemoveExtension(fileInfo.Name));
  116. // map.TryAdd(pathInfo.GetPath(), pathInfo);
  117. // }
  118. // }
  119. // }
  120. // }
  121. // }
  122. //
  123. // //地图配置数据
  124. // foreach (var groupDir in tileDataGroup)
  125. // {
  126. // var groupName = groupDir.Name;
  127. // var typeDirArray = groupDir.GetDirectories();
  128. // //遍历枚举, 获取指定路径文件
  129. // foreach (DungeonRoomType roomType in Enum.GetValues(typeof(DungeonRoomType)))
  130. // {
  131. // var typeName = DungeonManager.DungeonRoomTypeToString(roomType);
  132. //
  133. // //收集所有文件名称
  134. // var tempFileDataInfos = typeDirArray.FirstOrDefault(dirInfo => dirInfo.Name == typeName);
  135. // if (tempFileDataInfos != null)
  136. // {
  137. // foreach (var fileInfo in tempFileDataInfos.GetFiles())
  138. // {
  139. // if (fileInfo.Extension == ".json")
  140. // {
  141. // var pathInfo = new FileInfo(groupName, roomType, typeName, ResourceManager.RemoveExtension(fileInfo.Name));
  142. // map.TryAdd(pathInfo.GetPath(), pathInfo);
  143. // }
  144. // }
  145. // }
  146. // }
  147. // }
  148. //
  149. // //剔除多余的 tile.json
  150. // foreach (var item in map)
  151. // {
  152. // var path = item.Key;
  153. // if (!File.Exists(tileDir + path + ".tscn"))
  154. // {
  155. // map.Remove(path);
  156. // var filePath = tileDataDir + path + ".json";
  157. // if (File.Exists(filePath))
  158. // {
  159. // GD.Print($"未找到'{tileDir + path}.tscn', 删除配置文件: {filePath}");
  160. // File.Delete(filePath);
  161. // }
  162. // }
  163. // }
  164. //
  165. // //手动生成缺失的 tile.json
  166. // foreach (var item in map)
  167. // {
  168. // if (!File.Exists(tileDataDir + item.Key + ".json"))
  169. // {
  170. // var tscnName = tileDir + item.Key + ".tscn";
  171. // var packedScene = ResourceManager.Load<PackedScene>(tscnName, false);
  172. // if (packedScene != null)
  173. // {
  174. // var dungeonRoomTemplate = packedScene.Instantiate<DungeonRoomTemplate>();
  175. // var usedRect = dungeonRoomTemplate.GetUsedRect();
  176. // var dungeonTile = new DungeonTileMap(dungeonRoomTemplate);
  177. // dungeonTile.SetFloorAtlasCoords(new List<Vector2I>() { new Vector2I(0, 8) });
  178. // //计算导航网格
  179. // dungeonTile.GenerateNavigationPolygon(0);
  180. //
  181. // DungeonRoomInfo.SaveConfig(new List<DoorAreaInfo>(), usedRect.Position, usedRect.Size,
  182. // item.Value.GroupName, item.Value.RoomType, item.Value.FileName, dungeonRoomTemplate.Weight);
  183. // dungeonRoomTemplate.QueueFree();
  184. // }
  185. // }
  186. // }
  187. //
  188. // var roomGroupMap = new Dictionary<string, DungeonRoomGroup>();
  189. // //var list = new List<DungeonRoomSplit>();
  190. // //整合操作
  191. // foreach (var item in map)
  192. // {
  193. // var path = item.Key;
  194. // var configPath = tileDataDir + path + ".json";
  195. // var split = new DungeonRoomSplit();
  196. // split.ScenePath = ResourceManager.ToResPath(tileDir + path + ".tscn");
  197. // split.RoomPath = ResourceManager.ToResPath(configPath);
  198. //
  199. // if (!roomGroupMap.TryGetValue(item.Value.GroupName, out var group))
  200. // {
  201. // group = new DungeonRoomGroup();
  202. // group.GroupName = item.Value.GroupName;
  203. // roomGroupMap.Add(group.GroupName, group);
  204. // }
  205. //
  206. // group.GetRoomList(item.Value.RoomType).Add(split);
  207. // }
  208. //
  209. // //写出配置
  210. // var config = new JsonSerializerOptions();
  211. // config.WriteIndented = true;
  212. // var text = JsonSerializer.Serialize(roomGroupMap, config);
  213. // File.WriteAllText(GameConfig.RoomTileConfigFile, text);
  214. //
  215. // GD.Print("地牢房间配置, 重新打包完成!");
  216. // }
  217. // catch (Exception e)
  218. // {
  219. // GD.PrintErr(e.ToString());
  220. // return false;
  221. // }
  222.  
  223. return true;
  224. }
  225.  
  226. private class FileInfo
  227. {
  228. public FileInfo(string groupName, DungeonRoomType roomType, string typeName, string fileName)
  229. {
  230. GroupName = groupName;
  231. RoomType = roomType;
  232. TypeName = typeName;
  233. FileName = fileName;
  234. }
  235.  
  236. public string GroupName;
  237. public DungeonRoomType RoomType;
  238. public string TypeName;
  239. public string FileName;
  240.  
  241. public string GetPath()
  242. {
  243. return GroupName + "/" + TypeName + "/" + FileName;
  244. }
  245. }
  246. }
  247.  
  248.  
  249. #endif