Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / GameObject.cs
@小李xl 小李xl on 24 Aug 2022 1 KB 架构调整
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. public abstract class GameObject<T> : IProcess where T : Node2D
  5. {
  6. public Vector3 Position { get; set; }
  7. public Vector2 Position2D { get; set; }
  8.  
  9. public T Node;
  10.  
  11. private List<IComponent<T>> _components = new List<IComponent<T>>();
  12. private bool _isDestroy = false;
  13.  
  14. public GameObject(T node)
  15. {
  16. Node = node;
  17. }
  18.  
  19. public void AddComponent<TN>(Component<TN, T> component) where TN : Node
  20. {
  21. if (!_components.Contains(component))
  22. {
  23. component.SetGameObject(this);
  24. Node.AddChild(component.Node);
  25. }
  26. }
  27.  
  28. public void RemoveComponent<TN>(Component<TN, T> component) where TN : Node
  29. {
  30. if (_components.Remove(component))
  31. {
  32. component.SetGameObject(null);
  33. Node.RemoveChild(component.Node);
  34. }
  35. }
  36.  
  37. public void Process(float delta)
  38. {
  39. var arr = _components.ToArray();
  40. for (int i = 0; i < arr.Length; i++)
  41. {
  42. arr[i].Process(delta);
  43. }
  44. }
  45.  
  46. public void PhysicsProcess(float delta)
  47. {
  48. var arr = _components.ToArray();
  49. for (int i = 0; i < arr.Length; i++)
  50. {
  51. arr[i].PhysicsProcess(delta);
  52. }
  53. }
  54.  
  55. public void Destroy()
  56. {
  57. if (_isDestroy)
  58. {
  59. return;
  60. }
  61. _isDestroy = true;
  62. }
  63.  
  64. public bool IsDestroy()
  65. {
  66. return _isDestroy;
  67. }
  68. }