Big fix patch for enemy behaviors
This commit is contained in:
@@ -12,12 +12,12 @@ public abstract partial class Enemy2D : Enemy
|
||||
|
||||
[Node] private Area3D LineOfSight { get; set; } = default!;
|
||||
|
||||
[Node] private RayCast3D Raycast { get; set; } = default!;
|
||||
|
||||
public override IEnemyModelView EnemyModelView => _enemyModelView;
|
||||
|
||||
[Node] private EnemyModelView2D _enemyModelView { get; set; } = default!;
|
||||
|
||||
private Vector3 _previousPosition = Vector3.Zero;
|
||||
|
||||
public void OnEnterTree()
|
||||
{
|
||||
LineOfSight.BodyEntered += LineOfSight_BodyEntered;
|
||||
@@ -28,6 +28,19 @@ public abstract partial class Enemy2D : Enemy
|
||||
_enemyModelView.SetCurrentDirection(GlobalBasis, -_player.GlobalBasis.Z);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_enemyLogic.Value is EnemyLogic.State.FollowPlayer || _enemyLogic.Value is EnemyLogic.State.Patrolling)
|
||||
{
|
||||
var velocity = (GlobalPosition - _previousPosition) / (float)delta;
|
||||
if (velocity.IsZeroApprox())
|
||||
_enemyLogic.Input(new EnemyLogic.Input.Idle());
|
||||
else
|
||||
_enemyLogic.Input(new EnemyLogic.Input.Move());
|
||||
_previousPosition = GlobalPosition;
|
||||
}
|
||||
}
|
||||
|
||||
protected void PlayerDetector_BodyEntered(Node3D node)
|
||||
{
|
||||
if (node is IPlayer)
|
||||
@@ -53,18 +66,7 @@ public abstract partial class Enemy2D : Enemy
|
||||
|
||||
private void LineOfSight_BodyEntered(Node3D body)
|
||||
{
|
||||
var overlappingBodies = LineOfSight.GetOverlappingBodies();
|
||||
foreach (var _ in overlappingBodies)
|
||||
{
|
||||
if (Raycast.GlobalPosition != _player.GlobalPosition)
|
||||
Raycast.LookAt(_player.GlobalPosition, Vector3.Up);
|
||||
Raycast.ForceRaycastUpdate();
|
||||
if (Raycast.IsColliding())
|
||||
{
|
||||
var collider = Raycast.GetCollider();
|
||||
if (collider is IPlayer)
|
||||
_enemyLogic.Input(new EnemyLogic.Input.Alert());
|
||||
}
|
||||
}
|
||||
if (body is IPlayer)
|
||||
_enemyLogic.Input(new EnemyLogic.Input.Alert());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user