diff --git a/DungeonShooting_Godot/src/framework/Component.cs b/DungeonShooting_Godot/src/framework/Component.cs new file mode 100644 index 0000000..1725b03 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/Component.cs @@ -0,0 +1,97 @@ + +using Godot; + +/// +/// 组件基类 +/// +public abstract class Component : IComponent where TG : Node2D +{ + public GameObject GameObject { get; private set; } + + public Vector2 Position + { + get => GameObject.Position; + set => GameObject.Position = value; + } + + public Vector2 GlobalPosition + { + get => GameObject.GlobalPosition; + set => GameObject.GlobalPosition = value; + } + + public bool Visible + { + get => GameObject.Visible; + set => GameObject.Visible = value; + } + + public bool Enable { get; set; } = true; + + public bool IsDestroyed { get; private set; } + + private bool _isReady = false; + + public virtual void Ready() + { + } + + public virtual void Process(float delta) + { + } + + public virtual void PhysicsProcess(float delta) + { + } + + public virtual void OnDestroy() + { + } + + public virtual void OnMount() + { + } + + public virtual void OnUnMount() + { + } + + public void Destroy() + { + if (IsDestroyed) + { + return; + } + + IsDestroyed = true; + if (GameObject != null) + { + GameObject.RemoveComponent(this); + } + + OnDestroy(); + } + + internal void _TriggerProcess(float delta) + { + if (!_isReady) + { + _isReady = true; + Ready(); + } + Process(delta); + } + internal void _TriggerPhysicsProcess(float delta) + { + if (!_isReady) + { + _isReady = true; + Ready(); + } + PhysicsProcess(delta); + } + internal void SetGameObject(GameObject gameObject) + { + GameObject = gameObject; + } +} diff --git a/DungeonShooting_Godot/src/framework/IComponent.cs b/DungeonShooting_Godot/src/framework/IComponent.cs index 3db06bb..74b5083 100644 --- a/DungeonShooting_Godot/src/framework/IComponent.cs +++ b/DungeonShooting_Godot/src/framework/IComponent.cs @@ -1,18 +1,48 @@  using Godot; +/// +/// 组件接口 +/// public interface IComponent : IProcess, IDestroy where TN : Node2D { + /// + /// 该组件绑定的GameObject对象 + /// GameObject GameObject { get; } + /// + /// 该组件所绑定的GameObject的坐标 + /// Vector2 Position { get; set; } + + /// + /// 该组件所绑定的GameObject的全局坐标 + /// Vector2 GlobalPosition { get; set; } + + /// + /// 该组件所绑定的GameObject的显示状态 + /// bool Visible { get; set; } + + /// + /// 是否启用该组件, 如果停用, 则不会调用 Process 和 PhysicsProcess + /// bool Enable { get; set; } + /// + /// 第一次调用 Process 或 PhysicsProcess 之前调用 + /// void Ready(); - void OnMount(); - void OnUnMount(); - void SetGameObject(GameObject gameObject); + /// + /// 当该组件挂载到GameObject上时调用 + /// + void OnMount(); + + /// + /// 当该组件被取消挂载时调用 + /// + void OnUnMount(); } \ No newline at end of file