40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using Chickensoft.Introspection;
|
|
using Godot;
|
|
|
|
namespace GameJamDungeon
|
|
{
|
|
public partial class EnemyLogic
|
|
{
|
|
public partial record State
|
|
{
|
|
[Meta, Id("enemy_logic_state_followplayer")]
|
|
public partial record FollowPlayer : Alive, IGet<Input.PhysicsTick>, IGet<Input.LostPlayer>
|
|
{
|
|
public Transition On(in Input.PhysicsTick input)
|
|
{
|
|
var delta = input.Delta;
|
|
var enemy = Get<IEnemy>();
|
|
var gameRepo = Get<IGameRepo>();
|
|
var target = gameRepo.PlayerGlobalPosition.Value;
|
|
enemy.NavAgent.TargetPosition = target;
|
|
var targetPosition = enemy.NavAgent.GetNextPathPosition();
|
|
|
|
enemy.Velocity = (targetPosition - enemy.GlobalTransform.Origin).Normalized() * 2f;
|
|
var lookAtDir = enemy.GlobalTransform.Origin - enemy.Velocity;
|
|
var lookAtPosition = new Vector3(lookAtDir.X, enemy.GlobalPosition.Y, lookAtDir.Z);
|
|
if (!enemy.Velocity.IsEqualApprox(Vector3.Zero) && !enemy.GlobalPosition.IsEqualApprox(lookAtPosition))
|
|
enemy.LookAt(lookAtPosition);
|
|
|
|
Output(new Output.MovementComputed());
|
|
return ToSelf();
|
|
}
|
|
|
|
public Transition On(in Input.LostPlayer input)
|
|
{
|
|
return To<Idle>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|