diff --git a/DungeonShooting_Godot/src/game/role/IState.cs b/DungeonShooting_Godot/src/game/role/IState.cs index 14d609d..0a7c2b0 100644 --- a/DungeonShooting_Godot/src/game/role/IState.cs +++ b/DungeonShooting_Godot/src/game/role/IState.cs @@ -1,12 +1,12 @@ -/// +using System;/// /// 状态接口 /// -public interface IState where T : ActivityObject +public interface IState where T : ActivityObject where S : Enum { /// /// 当前状态对象对应的状态枚举类型 /// - StateEnum StateType { get; } + S StateType { get; } /// /// 当前状态对象挂载的角色对象 @@ -16,14 +16,14 @@ /// /// 当前状态对象所处的状态机对象 /// - StateController StateController { get; set; } + StateController StateController { get; set; } /// /// 当从其他状态进入到当前状态时调用 /// /// 上一个状态类型 /// 切换当前状态时附带的参数 - void Enter(StateEnum prev, params object[] args); + void Enter(S prev, params object[] args); /// /// 物理帧每帧更新 @@ -34,11 +34,11 @@ /// 是否允许切换至下一个状态 /// /// 下一个状态类型 - bool CanChangeState(StateEnum next); + bool CanChangeState(S next); /// /// 从当前状态退出时调用 /// /// 下一个状态类型 - void Exit(StateEnum next); + void Exit(S next); } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/StateController.cs b/DungeonShooting_Godot/src/game/role/StateController.cs index 2ffc47a..18943c0 100644 --- a/DungeonShooting_Godot/src/game/role/StateController.cs +++ b/DungeonShooting_Godot/src/game/role/StateController.cs @@ -1,21 +1,22 @@ +using System; using Godot; using System.Collections.Generic; /// /// 对象状态机控制器 /// -public class StateController : Component where T : ActivityObject +public class StateController : Component where T : ActivityObject where S : Enum { /// /// 当前活跃的状态 /// - public IState CurrState => _currState; - private IState _currState; + public IState CurrState => _currState; + private IState _currState; /// /// 负责存放状态实例对象 /// - private readonly Dictionary> _states = new Dictionary>(); + private readonly Dictionary> _states = new Dictionary>(); /// /// 记录下当前帧是否有改变的状态 @@ -39,7 +40,7 @@ /// /// 往状态机力注册一个新的状态 /// - public void Register(IState state) + public void Register(IState state) { if (GetStateInstance(state.StateType) != null) { @@ -54,7 +55,7 @@ /// /// 立即切换到下一个指定状态, 并且这一帧会被调用 PhysicsProcess /// - public void ChangeState(StateEnum next, params object[] arg) + public void ChangeState(S next, params object[] arg) { _changeState(false, next, arg); } @@ -62,7 +63,7 @@ /// /// 切换到下一个指定状态, 下一帧才会调用 PhysicsProcess /// - public void ChangeStateLate(StateEnum next, params object[] arg) + public void ChangeStateLate(S next, params object[] arg) { _changeState(true, next, arg); } @@ -70,7 +71,7 @@ /// /// 根据状态类型获取相应的状态对象 /// - private IState GetStateInstance(StateEnum stateType) + private IState GetStateInstance(S stateType) { _states.TryGetValue(stateType, out var v); return v; @@ -79,9 +80,9 @@ /// /// 切换状态 /// - private void _changeState(bool late, StateEnum next, params object[] arg) + private void _changeState(bool late, S next, params object[] arg) { - if (_currState != null && _currState.StateType == next) + if (_currState != null && _currState.StateType.Equals(next)) { return; } @@ -94,7 +95,7 @@ if (_currState == null) { _currState = newState; - newState.Enter(StateEnum.None, arg); + newState.Enter(default, arg); } else if (_currState.CanChangeState(next)) { diff --git a/DungeonShooting_Godot/src/game/role/StateEnum.cs b/DungeonShooting_Godot/src/game/role/StateEnum.cs deleted file mode 100644 index 3ba322d..0000000 --- a/DungeonShooting_Godot/src/game/role/StateEnum.cs +++ /dev/null @@ -1,20 +0,0 @@ - -public enum StateEnum -{ - /// - /// 无状态 - /// - None = 0, - /// - /// 静止状态 - /// - Idle = 1, - /// - /// 奔跑状态 - /// - Run = 2, - /// - /// 行走状态 - /// - Walk = 3, -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs index 142ee21..626896f 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs @@ -22,7 +22,7 @@ /// /// 敌人身上的状态机控制器 /// - public StateController StateController { get; } + public StateController StateController { get; } /// /// 视野半径, 单位像素 @@ -55,7 +55,7 @@ public Enemy() : base(ResourcePath.prefab_role_Enemy_tscn) { - StateController = new StateController(); + StateController = new StateController(); AddComponent(StateController); AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Player; @@ -67,10 +67,10 @@ ViewRay = GetNode("ViewRay"); //注册Ai状态机 - StateController.Register(new AIIdleState()); - StateController.Register(new AIRunState()); + StateController.Register(new AINormalState()); + StateController.Register(new AITailAfterState()); //默认状态 - StateController.ChangeState(StateEnum.Idle); + StateController.ChangeState(AIStateEnum.AINormal); TargetSign = new PathSign(this, PathSignLength, GameApplication.Instance.Room.Player); } diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AIIdleState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AIIdleState.cs deleted file mode 100644 index 06d64eb..0000000 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AIIdleState.cs +++ /dev/null @@ -1,29 +0,0 @@ - -/// -/// AI 静止行为 -/// -public class AIIdleState : IState -{ - public StateEnum StateType { get; } = StateEnum.Idle; - public Enemy Master { get; set; } - public StateController StateController { get; set; } - public void Enter(StateEnum prev, params object[] args) - { - - } - - public void PhysicsProcess(float delta) - { - - } - - public bool CanChangeState(StateEnum next) - { - return true; - } - - public void Exit(StateEnum next) - { - - } -} diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs new file mode 100644 index 0000000..664b8d6 --- /dev/null +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs @@ -0,0 +1,29 @@ + +/// +/// AI 正常状态 +/// +public class AINormalState : IState +{ + public AIStateEnum StateType { get; } = AIStateEnum.AINormal; + public Enemy Master { get; set; } + public StateController StateController { get; set; } + public void Enter(AIStateEnum prev, params object[] args) + { + + } + + public void PhysicsProcess(float delta) + { + + } + + public bool CanChangeState(AIStateEnum next) + { + return true; + } + + public void Exit(AIStateEnum next) + { + + } +} diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs new file mode 100644 index 0000000..9a4efca --- /dev/null +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs @@ -0,0 +1,29 @@ + +/// +/// Ai 不确定玩家位置 +/// +public class AIProbeState : IState +{ + public AIStateEnum StateType { get; } = AIStateEnum.AIProbe; + public Enemy Master { get; set; } + public StateController StateController { get; set; } + public void Enter(AIStateEnum prev, params object[] args) + { + + } + + public void PhysicsProcess(float delta) + { + + } + + public bool CanChangeState(AIStateEnum next) + { + return true; + } + + public void Exit(AIStateEnum next) + { + + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AIRunState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AIRunState.cs deleted file mode 100644 index be7cee5..0000000 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AIRunState.cs +++ /dev/null @@ -1,35 +0,0 @@ - -/// -/// AI 奔跑行为 -/// -public class AIRunState : IState -{ - public StateEnum StateType { get; } = StateEnum.Run; - public Enemy Master { get; set; } - public StateController StateController { get; set; } - public void Enter(StateEnum prev, params object[] args) - { - - } - - public void PhysicsProcess(float delta) - { - var master = Master; - if (master.LookTarget != null) - { - master.AnimatedSprite.Animation = AnimatorNames.ReverseRun; - master.Velocity = (master.LookTarget.GlobalPosition - master.GlobalPosition).Normalized() * master.MoveSpeed; - master.CalcMove(delta); - } - } - - public bool CanChangeState(StateEnum next) - { - return true; - } - - public void Exit(StateEnum next) - { - - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AIStateEnum.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AIStateEnum.cs new file mode 100644 index 0000000..9553cbd --- /dev/null +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AIStateEnum.cs @@ -0,0 +1,16 @@ + +public enum AIStateEnum +{ + /// + /// ai状态, 正常, 未发现目标 + /// + AINormal, + /// + /// 发现目标, 但不知道在哪 + /// + AIProbe, + /// + /// 发现目标, 并且知道位置 + /// + AITailAfter, +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs new file mode 100644 index 0000000..1a84a2d --- /dev/null +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs @@ -0,0 +1,35 @@ + +/// +/// AI 发现玩家 +/// +public class AITailAfterState : IState +{ + public AIStateEnum StateType { get; } = AIStateEnum.AITailAfter; + public Enemy Master { get; set; } + public StateController StateController { get; set; } + public void Enter(AIStateEnum prev, params object[] args) + { + + } + + public void PhysicsProcess(float delta) + { + var master = Master; + if (master.LookTarget != null) + { + master.AnimatedSprite.Animation = AnimatorNames.ReverseRun; + master.Velocity = (master.LookTarget.GlobalPosition - master.GlobalPosition).Normalized() * master.MoveSpeed; + master.CalcMove(delta); + } + } + + public bool CanChangeState(AIStateEnum next) + { + return true; + } + + public void Exit(AIStateEnum next) + { + + } +} \ No newline at end of file