Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / Component.cs
@小李xl 小李xl on 21 Nov 2023 7 KB 第二种敌人死亡动画
  1.  
  2. using System;
  3. using System.Collections;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// 组件基类, 用于挂载到游戏物体上, 相比于原生 Node 更加轻量化, 实例化 Component 不会创建额外的 Node, 可以大量添加组件
  8. /// </summary>
  9. public abstract class Component<T> : Component where T : ActivityObject
  10. {
  11. /// <summary>
  12. /// 当前组件所挂载的游戏对象
  13. /// </summary>
  14. public new T Master => (T)base.Master;
  15. }
  16.  
  17. /// <summary>
  18. /// 组件基类, 用于挂载到游戏物体上, 相比于原生 Node 更加轻量化, 实例化 Component 不会创建额外的 Node, 可以大量添加组件
  19. /// </summary>
  20. public abstract class Component : IProcess, IDestroy, ICoroutine
  21. {
  22. /// <summary>
  23. /// 当前组件所挂载的游戏对象
  24. /// </summary>
  25. public ActivityObject Master { get; internal set; }
  26.  
  27. /// <summary>
  28. /// 当前组件所挂载的物体的坐标
  29. /// </summary>
  30. public Vector2 Position
  31. {
  32. get => Master.Position;
  33. set => Master.Position = value;
  34. }
  35.  
  36. /// <summary>
  37. /// 当前组件所挂载物体的全局坐标
  38. /// </summary>
  39. public Vector2 GlobalPosition
  40. {
  41. get => Master.GlobalPosition;
  42. set => Master.GlobalPosition = value;
  43. }
  44.  
  45. /// <summary>
  46. /// 当前组件所挂载物体的缩放
  47. /// </summary>
  48. public Vector2 Scale
  49. {
  50. get => Master.Scale;
  51. set => Master.Scale = value;
  52. }
  53. /// <summary>
  54. /// 当前组件所挂载物体的全局缩放
  55. /// </summary>
  56. public Vector2 GlobalScale
  57. {
  58. get => Master.GlobalScale;
  59. set => Master.GlobalScale = value;
  60. }
  61.  
  62. /// <summary>
  63. /// 当前组件所挂载物体的旋转角度
  64. /// </summary>
  65. public float Rotation
  66. {
  67. get => Master.Rotation;
  68. set => Master.Rotation = value;
  69. }
  70. /// <summary>
  71. /// 当前组件所挂载物体的全局旋转角度
  72. /// </summary>
  73. public float GlobalRotation
  74. {
  75. get => Master.GlobalRotation;
  76. set => Master.GlobalRotation = value;
  77. }
  78.  
  79. /// <summary>
  80. /// 当前组件所挂载物体的角度制旋转角度
  81. /// </summary>
  82. public float RotationDegrees
  83. {
  84. get => Master.RotationDegrees;
  85. set => Master.RotationDegrees = value;
  86. }
  87. /// <summary>
  88. /// 当前组件所挂载物体的全局角度制旋转角度
  89. /// </summary>
  90. public float GlobalRotationDegrees
  91. {
  92. get => Master.GlobalRotationDegrees;
  93. set => Master.GlobalRotationDegrees = value;
  94. }
  95. /// <summary>
  96. /// 当前组件所挂载物体的ZIndex
  97. /// </summary>
  98. public int ZIndex
  99. {
  100. get => Master.ZIndex;
  101. set => Master.ZIndex = value;
  102. }
  103. /// <summary>
  104. /// 当前组件是否显示
  105. /// </summary>
  106. public bool Visible
  107. {
  108. get => Master.Visible;
  109. set => Master.Visible = value;
  110. }
  111.  
  112. /// <summary>
  113. /// 挂载物体的动画节点
  114. /// </summary>
  115. public AnimatedSprite2D AnimatedSprite => Master.AnimatedSprite;
  116. /// <summary>
  117. /// 挂载物体的阴影节点
  118. /// </summary>
  119. public Sprite2D ShadowSprite => Master.ShadowSprite;
  120. /// <summary>
  121. /// 挂载物体的碰撞器节点
  122. /// </summary>
  123. public CollisionShape2D Collision => Master.Collision;
  124. /// <summary>
  125. /// 移动控制器
  126. /// </summary>
  127. public MoveController MoveController => Master.MoveController;
  128.  
  129. /// <summary>
  130. /// 是否启用当前组件, 如果禁用, 则不会调用 Process 和 PhysicsProcess
  131. /// </summary>
  132. public bool Enable
  133. {
  134. get => _enable;
  135. set
  136. {
  137. if (!_enable && value)
  138. {
  139. _enable = true;
  140. OnEnable();
  141. }
  142. else if (_enable && !value)
  143. {
  144. _enable = false;
  145. OnDisable();
  146. }
  147. }
  148. }
  149.  
  150. private bool _enable = true;
  151.  
  152. /// <summary>
  153. /// 是否被销毁
  154. /// </summary>
  155. public bool IsDestroyed { get; private set; }
  156.  
  157. /// <summary>
  158. /// 初始化时调用
  159. /// </summary>
  160. public virtual void Ready()
  161. {
  162. }
  163.  
  164. /// <summary>
  165. /// 如果启用了组件, 则每帧会调用一次 Process
  166. /// </summary>
  167. public virtual void Process(float delta)
  168. {
  169. }
  170.  
  171. /// <summary>
  172. /// 如果启用了组件, 则每物理帧会调用一次 PhysicsProcess
  173. /// </summary>
  174. public virtual void PhysicsProcess(float delta)
  175. {
  176. }
  177.  
  178. /// <summary>
  179. /// 当组件被销毁时调用
  180. /// </summary>
  181. public virtual void OnDestroy()
  182. {
  183. }
  184.  
  185. /// <summary>
  186. /// 当组件启用时调用
  187. /// </summary>
  188. public virtual void OnEnable()
  189. {
  190. }
  191.  
  192. /// <summary>
  193. /// 当组件禁用时调用
  194. /// </summary>
  195. public virtual void OnDisable()
  196. {
  197. }
  198.  
  199. /// <summary>
  200. /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等, 需要调用 ActivityInstance 身上的绘制函数
  201. /// </summary>
  202. public virtual void DebugDraw()
  203. {
  204. }
  205.  
  206. /// <summary>
  207. /// 当组件销毁
  208. /// </summary>
  209. public void Destroy()
  210. {
  211. if (IsDestroyed)
  212. {
  213. return;
  214. }
  215.  
  216. IsDestroyed = true;
  217. Master.RemoveComponent(this);
  218. OnDestroy();
  219. }
  220.  
  221. public T AddComponent<T>() where T : Component, new()
  222. {
  223. return Master.AddComponent<T>();
  224. }
  225. public Component AddComponent(Type type)
  226. {
  227. return Master.AddComponent(type);
  228. }
  229. public T GetComponent<T>() where T : Component, new()
  230. {
  231. return Master.GetComponent<T>();
  232. }
  233. public Component GetComponent(Type type)
  234. {
  235. return Master.GetComponent(type);
  236. }
  237.  
  238. public T[] GetComponents<T>() where T : Component, new()
  239. {
  240. return Master.GetComponents<T>();
  241. }
  242. public Component[] GetComponents(Type type)
  243. {
  244. return Master.GetComponents(type);
  245. }
  246. public void RemoveComponent(Component component)
  247. {
  248. Master.RemoveComponent(component);
  249. }
  250. public void AddChild(Node node)
  251. {
  252. Master.AddChild(node);
  253. }
  254. public void RemoveChild(Node node)
  255. {
  256. Master.RemoveChild(node);
  257. }
  258. public int GetChildCount()
  259. {
  260. return Master.GetChildCount();
  261. }
  262. public Node GetNode(NodePath path)
  263. {
  264. return Master.GetNode(path);
  265. }
  266. public T GetNode<T>(NodePath path) where T : class
  267. {
  268. return Master.GetNode<T>(path);
  269. }
  270. public Node GetNodeOrNull(NodePath path)
  271. {
  272. return Master.GetNodeOrNull(path);
  273. }
  274. public T GetNodeOrNull<T>(NodePath path) where T : class
  275. {
  276. return Master.GetNodeOrNull<T>(path);
  277. }
  278. public Node GetParent()
  279. {
  280. return Master.GetParent();
  281. }
  282.  
  283. public T GetParent<T>() where T : class
  284. {
  285. return Master.GetParent<T>();
  286. }
  287.  
  288. public void Reparent(Node node)
  289. {
  290. Master.Reparent(node);
  291. }
  292.  
  293. public float GetProcessDeltaTime()
  294. {
  295. return (float)Master.GetProcessDeltaTime();
  296. }
  297. public float GetPhysicsProcessDeltaTime()
  298. {
  299. return (float)Master.GetPhysicsProcessDeltaTime();
  300. }
  301. public Vector2 GetGlobalMousePosition()
  302. {
  303. return Master.GetGlobalMousePosition();
  304. }
  305. public Vector2 GetLocalMousePosition()
  306. {
  307. return Master.GetLocalMousePosition();
  308. }
  309. public long StartCoroutine(IEnumerator able)
  310. {
  311. return Master.StartCoroutine(able);
  312. }
  313.  
  314. public void StopCoroutine(long coroutineId)
  315. {
  316. Master.StopCoroutine(coroutineId);
  317. }
  318.  
  319. public bool IsCoroutineOver(long coroutineId)
  320. {
  321. return Master.IsCoroutineOver(coroutineId);
  322. }
  323.  
  324. public void StopAllCoroutine()
  325. {
  326. Master.StopAllCoroutine();
  327. }
  328. }