using Godot; using System; public partial class PatrolState : State { [Export] public float Speed = 40f; [Export] public float visibilityRange = 50f; [Export] public Node2D[] waypoints; // Waypoints som der patruljeres imellem. [Export] public CharacterBody2D player; public int currentWaypointIndex = 0; // Det nuværende waypoint public Vector2 targetPosition; public override void Ready() { targetPosition = waypoints[currentWaypointIndex].GlobalPosition; GD.Print("PatrolState ready."); } public override void Update(float delta) { fsm.npc.Velocity = fsm.npc.GlobalPosition.DirectionTo(targetPosition) * Speed; // Er vi nået til vores nuværende Waypoint? if( fsm.npc.GlobalPosition.DistanceTo( targetPosition ) < 1.5f ){ currentWaypointIndex++; if (currentWaypointIndex > waypoints.Length - 1) { currentWaypointIndex = 0; } targetPosition = waypoints[currentWaypointIndex].GlobalPosition; } // Er vi inde for visibility range af Player? if (fsm.npc.GlobalPosition.DistanceTo(player.GlobalPosition) < this.visibilityRange) { GD.Print("Skift til ChaseState"); fsm.TransitionTo("ChaseState"); } fsm.npc.MoveAndSlide(); } }