Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / liquid / BrushImageData.cs
  1. using System;
  2. using Godot;
  3. using System.Collections.Generic;
  4.  
  5. /// <summary>
  6. /// 液体笔刷数据
  7. /// </summary>
  8. public class BrushImageData
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public int Width;
  14.  
  15. public int Height;
  16. public BrushPixelData[] Pixels;
  17.  
  18. //有效像素范围
  19. public int PixelMinX = int.MaxValue;
  20. public int PixelMinY = int.MaxValue;
  21. public int PixelMaxX;
  22. public int PixelMaxY;
  23.  
  24. public int PixelWidth;
  25. public int PixelHeight;
  26.  
  27. //补帧间距倍率
  28. public float Ffm;
  29.  
  30. public BrushImageData(Image image, byte type, float ffm, float duration, float writeOffSpeed)
  31. {
  32. Ffm = ffm;
  33. var list = new List<BrushPixelData>();
  34. var width = image.GetWidth();
  35. var height = image.GetHeight();
  36. var flag = false;
  37. for (var x = 0; x < width; x++)
  38. {
  39. for (var y = 0; y < height; y++)
  40. {
  41. var pixel = image.GetPixel(x, y);
  42. if (pixel.A > 0)
  43. {
  44. flag = true;
  45. list.Add(new BrushPixelData()
  46. {
  47. X = x,
  48. Y = y,
  49. Color = pixel,
  50. Type = type,
  51. Duration = duration,
  52. WriteOffSpeed = writeOffSpeed
  53. });
  54. if (x < PixelMinX)
  55. {
  56. PixelMinX = x;
  57. }
  58. else if (x > PixelMaxX)
  59. {
  60. PixelMaxX = x;
  61. }
  62.  
  63. if (y < PixelMinY)
  64. {
  65. PixelMinY = y;
  66. }
  67. else if (y > PixelMaxY)
  68. {
  69. PixelMaxY = y;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. if (!flag)
  76. {
  77. throw new Exception("不能使用完全透明的图片作为笔刷!");
  78. }
  79.  
  80. Pixels = list.ToArray();
  81. Width = width;
  82. Height = height;
  83.  
  84. PixelWidth = PixelMaxX - PixelMinX;
  85. PixelHeight = PixelMaxY - PixelMinY;
  86. }
  87. }