Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / serialize / NavigationPolygonData.cs
  1.  
  2. using System.Collections.Generic;
  3. using System.Text.Json.Serialization;
  4. using Godot;
  5.  
  6. public enum NavigationPolygonType
  7. {
  8. /// <summary>
  9. /// 外轮廓
  10. /// </summary>
  11. Out,
  12. /// <summary>
  13. /// 内轮廓
  14. /// </summary>
  15. In,
  16. }
  17.  
  18. /// <summary>
  19. /// 描述导航多边形数据
  20. /// </summary>
  21. public class NavigationPolygonData
  22. {
  23. /// <summary>
  24. /// 导航轮廓类型
  25. /// </summary>
  26. [JsonInclude]
  27. public NavigationPolygonType Type;
  28.  
  29. /// <summary>
  30. /// 多边形的顶点, 两个为一组, 单位: 像素, 需要获取转为 Vector2[] 的值请调用 GetPoints() 函数
  31. /// </summary>
  32. [JsonInclude]
  33. public List<float> Points;
  34.  
  35. private Vector2[] _pointVector2Array;
  36. public NavigationPolygonData()
  37. {
  38. }
  39.  
  40. public NavigationPolygonData(NavigationPolygonType type)
  41. {
  42. Type = type;
  43. }
  44.  
  45. /// <summary>
  46. /// 读取所有的坐标点
  47. /// </summary>
  48. public Vector2[] GetPoints()
  49. {
  50. if (_pointVector2Array == null)
  51. {
  52. if (Points == null)
  53. {
  54. return null;
  55. }
  56.  
  57. _pointVector2Array = new Vector2[Points.Count / 2];
  58. for (var i = 0; i < Points.Count; i += 2)
  59. {
  60. _pointVector2Array[i / 2] = new Vector2(Points[i], Points[i + 1]);
  61. }
  62. }
  63.  
  64. return _pointVector2Array;
  65. }
  66.  
  67. /// <summary>
  68. /// 设置所有的坐标点
  69. /// </summary>
  70. public void SetPoints(Vector2[] array)
  71. {
  72. _pointVector2Array = array;
  73. Points = new List<float>();
  74. foreach (var pos in array)
  75. {
  76. Points.Add(pos.X);
  77. Points.Add(pos.Y);
  78. }
  79. }
  80. }