Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / EditorTileSetManager.cs
@小李xl 小李xl on 7 Jan 2024 10 KB 读取TileSet数据, 基础完成
  1.  
  2. using System.IO;
  3. using System.Text.Json;
  4. using Godot;
  5.  
  6. public static class EditorTileSetManager
  7. {
  8. /// <summary>
  9. /// 扫描路径
  10. /// </summary>
  11. public static string CustomMapPath { get; private set; }
  12. private static bool _init;
  13. public static void Init()
  14. {
  15. if (_init)
  16. {
  17. return;
  18. }
  19.  
  20. _init = true;
  21. #if TOOLS
  22. CustomMapPath = GameConfig.RoomTileSetDir;
  23. #else
  24. CustomMapPath = GameConfig.RoomTileSetDir;
  25. #endif
  26. EventManager.AddEventListener(EventEnum.OnTileSetSave, OnTileSetSave);
  27. }
  28.  
  29. /// <summary>
  30. /// 将地形掩码存入 TileSetTerrainInfo 中
  31. /// </summary>
  32. public static void SetTileSetTerrainBit(TileSetTerrainInfo terrain, int index, byte type, int[] cellData)
  33. {
  34. if (type == 1) //顶部墙壁
  35. {
  36. switch (index)
  37. {
  38. //第一排
  39. case 0: terrain._000_010_010 = cellData; break;
  40. case 1: terrain._000_011_010 = cellData; break;
  41. case 2: terrain._000_111_010 = cellData; break;
  42. case 3: terrain._000_110_010 = cellData; break;
  43. case 4: terrain._110_111_010 = cellData; break;
  44. case 5: terrain._000_111_011 = cellData; break;
  45. case 6: terrain._000_111_110 = cellData; break;
  46. case 7: terrain._011_111_010 = cellData; break;
  47. case 8: terrain._000_011_011 = cellData; break;
  48. case 9: terrain._010_111_111 = cellData; break;
  49. case 10: terrain._000_111_111 = cellData; break;
  50. case 11: terrain._000_110_110 = cellData; break;
  51. //第二排
  52. case 12: terrain._010_010_010 = cellData; break;
  53. case 13: terrain._010_011_010 = cellData; break;
  54. case 14: terrain._010_111_010 = cellData; break;
  55. case 15: terrain._010_110_010 = cellData; break;
  56. case 16: terrain._010_011_011 = cellData; break;
  57. case 17: terrain._011_111_111 = cellData; break;
  58. case 18: terrain._110_111_111 = cellData; break;
  59. case 19: terrain._010_110_110 = cellData; break;
  60. case 20: terrain._011_011_011 = cellData; break;
  61. case 21: terrain._011_111_110 = cellData; break;
  62. case 22: break;
  63. case 23: terrain._110_111_110 = cellData; break;
  64. //第三排
  65. case 24: terrain._010_010_000 = cellData; break;
  66. case 25: terrain._010_011_000 = cellData; break;
  67. case 26: terrain._010_111_000 = cellData; break;
  68. case 27: terrain._010_110_000 = cellData; break;
  69. case 28: terrain._011_011_010 = cellData; break;
  70. case 29: terrain._111_111_011 = cellData; break;
  71. case 30: terrain._111_111_110 = cellData; break;
  72. case 31: terrain._110_110_010 = cellData; break;
  73. case 32: terrain._011_111_011 = cellData; break;
  74. case 33: terrain._111_111_111 = cellData; break;
  75. case 34: terrain._110_111_011 = cellData; break;
  76. case 35: terrain._110_110_110 = cellData; break;
  77. //第四排
  78. case 36: terrain._000_010_000 = cellData; break;
  79. case 37: terrain._000_011_000 = cellData; break;
  80. case 38: terrain._000_111_000 = cellData; break;
  81. case 39: terrain._000_110_000 = cellData; break;
  82. case 40: terrain._010_111_110 = cellData; break;
  83. case 41: terrain._011_111_000 = cellData; break;
  84. case 42: terrain._110_111_000 = cellData; break;
  85. case 43: terrain._010_111_011 = cellData; break;
  86. case 44: terrain._011_011_000 = cellData; break;
  87. case 45: terrain._111_111_000 = cellData; break;
  88. case 46: terrain._111_111_010 = cellData; break;
  89. case 47: terrain._110_110_000 = cellData; break;
  90. }
  91. }
  92. else if (type == 2) //侧方墙壁
  93. {
  94. switch (index)
  95. {
  96. case 0: terrain._vs = cellData; break;
  97. case 1: terrain._vl = cellData; break;
  98. case 2: terrain._vc = cellData; break;
  99. case 3: terrain._vr = cellData; break;
  100. }
  101. }
  102. else if (type == 3) //地板
  103. {
  104. terrain._f = cellData;
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 获取指定索引的地形掩码存储的数据
  110. /// </summary>
  111. public static int[] GetTileSetTerrainBit(TileSetTerrainInfo terrain, int index, byte type)
  112. {
  113. if (type == 1) //顶部墙壁
  114. {
  115. switch (index)
  116. {
  117. //第一排
  118. case 0: return terrain._000_010_010;
  119. case 1: return terrain._000_011_010;
  120. case 2: return terrain._000_111_010;
  121. case 3: return terrain._000_110_010;
  122. case 4: return terrain._110_111_010;
  123. case 5: return terrain._000_111_011;
  124. case 6: return terrain._000_111_110;
  125. case 7: return terrain._011_111_010;
  126. case 8: return terrain._000_011_011;
  127. case 9: return terrain._010_111_111;
  128. case 10: return terrain._000_111_111;
  129. case 11: return terrain._000_110_110;
  130. //第二排
  131. case 12: return terrain._010_010_010;
  132. case 13: return terrain._010_011_010;
  133. case 14: return terrain._010_111_010;
  134. case 15: return terrain._010_110_010;
  135. case 16: return terrain._010_011_011;
  136. case 17: return terrain._011_111_111;
  137. case 18: return terrain._110_111_111;
  138. case 19: return terrain._010_110_110;
  139. case 20: return terrain._011_011_011;
  140. case 21: return terrain._011_111_110;
  141. case 22: break;
  142. case 23: return terrain._110_111_110;
  143. //第三排
  144. case 24: return terrain._010_010_000;
  145. case 25: return terrain._010_011_000;
  146. case 26: return terrain._010_111_000;
  147. case 27: return terrain._010_110_000;
  148. case 28: return terrain._011_011_010;
  149. case 29: return terrain._111_111_011;
  150. case 30: return terrain._111_111_110;
  151. case 31: return terrain._110_110_010;
  152. case 32: return terrain._011_111_011;
  153. case 33: return terrain._111_111_111;
  154. case 34: return terrain._110_111_011;
  155. case 35: return terrain._110_110_110;
  156. //第四排
  157. case 36: return terrain._000_010_000;
  158. case 37: return terrain._000_011_000;
  159. case 38: return terrain._000_111_000;
  160. case 39: return terrain._000_110_000;
  161. case 40: return terrain._010_111_110;
  162. case 41: return terrain._011_111_000;
  163. case 42: return terrain._110_111_000;
  164. case 43: return terrain._010_111_011;
  165. case 44: return terrain._011_011_000;
  166. case 45: return terrain._111_111_000;
  167. case 46: return terrain._111_111_010;
  168. case 47: return terrain._110_110_000;
  169. }
  170. }
  171. else if (type == 2) //侧方墙壁
  172. {
  173. switch (index)
  174. {
  175. case 0: return terrain._vs;
  176. case 1: return terrain._vl;
  177. case 2: return terrain._vc;
  178. case 3: return terrain._vr;
  179. }
  180. }
  181. else if (type == 3) //地板
  182. {
  183. return terrain._f;
  184. }
  185.  
  186. return null;
  187. }
  188.  
  189. //保存图块集
  190. private static void OnTileSetSave(object o)
  191. {
  192. if (o is TileSetInfo tileSetInfo)
  193. {
  194. var dir = CustomMapPath + tileSetInfo.Name;
  195. if (Directory.Exists(dir))
  196. {
  197. //删除多余文件
  198. if (tileSetInfo.Sources == null)
  199. {
  200. Directory.Delete(dir, true);
  201. }
  202. else
  203. {
  204. var directoryInfo = new DirectoryInfo(dir);
  205. var fileInfos = directoryInfo.GetFiles();
  206. foreach (var fileInfo in fileInfos)
  207. {
  208. if (fileInfo.Name.EndsWith(".png"))
  209. {
  210. var name = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
  211. if (tileSetInfo.Sources.FindIndex(info => info.Name == name) < 0)
  212. {
  213. fileInfo.Delete();
  214. }
  215. }
  216. }
  217. }
  218. }
  219. Directory.CreateDirectory(dir);
  220.  
  221. var path = dir + "/TileSet.json";
  222. //保存json
  223. var options = new JsonSerializerOptions();
  224. options.WriteIndented = true;
  225. var jsonText = JsonSerializer.Serialize(tileSetInfo, options);
  226. File.WriteAllText(path, jsonText);
  227. //保存资源
  228. if (tileSetInfo.Sources != null)
  229. {
  230. foreach (var sourceInfo in tileSetInfo.Sources)
  231. {
  232. if (sourceInfo.IsOverWriteImage())
  233. {
  234. var image = sourceInfo.GetSourceImage();
  235. image.SavePng(dir + "/" + sourceInfo.Name + ".png");
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }