Улучшен поиск путей

This commit is contained in:
Евгений Титаренко 2023-08-21 21:27:22 +03:00
parent 3e60215e11
commit 26cc09241f
8 changed files with 107 additions and 18 deletions

View file

@ -17,11 +17,14 @@ public partial class Claw : CharacterBody2D
private float _stateTimeout = 0;
private bool _isPlayerNearBy = false;
private AnimatedSprite2D _sprite;
private NavigationAgent2D _nav;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
_nav = (NavigationAgent2D)FindChild("NavigationAgent2D");
_nav.VelocityComputed += OnNavVelocityCompute;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -38,10 +41,11 @@ public partial class Claw : CharacterBody2D
break;
case State.Moving:
var direction = (Player.Instance.Position - Position).Normalized();
Velocity = direction * MovingSpeed;
_nav.TargetPosition = Player.Instance.Position;
var direction = (_nav.GetNextPathPosition() - Position).Normalized();
_sprite.FlipH = Velocity.X < 0.001f;
_sprite.Play("default");
_nav.Velocity = direction * MovingSpeed;
MoveAndSlide();
break;
case State.Prepare:
@ -87,4 +91,9 @@ public partial class Claw : CharacterBody2D
GD.Print("Boss enabled");
_state = State.Moving;
}
private void OnNavVelocityCompute(Vector2 safeVelocity)
{
Velocity = safeVelocity;
}
}