Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / Component.cs
  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. /// <param name="delta"></param>
  168. public virtual void Process(float delta)
  169. {
  170. }
  171.  
  172. /// <summary>
  173. /// 如果启用了组件, 则每物理帧会调用一次 PhysicsProcess
  174. /// </summary>
  175. /// <param name="delta"></param>
  176. public virtual void PhysicsProcess(float delta)
  177. {
  178. }
  179.  
  180. /// <summary>
  181. /// 当组件被销毁时调用
  182. /// </summary>
  183. public virtual void OnDestroy()
  184. {
  185. }
  186.  
  187. /// <summary>
  188. /// 当组件启用时调用
  189. /// </summary>
  190. public virtual void OnEnable()
  191. {
  192. }
  193.  
  194. /// <summary>
  195. /// 当组件禁用时调用
  196. /// </summary>
  197. public virtual void OnDisable()
  198. {
  199. }
  200.  
  201. /// <summary>
  202. /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等, 需要调用 ActivityInstance 身上的绘制函数
  203. /// </summary>
  204. public virtual void DebugDraw()
  205. {
  206. }
  207.  
  208. /// <summary>
  209. /// 当组件销毁
  210. /// </summary>
  211. public void Destroy()
  212. {
  213. if (IsDestroyed)
  214. {
  215. return;
  216. }
  217.  
  218. IsDestroyed = true;
  219. Master.RemoveComponent(this);
  220. OnDestroy();
  221. }
  222.  
  223. public T AddComponent<T>() where T : Component, new()
  224. {
  225. return Master.AddComponent<T>();
  226. }
  227. public Component AddComponent(Type type)
  228. {
  229. return Master.AddComponent(type);
  230. }
  231. public T GetComponent<T>() where T : Component, new()
  232. {
  233. return Master.GetComponent<T>();
  234. }
  235. public Component GetComponent(Type type)
  236. {
  237. return Master.GetComponent(type);
  238. }
  239.  
  240. public T[] GetComponents<T>() where T : Component, new()
  241. {
  242. return Master.GetComponents<T>();
  243. }
  244. public Component[] GetComponents(Type type)
  245. {
  246. return Master.GetComponents(type);
  247. }
  248. public void RemoveComponent(Component component)
  249. {
  250. Master.RemoveComponent(component);
  251. }
  252. public void AddChild(Node node)
  253. {
  254. Master.AddChild(node);
  255. }
  256. public void RemoveChild(Node node)
  257. {
  258. Master.RemoveChild(node);
  259. }
  260. public int GetChildCount()
  261. {
  262. return Master.GetChildCount();
  263. }
  264. public Node GetNode(NodePath path)
  265. {
  266. return Master.GetNode(path);
  267. }
  268. public T GetNode<T>(NodePath path) where T : class
  269. {
  270. return Master.GetNode<T>(path);
  271. }
  272. public Node GetNodeOrNull(NodePath path)
  273. {
  274. return Master.GetNodeOrNull(path);
  275. }
  276. public T GetNodeOrNull<T>(NodePath path) where T : class
  277. {
  278. return Master.GetNodeOrNull<T>(path);
  279. }
  280. public Node GetParent()
  281. {
  282. return Master.GetParent();
  283. }
  284.  
  285. public T GetParent<T>() where T : class
  286. {
  287. return Master.GetParent<T>();
  288. }
  289.  
  290. public void Reparent(Node node)
  291. {
  292. Master.Reparent(node);
  293. }
  294.  
  295. public float GetProcessDeltaTime()
  296. {
  297. return (float)Master.GetProcessDeltaTime();
  298. }
  299. public float GetPhysicsProcessDeltaTime()
  300. {
  301. return (float)Master.GetPhysicsProcessDeltaTime();
  302. }
  303. public Vector2 GetGlobalMousePosition()
  304. {
  305. return Master.GetGlobalMousePosition();
  306. }
  307. public Vector2 GetLocalMousePosition()
  308. {
  309. return Master.GetLocalMousePosition();
  310. }
  311. public long StartCoroutine(IEnumerator able)
  312. {
  313. return Master.StartCoroutine(able);
  314. }
  315.  
  316. public void StopCoroutine(long coroutineId)
  317. {
  318. Master.StopCoroutine(coroutineId);
  319. }
  320.  
  321. public bool IsCoroutineOver(long coroutineId)
  322. {
  323. return Master.IsCoroutineOver(coroutineId);
  324. }
  325.  
  326. public void StopAllCoroutine()
  327. {
  328. Master.StopAllCoroutine();
  329. }
  330. }