diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index 7154560..7bfb950 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -24,4 +24,4 @@ [node name="AnimatedSprite" parent="." index="2"] material = SubResource( 2 ) -frame = 1 +frame = 3 diff --git a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs index bd6bf06..6e3eaa8 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs @@ -23,7 +23,7 @@ /// 敌人身上的状态机控制器 /// public StateController StateController { get; } - + /// /// 视野半径, 单位像素 /// @@ -33,50 +33,36 @@ /// 背后的视野半径, 单位像素 /// public float BackViewRange { get; set; } = 50; - + /// /// 视野检测射线, 朝玩家打射线, 检测是否碰到墙 /// public RayCast2D ViewRay { get; } - - //------------------- 寻路相关 --------------------------- - - // /// - // /// 移动目标标记 - // /// - // public PathSign PathSign { get; } - // - // /// - // /// 寻路标记线段总长度 - // /// - // public float PathSignLength { get; set; } = 500; - - //------------------------------------------------------- private Position2D _navigationPoint; private NavigationAgent2D _navigationAgent2D; private float _navigationUpdateTimer = 0; - + public Enemy() : base(ResourcePath.prefab_role_Enemy_tscn) { StateController = new StateController(); AddComponent(StateController); - + AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Player; Camp = CampEnum.Camp2; MoveSpeed = 30; - + Holster.SlotList[2].Enable = true; Holster.SlotList[3].Enable = true; - + //视野射线 ViewRay = GetNode("ViewRay"); _navigationPoint = GetNode("NavigationPoint"); _navigationAgent2D = _navigationPoint.GetNode("NavigationAgent2D"); - + //PathSign = new PathSign(this, PathSignLength, GameApplication.Instance.Room.Player); - + //注册Ai状态机 StateController.Register(new AINormalState()); StateController.Register(new AIProbeState()); @@ -88,58 +74,37 @@ base._Ready(); //默认状态 StateController.ChangeState(AIStateEnum.AINormal); - + _navigationAgent2D.SetTargetLocation(GameApplication.Instance.Room.Player.GlobalPosition); } - - public override void _Process(float delta) - { - base._Process(delta); - if (GameApplication.Instance.Debug) - { - // PathSign.Scale = new Vector2((int)Face, 1); - Update(); - } - } - public override void _PhysicsProcess(float delta) + /// + /// 返回目标点是否在视野范围内 + /// + public bool IsInViewRange(Vector2 target) { - base._PhysicsProcess(delta); - - if (_navigationAgent2D.IsNavigationFinished()) + var isForward = IsPositionInForward(target); + if (isForward) { - return; - } - var playerGlobalPosition = GameApplication.Instance.Room.Player.GlobalPosition; - //临时处理, 让敌人跟随玩家 - if (_navigationUpdateTimer <= 0) - { - _navigationUpdateTimer = 0.2f; - if (_navigationAgent2D.GetTargetLocation() != playerGlobalPosition) + if (GlobalPosition.DistanceSquaredTo(target) <= ViewRange * ViewRange) //没有超出视野半径 { - _navigationAgent2D.SetTargetLocation(playerGlobalPosition); + return true; } } - else - { - _navigationUpdateTimer -= delta; - } - - var nextPos = _navigationAgent2D.GetNextLocation(); - LookTargetPosition(playerGlobalPosition); - AnimatedSprite.Animation = AnimatorNames.Run; - Velocity = (nextPos - GlobalPosition - _navigationPoint.Position).Normalized() * MoveSpeed; - CalcMove(delta); + + return false; } - public override void _Draw() + /// + /// 调用视野检测, 如果被墙壁和其它物体遮挡, 则返回被挡住视野的物体对象, 视野无阻则返回 null + /// + public Godot.Object TestViewRayCast(Vector2 target) { - if (GameApplication.Instance.Debug) - { - // if (PathSign != null) - // { - // DrawLine(Vector2.Zero,ToLocal(PathSign.GlobalPosition), Colors.Red); - // } - } + ViewRay.Enabled = true; + ViewRay.CastTo = ViewRay.ToLocal(target); + ViewRay.ForceRaycastUpdate(); + var collObj = ViewRay.GetCollider(); + ViewRay.Enabled = false; + return collObj; } } diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs index 5733bec..45b13c2 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AINormalState.cs @@ -1,4 +1,6 @@  +using Godot; + /// /// AI 正常状态 /// @@ -9,7 +11,7 @@ public StateController StateController { get; set; } public void Enter(AIStateEnum prev, params object[] args) { - Master.PathSign.Enable = false; + } public void PhysicsProcess(float delta) @@ -18,30 +20,12 @@ var player = GameApplication.Instance.Room.Player; //玩家中心点坐标 var playerPos = player.MountPoint.GlobalPosition; - - //玩家是否在前方 - var isForward = Master.IsPositionInForward(playerPos); - - if (isForward) //脸朝向玩家 - { - if (Master.GlobalPosition.DistanceSquaredTo(playerPos) <= Master.ViewRange * Master.ViewRange) //没有超出视野半径 - { - //射线检测墙体 - Master.ViewRay.Enabled = true; - var localPos = Master.ViewRay.ToLocal(playerPos); - Master.ViewRay.CastTo = localPos; - Master.ViewRay.ForceRaycastUpdate(); - - if (!Master.ViewRay.IsColliding()) //视野无阻 - { - //发现玩家, 切换状态 - StateController.ChangeStateLate(AIStateEnum.AITailAfter); - } - Master.ViewRay.Enabled = false; - } + if (Master.IsInViewRange(playerPos) && Master.TestViewRayCast(playerPos) == null) + { + //发现玩家 + StateController.ChangeStateLate(AIStateEnum.AITailAfter); } - } public bool CanChangeState(AIStateEnum next) diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs index 9a4efca..368947d 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AIProbeState.cs @@ -14,7 +14,7 @@ public void PhysicsProcess(float delta) { - + } public bool CanChangeState(AIStateEnum next) diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs index deeb1ad..0c1417e 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs @@ -9,13 +9,11 @@ public StateController StateController { get; set; } public void Enter(AIStateEnum prev, params object[] args) { - //临时处理 - //Master.PathSign.Enable = true; + } public void PhysicsProcess(float delta) { - var master = Master; }