Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / serialize / tileSet / TileSetSourceInfo.cs
@小李xl 小李xl on 8 Jan 2024 2 KB 生成Godot.TileSet, 开发中
  1.  
  2. using System.Collections.Generic;
  3. using System.Text.Json.Serialization;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// 图集资源数据
  8. /// </summary>
  9. public class TileSetSourceInfo : IClone<TileSetSourceInfo>, IDestroy
  10. {
  11. [JsonIgnore]
  12. public bool IsDestroyed { get; private set; }
  13. /// <summary>
  14. /// 资源名称
  15. /// </summary>
  16. [JsonInclude]
  17. public string Name;
  18.  
  19. /// <summary>
  20. /// 资源路径
  21. /// </summary>
  22. [JsonInclude]
  23. public string SourcePath;
  24.  
  25. /// <summary>
  26. /// 地形配置数据
  27. /// </summary>
  28. [JsonInclude]
  29. public TileSetTerrainInfo Terrain;
  30.  
  31. /// <summary>
  32. /// 组合数据
  33. /// </summary>
  34. [JsonInclude]
  35. public List<TileCombinationInfo> Combination;
  36. [JsonIgnore]
  37. private Image _sourceImage;
  38.  
  39. /// <summary>
  40. /// 初始化默认数据
  41. /// </summary>
  42. public void InitData()
  43. {
  44. Terrain = new TileSetTerrainInfo();
  45. Terrain.InitData();
  46. Combination = new List<TileCombinationInfo>();
  47. }
  48. /// <summary>
  49. /// 获取资源图像数据
  50. /// </summary>
  51. public Image GetSourceImage()
  52. {
  53. if (_sourceImage == null && string.IsNullOrEmpty(SourcePath))
  54. {
  55. return null;
  56. }
  57. return _sourceImage ??= Image.LoadFromFile(SourcePath);
  58. }
  59. /// <summary>
  60. /// 设置图像资源
  61. /// </summary>
  62. public void SetSourceImage(Image image)
  63. {
  64. if (_sourceImage != null)
  65. {
  66. _sourceImage.Dispose();
  67. }
  68. _sourceImage = image;
  69. }
  70.  
  71. public TileSetSourceInfo Clone()
  72. {
  73. var tileSetSourceInfo = new TileSetSourceInfo();
  74. tileSetSourceInfo.Name = Name;
  75. tileSetSourceInfo.Terrain = Terrain.Clone();
  76. tileSetSourceInfo.Combination = new List<TileCombinationInfo>();
  77. foreach (var combination in Combination)
  78. {
  79. tileSetSourceInfo.Combination.Add(combination.Clone());
  80. }
  81. tileSetSourceInfo.SourcePath = SourcePath;
  82. if (_sourceImage != null)
  83. {
  84. tileSetSourceInfo._sourceImage = (Image)_sourceImage.Duplicate();
  85. }
  86.  
  87. return tileSetSourceInfo;
  88. }
  89. public void Destroy()
  90. {
  91. if (IsDestroyed) return;
  92. IsDestroyed = true;
  93. if (_sourceImage != null)
  94. {
  95. _sourceImage.Dispose();
  96. _sourceImage = null;
  97. }
  98.  
  99. Terrain = null;
  100. Combination = null;
  101. }
  102. }