diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index fb6b78b..0364afa 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -10,4 +10,4 @@ GunPrefab = ExtResource( 4 ) [node name="AnimatedSprite" parent="." index="1"] -frame = 3 +frame = 2 diff --git a/DungeonShooting_Godot/src/framework/ActivityObject.cs b/DungeonShooting_Godot/src/framework/ActivityObject.cs index dbf9cc7..1742c69 100644 --- a/DungeonShooting_Godot/src/framework/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/ActivityObject.cs @@ -1,20 +1,38 @@  using System; +using System.Collections.Generic; using Godot; /// /// 房间内活动物体基类 /// -public abstract class ActivityObject : ActivityObject where T : ActivityObject +public abstract class ActivityObject : KinematicBody2D { + /// + /// 当前物体显示的精灵图像, 节点名称必须叫 "Sprite" + /// + public Sprite Sprite { get; } + + /// + /// 当前物体碰撞器节点, 节点名称必须叫 "Collision" + /// + public CollisionShape2D Collision { get; } + + /// + /// 是否调用过 Destroy() 函数 + /// + public bool IsDestroyed { get; private set; } + + private List> _components = new List>(); + public ActivityObject() { - ComponentControl = (ComponentControl)Activator.CreateInstance(typeof(ComponentControl), this); Sprite = GetNodeOrNull("Sprite"); if (Sprite == null) { GD.PrintErr("ActivityObject节点下必须要有一个'Sprite'节点!"); } + Collision = GetNodeOrNull("Collision"); if (Collision == null) { @@ -23,41 +41,108 @@ } /// - /// 组件管理器 - /// - public new ComponentControl ComponentControl { get; } -} - -/// -/// 房间内活动物体基类 -/// -public abstract class ActivityObject : KinematicBody2D -{ - - /// - /// 组件管理器 - /// - public ComponentControl ComponentControl { get; } - - /// - /// 当前物体显示的精灵图像, 节点名称必须叫 "Sprite" - /// - public Sprite Sprite { get; protected set; } - - /// - /// 当前物体碰撞器节点, 节点名称必须叫 "Collision" - /// - public CollisionShape2D Collision { get; protected set; } - - /// /// 返回是否能与其他ActivityObject互动 /// /// 触发者 - public abstract CheckInteractiveResult CheckInteractive(ActivityObject master) where TU : ActivityObject; + public abstract CheckInteractiveResult CheckInteractive(ActivityObject master); /// /// 与其它ActivityObject互动时调用 /// /// 触发者 - public abstract void Interactive(ActivityObject master) where TU : ActivityObject; + public abstract void Interactive(ActivityObject master); + + public void AddComponent(Component component) + { + if (!ContainsComponent(component)) + { + _components.Add(new KeyValuePair(component.GetType(), component)); + component.OnMount(); + } + } + + public void RemoveComponent(Component component) + { + if (ContainsComponent(component)) + { + component.OnUnMount(); + } + } + + public Component GetComponent(Type type) + { + for (int i = 0; i < _components.Count; i++) + { + var temp = _components[i]; + if (temp.Key == type) + { + return temp.Value; + } + } + + return null; + } + + public TC GetComponent() where TC : Component + { + var component = GetComponent(typeof(TC)); + if (component == null) return null; + return (TC)component; + } + + public override void _Process(float delta) + { + var arr = _components.ToArray(); + for (int i = 0; i < arr.Length; i++) + { + if (IsDestroyed) return; + var temp = arr[i].Value; + if (temp != null && temp.Master == this && temp.Enable) + { + temp._TriggerProcess(delta); + } + } + } + + public override void _PhysicsProcess(float delta) + { + var arr = _components.ToArray(); + for (int i = 0; i < arr.Length; i++) + { + if (IsDestroyed) return; + var temp = arr[i].Value; + if (temp != null && temp.Master == this && temp.Enable) + { + temp._TriggerPhysicsProcess(delta); + } + } + } + + public void Destroy() + { + if (IsDestroyed) + { + return; + } + + IsDestroyed = true; + QueueFree(); + var arr = _components.ToArray(); + for (int i = 0; i < arr.Length; i++) + { + arr[i].Value?.Destroy(); + } + } + + private bool ContainsComponent(Component component) + { + for (int i = 0; i < _components.Count; i++) + { + if (_components[i].Value == component) + { + return true; + } + } + return false; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/Component.cs b/DungeonShooting_Godot/src/framework/Component.cs index 882ccad..037a263 100644 --- a/DungeonShooting_Godot/src/framework/Component.cs +++ b/DungeonShooting_Godot/src/framework/Component.cs @@ -4,106 +4,42 @@ /// /// 组件基类 /// -public abstract class Component : Component where TG : ActivityObject +public abstract class Component : IProcess, IDestroy { - public new ComponentControl ComponentControl { get; private set; } + public ActivityObject Master { get; private set; } public Vector2 Position { - get => ComponentControl.Position; - set => ComponentControl.Position = value; + get => Master.Position; + set => Master.Position = value; } public Vector2 GlobalPosition { - get => ComponentControl.GlobalPosition; - set => ComponentControl.GlobalPosition = value; + get => Master.GlobalPosition; + set => Master.GlobalPosition = value; } - + public bool Visible { - get => ComponentControl.Visible; - set => ComponentControl.Visible = value; + get => Master.Visible; + set => Master.Visible = value; } - - public Sprite Sprite => ComponentControl.Sprite; - public CollisionShape2D Collision => ComponentControl.Collision; + public Sprite Sprite => Master.Sprite; + + public CollisionShape2D Collision => Master.Collision; public bool Enable { get; set; } = true; public bool IsDestroyed { get; private set; } - /// - /// 当组件销毁 - /// - public new void Destroy() - { - if (IsDestroyed) - { - return; - } - - IsDestroyed = true; - if (ComponentControl != null) - { - ComponentControl.RemoveComponent(this); - } - - OnDestroy(); - } - - internal void SetGameObject(ComponentControl componentControl) - { - ComponentControl = componentControl; - } -} - -/// -/// 组件基类 -/// -public abstract class Component : IProcess, IDestroy -{ - public ComponentControl ComponentControl { get; } - - /// - /// 该组件所绑定的ComponentControl的坐标 - /// - Vector2 Position { get; set; } - - /// - /// 该组件所绑定的ComponentControl的全局坐标 - /// - Vector2 GlobalPosition { get; set; } - - /// - /// 该组件所绑定的ComponentControl的显示状态 - /// - bool Visible { get; set; } - - /// - /// 是否启用该组件, 如果停用, 则不会调用 Process 和 PhysicsProcess - /// - bool Enable { get; set; } - - /// - /// 该组件所绑定的ComponentControl显示的精灵 - /// - Sprite Sprite { get; } - - /// - /// 该组件所绑定的ComponentControl的碰撞器 - /// - CollisionShape2D Collision { get; } - - public bool IsDestroyed { get; } - private bool _isReady = false; public Component() { } - + /// /// 第一次调用 Process 或 PhysicsProcess 之前调用 /// @@ -129,7 +65,7 @@ public virtual void OnMount() { } - + /// /// 当该组件被取消挂载时调用 /// @@ -137,8 +73,23 @@ { } - public void Destroy() + /// + /// 当组件销毁 + /// + public new void Destroy() { + if (IsDestroyed) + { + return; + } + + IsDestroyed = true; + if (Master != null) + { + Master.RemoveComponent(this); + } + + OnDestroy(); } internal void _TriggerProcess(float delta) @@ -162,4 +113,9 @@ PhysicsProcess(delta); } + + internal void _SetMaster(ActivityObject master) + { + Master = master; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/ComponentControl.cs b/DungeonShooting_Godot/src/framework/ComponentControl.cs deleted file mode 100644 index b1beb8e..0000000 --- a/DungeonShooting_Godot/src/framework/ComponentControl.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Collections.Generic; -using Godot; - -public class ComponentControl : Node, IDestroy where T : ActivityObject -{ - - public float Altitude { get; set; } - - public Vector2 Position - { - get => Node.Position; - set => Node.Position = value; - } - - public Vector2 GlobalPosition - { - get => Node.GlobalPosition; - set => Node.GlobalPosition = value; - } - - public bool Visible - { - get => Node.Visible; - set => Node.Visible = value; - } - - public Sprite Sprite => Node.Sprite; - - public CollisionShape2D Collision => Node.Collision; - - public bool IsDestroyed { get; private set; } - - public T Node { get; private set; } - - private List>> _components = new List>>(); - - public ComponentControl(T node) - { - Name = "ComponentControl"; - Node = node; - node.AddChild(this); - } - - public void AddComponent(Component component) - { - if (!ContainsComponent(component)) - { - _components.Add(new KeyValuePair>(component.GetType(), component)); - component.SetGameObject(this); - component.OnMount(); - } - } - - public void RemoveComponent(Component component) - { - if (ContainsComponent(component)) - { - component.SetGameObject(null); - component.OnUnMount(); - } - } - - public Component GetComponent(Type type) - { - for (int i = 0; i < _components.Count; i++) - { - var temp = _components[i]; - if (temp.Key == type) - { - return temp.Value; - } - } - - return null; - } - - public TC GetComponent() where TC : Component - { - var component = GetComponent(typeof(TC)); - if (component == null) return null; - return (TC)component; - } - - public override void _Process(float delta) - { - var arr = _components.ToArray(); - for (int i = 0; i < arr.Length; i++) - { - if (IsDestroyed) return; - var temp = arr[i].Value; - if (temp != null && temp.ComponentControl == this && temp.Enable) - { - temp._TriggerProcess(delta); - } - } - } - - public override void _PhysicsProcess(float delta) - { - var arr = _components.ToArray(); - for (int i = 0; i < arr.Length; i++) - { - if (IsDestroyed) return; - var temp = arr[i].Value; - if (temp != null && temp.ComponentControl == this && temp.Enable) - { - temp._TriggerPhysicsProcess(delta); - } - } - } - - public void Destroy() - { - if (IsDestroyed) - { - return; - } - - IsDestroyed = true; - Node.QueueFree(); - var arr = _components.ToArray(); - for (int i = 0; i < arr.Length; i++) - { - arr[i].Value?.Destroy(); - } - } - - private bool ContainsComponent(Component component) - { - for (int i = 0; i < _components.Count; i++) - { - if (_components[i].Value == component) - { - return true; - } - } - return false; - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/common/NodeExtend.cs b/DungeonShooting_Godot/src/game/common/NodeExtend.cs index 8cb5c41..3a1ac71 100644 --- a/DungeonShooting_Godot/src/game/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/game/common/NodeExtend.cs @@ -107,11 +107,11 @@ public static T StartThrow(this ActivityObject node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate) where T : ThrowComponent { - T throwNode = node.ComponentControl.GetComponent(); + T throwNode = node.GetComponent(); if (throwNode == null) { throwNode = Activator.CreateInstance(); - node.ComponentControl.AddComponent(throwNode); + node.AddComponent(throwNode); } else { diff --git a/DungeonShooting_Godot/src/game/item/ThrowComponent.cs b/DungeonShooting_Godot/src/game/item/ThrowComponent.cs index 6b0d129..4618d7f 100644 --- a/DungeonShooting_Godot/src/game/item/ThrowComponent.cs +++ b/DungeonShooting_Godot/src/game/item/ThrowComponent.cs @@ -1,7 +1,7 @@  using Godot; -public class ThrowComponent : Component +public class ThrowComponent : Component { public override void Ready() { diff --git a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs index 752862c..802af39 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs @@ -4,7 +4,7 @@ /// /// 武器的基类 /// -public abstract class Weapon : ActivityObject +public abstract class Weapon : ActivityObject { /// @@ -574,7 +574,7 @@ } } - public override CheckInteractiveResult CheckInteractive(ActivityObject master) + public override CheckInteractiveResult CheckInteractive(ActivityObject master) { var result = new CheckInteractiveResult(this); @@ -625,7 +625,7 @@ return result; } - public override void Interactive(ActivityObject master) + public override void Interactive(ActivityObject master) { if (master is Role roleMaster) //与role碰撞 { diff --git a/DungeonShooting_Godot/src/game/role/Role.cs b/DungeonShooting_Godot/src/game/role/Role.cs index dffd83d..e0a19f6 100644 --- a/DungeonShooting_Godot/src/game/role/Role.cs +++ b/DungeonShooting_Godot/src/game/role/Role.cs @@ -4,7 +4,7 @@ /// /// 角色基类 /// -public abstract class Role : ActivityObject +public abstract class Role : ActivityObject { /// /// 重写的纹理 @@ -107,12 +107,12 @@ /// 检测是否可互动时的返回值 protected abstract void ChangeInteractiveItem(CheckInteractiveResult result); - public override CheckInteractiveResult CheckInteractive(ActivityObject master) + public override CheckInteractiveResult CheckInteractive(ActivityObject master) { return null; } - public override void Interactive(ActivityObject master) + public override void Interactive(ActivityObject master) { }