Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / mark / ActivityMark.cs
@小李xl 小李xl on 7 Apr 2023 5 KB 修复Mark导致游戏闪退问题
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 物体生成标记
  6. /// </summary>
  7. [Tool]
  8. public partial class ActivityMark : Node2D
  9. {
  10. /// <summary>
  11. /// 物体类型
  12. /// </summary>
  13. [Export]
  14. public ActivityIdPrefix.ActivityPrefixType Type = ActivityIdPrefix.ActivityPrefixType.NonePrefix;
  15.  
  16. /// <summary>
  17. /// 物体id
  18. /// </summary>
  19. [Export]
  20. public string ItemId;
  21.  
  22. /// <summary>
  23. /// 所在层级
  24. /// </summary>
  25. [Export]
  26. public RoomLayerEnum Layer = RoomLayerEnum.NormalLayer;
  27.  
  28. /// <summary>
  29. /// 该标记在第几波调用 BeReady,
  30. /// 一个房间内所以敌人清完即可进入下一波
  31. /// </summary>
  32. [Export]
  33. public int WaveNumber = 1;
  34.  
  35. /// <summary>
  36. /// 延时执行时间,单位:秒
  37. /// </summary>
  38. [Export]
  39. public float DelayTime = 0;
  40.  
  41. /// <summary>
  42. /// 物体会在该矩形区域内随机位置生成
  43. /// </summary>
  44. [Export]
  45. public Vector2I BirthRect = Vector2I.Zero;
  46. /// <summary>
  47. /// 绘制的颜色
  48. /// </summary>
  49. [Export]
  50. public Color DrawColor = new Color(1, 1, 1, 1);
  51.  
  52. /// <summary>
  53. /// 物体初始海拔高度
  54. /// </summary>
  55. [ExportGroup("Vertical")]
  56. [Export(PropertyHint.Range, "0, 36")]
  57. public int Altitude = 0;
  58.  
  59. /// <summary>
  60. /// 物体初始纵轴速度
  61. /// </summary>
  62. [Export(PropertyHint.Range, "-1000,1000,0.1")]
  63. public float VerticalSpeed = 0;
  64.  
  65. /// <summary>
  66. /// 当前标记所在Tile节点
  67. /// </summary>
  68. public TileMap TileRoot;
  69.  
  70. //是否已经结束
  71. private bool _isOver = true;
  72. private float _overTimer = 0;
  73. private float _timer = 0;
  74. private RoomInfo _tempRoom;
  75.  
  76. //绘制的字体
  77. private static Font _drawFont;
  78. /// <summary>
  79. /// 获取物体Id
  80. /// </summary>
  81. public string GetItemId()
  82. {
  83. return ActivityIdPrefix.GetNameByPrefixType(Type) + ItemId;
  84. }
  85.  
  86. public override void _Process(double delta)
  87. {
  88. #if TOOLS
  89. if (Engine.IsEditorHint())
  90. {
  91. QueueRedraw();
  92. return;
  93. }
  94. #endif
  95. if (_isOver)
  96. {
  97. _overTimer += (float)delta;
  98. if (_overTimer >= 1)
  99. {
  100. SetActive(false);
  101. }
  102. }
  103. else
  104. {
  105. if (DelayTime > 0)
  106. {
  107. _timer += (float)delta;
  108. if (_timer >= DelayTime)
  109. {
  110. Doing(_tempRoom);
  111. _tempRoom = null;
  112. _isOver = true;
  113. }
  114. }
  115. }
  116. }
  117.  
  118. /// <summary>
  119. /// 标记准备好了
  120. /// </summary>
  121. public void BeReady(RoomInfo roomInfo)
  122. {
  123. _isOver = false;
  124. _overTimer = 0;
  125. SetActive(true);
  126. if (DelayTime <= 0)
  127. {
  128. Doing(roomInfo);
  129. _isOver = true;
  130. }
  131. else
  132. {
  133. _timer = 0;
  134. _tempRoom = roomInfo;
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// 是否已经结束
  140. /// </summary>
  141. public bool IsOver()
  142. {
  143. return _isOver && _overTimer >= 1;
  144. }
  145.  
  146. /// <summary>
  147. /// 调用该函数表示该标记可以生成物体了
  148. /// </summary>
  149. public virtual void Doing(RoomInfo roomInfo)
  150. {
  151. CreateActivityObject().PutDown(Layer);
  152. }
  153.  
  154. /// <summary>
  155. /// 实例化ItemId指定的物体, 并返回对象实例, 函数会自动设置位置
  156. /// </summary>
  157. protected ActivityObject CreateActivityObject()
  158. {
  159. var instance = ActivityObject.Create(GetItemId());
  160. var pos = Position;
  161. if (BirthRect != Vector2I.Zero)
  162. {
  163. instance.Position = new Vector2(
  164. Utils.RandomRangeInt((int)pos.X - BirthRect.X / 2, (int)pos.X + BirthRect.X / 2),
  165. Utils.RandomRangeInt((int)pos.Y - BirthRect.Y / 2, (int)pos.Y + BirthRect.Y / 2)
  166. );
  167. }
  168. else
  169. {
  170. instance.Position = pos;
  171. }
  172. instance.VerticalSpeed = VerticalSpeed;
  173. instance.Altitude = Altitude;
  174. return instance;
  175. }
  176.  
  177. #if TOOLS
  178. public override void _Draw()
  179. {
  180. if (Engine.IsEditorHint() || GameApplication.Instance.Debug)
  181. {
  182. DrawLine(new Vector2(-2, -2), new Vector2(2, 2), DrawColor, 1f);
  183. DrawLine(new Vector2(-2, 2), new Vector2(2, -2), DrawColor, 1f);
  184.  
  185. if (BirthRect != Vector2.Zero)
  186. {
  187. var c = DrawColor;
  188. c.A = 0.5f;
  189. DrawRect(new Rect2(-BirthRect / 2, BirthRect), c, false, 0.5f);
  190. }
  191.  
  192. if (_drawFont == null)
  193. {
  194. _drawFont = ResourceManager.Load<Font>(ResourcePath.Silver_ttf);
  195. }
  196. DrawString(_drawFont, new Vector2(-14, 12), WaveNumber.ToString(), HorizontalAlignment.Center, 28, 14);
  197. }
  198. }
  199. #endif
  200.  
  201. /// <summary>
  202. /// 设置当前节点是否是活动状态
  203. /// </summary>
  204. public void SetActive(bool flag)
  205. {
  206. // SetProcess(flag);
  207. // SetPhysicsProcess(flag);
  208. // SetProcessInput(flag);
  209. // Visible = flag;
  210. var parent = GetParent();
  211. if (flag)
  212. {
  213. if (parent == null)
  214. {
  215. TileRoot.AddChild(this);
  216. }
  217. else if (parent != TileRoot)
  218. {
  219. parent.RemoveChild(this);
  220. TileRoot.AddChild(this);
  221. }
  222. Owner = TileRoot;
  223. }
  224. else
  225. {
  226. if (parent != null)
  227. {
  228. parent.RemoveChild(this);
  229. Owner = null;
  230. }
  231. }
  232. }
  233. }