Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / serialize / tileSet / TileSetSourceInfo.cs
  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 List<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 List<TileSetTerrainInfo>();
  45. Combination = new List<TileCombinationInfo>();
  46. }
  47. /// <summary>
  48. /// 获取资源图像数据
  49. /// </summary>
  50. public Image GetSourceImage()
  51. {
  52. if (_sourceImage == null && string.IsNullOrEmpty(SourcePath))
  53. {
  54. return null;
  55. }
  56. return _sourceImage ??= Image.LoadFromFile(SourcePath);
  57. }
  58. /// <summary>
  59. /// 设置图像资源
  60. /// </summary>
  61. public void SetSourceImage(Image image)
  62. {
  63. if (_sourceImage != null)
  64. {
  65. _sourceImage.Dispose();
  66. }
  67. _sourceImage = image;
  68. }
  69.  
  70. public TileSetSourceInfo Clone()
  71. {
  72. var tileSetSourceInfo = new TileSetSourceInfo();
  73. tileSetSourceInfo.Name = Name;
  74. tileSetSourceInfo.Terrain = new List<TileSetTerrainInfo>();
  75. foreach (var item in Terrain)
  76. {
  77. tileSetSourceInfo.Terrain.Add(item.Clone());
  78. }
  79. tileSetSourceInfo.Combination = new List<TileCombinationInfo>();
  80. foreach (var combination in Combination)
  81. {
  82. tileSetSourceInfo.Combination.Add(combination.Clone());
  83. }
  84. tileSetSourceInfo.SourcePath = SourcePath;
  85. if (_sourceImage != null)
  86. {
  87. tileSetSourceInfo._sourceImage = (Image)_sourceImage.Duplicate();
  88. }
  89.  
  90. return tileSetSourceInfo;
  91. }
  92. public void Destroy()
  93. {
  94. if (IsDestroyed) return;
  95. IsDestroyed = true;
  96. if (_sourceImage != null)
  97. {
  98. _sourceImage.Dispose();
  99. _sourceImage = null;
  100. }
  101.  
  102. Terrain = null;
  103. Combination = null;
  104. }
  105. }