Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ActivityObject.cs
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using Godot;
  5. using Plugin;
  6.  
  7. /// <summary>
  8. /// 房间内活动物体基类
  9. /// </summary>
  10. public abstract class ActivityObject : KinematicBody2D
  11. {
  12. /// <summary>
  13. /// 当前物体显示的精灵图像, 节点名称必须叫 "AnimatedSprite", 类型为 AnimatedSprite
  14. /// </summary>
  15. public AnimatedSprite AnimatedSprite { get; }
  16.  
  17. /// <summary>
  18. /// 当前物体显示的阴影图像, 节点名称必须叫 "ShadowSprite", 类型为 Sprite
  19. /// </summary>
  20. public Sprite ShadowSprite { get; }
  21.  
  22. /// <summary>
  23. /// 当前物体碰撞器节点, 节点名称必须叫 "Collision", 类型为 CollisionShape2D
  24. /// </summary>
  25. public CollisionShape2D Collision { get; }
  26.  
  27.  
  28. /// <summary>
  29. /// 是否调用过 Destroy() 函数
  30. /// </summary>
  31. public bool IsDestroyed { get; private set; }
  32.  
  33. private List<KeyValuePair<Type, Component>> _components = new List<KeyValuePair<Type, Component>>();
  34.  
  35. public ActivityObject(string scenePath)
  36. {
  37. //加载预制体
  38. var tempPrefab = ResourceManager.Load<PackedScene>(scenePath);
  39. if (tempPrefab == null)
  40. {
  41. throw new Exception("创建 ActivityObject 的参数 scenePath 为 null !");
  42. }
  43.  
  44. var tempNode = tempPrefab.Instance<ActivityObjectTemplate>();
  45. ZIndex = tempNode.ZIndex;
  46. CollisionLayer = tempNode.CollisionLayer;
  47. CollisionMask = tempNode.CollisionMask;
  48.  
  49. //移动子节点
  50. var count = tempNode.GetChildCount();
  51. for (int i = 0; i < count; i++)
  52. {
  53. var body = tempNode.GetChild(0);
  54. tempNode.RemoveChild(body);
  55. AddChild(body);
  56. switch (body.Name)
  57. {
  58. case "AnimatedSprite":
  59. AnimatedSprite = body as AnimatedSprite;
  60. break;
  61. case "ShadowSprite":
  62. ShadowSprite = body as Sprite;
  63. break;
  64. case "Collision":
  65. Collision = body as CollisionShape2D;
  66. break;
  67. }
  68. }
  69. }
  70.  
  71. /// <summary>
  72. /// 返回是否能与其他ActivityObject互动
  73. /// </summary>
  74. /// <param name="master">触发者</param>
  75. public abstract CheckInteractiveResult CheckInteractive(ActivityObject master);
  76.  
  77. /// <summary>
  78. /// 与其它ActivityObject互动时调用
  79. /// </summary>
  80. /// <param name="master">触发者</param>
  81. public abstract void Interactive(ActivityObject master);
  82.  
  83. public void AddComponent(Component component)
  84. {
  85. if (!ContainsComponent(component))
  86. {
  87. _components.Add(new KeyValuePair<Type, Component>(component.GetType(), component));
  88. component.OnMount();
  89. }
  90. }
  91.  
  92. public void RemoveComponent(Component component)
  93. {
  94. if (ContainsComponent(component))
  95. {
  96. component.OnUnMount();
  97. }
  98. }
  99.  
  100. public Component GetComponent(Type type)
  101. {
  102. for (int i = 0; i < _components.Count; i++)
  103. {
  104. var temp = _components[i];
  105. if (temp.Key == type)
  106. {
  107. return temp.Value;
  108. }
  109. }
  110.  
  111. return null;
  112. }
  113.  
  114. public TC GetComponent<TC>() where TC : Component
  115. {
  116. var component = GetComponent(typeof(TC));
  117. if (component == null) return null;
  118. return (TC)component;
  119. }
  120.  
  121. public override void _Process(float delta)
  122. {
  123. var arr = _components.ToArray();
  124. for (int i = 0; i < arr.Length; i++)
  125. {
  126. if (IsDestroyed) return;
  127. var temp = arr[i].Value;
  128. if (temp != null && temp.ActivityObject == this && temp.Enable)
  129. {
  130. temp._TriggerProcess(delta);
  131. }
  132. }
  133. }
  134.  
  135. public override void _PhysicsProcess(float delta)
  136. {
  137. var arr = _components.ToArray();
  138. for (int i = 0; i < arr.Length; i++)
  139. {
  140. if (IsDestroyed) return;
  141. var temp = arr[i].Value;
  142. if (temp != null && temp.ActivityObject == this && temp.Enable)
  143. {
  144. temp._TriggerPhysicsProcess(delta);
  145. }
  146. }
  147. }
  148.  
  149. public void Destroy()
  150. {
  151. if (IsDestroyed)
  152. {
  153. return;
  154. }
  155.  
  156. IsDestroyed = true;
  157. QueueFree();
  158. var arr = _components.ToArray();
  159. for (int i = 0; i < arr.Length; i++)
  160. {
  161. arr[i].Value?.Destroy();
  162. }
  163. }
  164.  
  165. private bool ContainsComponent(Component component)
  166. {
  167. for (int i = 0; i < _components.Count; i++)
  168. {
  169. if (_components[i].Value == component)
  170. {
  171. return true;
  172. }
  173. }
  174.  
  175. return false;
  176. }
  177. }