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

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;
}
}

View file

@ -48,6 +48,7 @@ public partial class LivingArmor : CharacterBody2D
private State _state;
private float _timeSinceState;
private AnimatedSprite2D _sprite;
private NavigationAgent2D _nav;
private bool _isLitUp;
//private readonly List<WeakRef> _bodiesInSight = new List<WeakRef>();
private readonly List<Node2D> _bodiesInSight = new List<Node2D>();
@ -58,6 +59,8 @@ public partial class LivingArmor : CharacterBody2D
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
_nav = (NavigationAgent2D)FindChild("NavigationAgent2D");
_nav.VelocityComputed += OnNavVelocityCompute;
_respawnPosition = Position;
CurrentState = State.Waiting;
var animationName = "side_walk";
@ -78,6 +81,7 @@ public partial class LivingArmor : CharacterBody2D
_sprite.Play(animationName);
_sprite.Stop();
}
public override void _Process(double delta)
@ -127,13 +131,15 @@ public partial class LivingArmor : CharacterBody2D
}
break;
}
if (_target is null)
{
_state = State.Waiting;
return;
}
var direction = (_target.Position - Position).Normalized();
Velocity = direction * MovingSpeed;
_nav.TargetPosition = _target.Position;
var direction = (_nav.GetNextPathPosition() - GlobalPosition).Normalized();
var animationName = "side_walk";
@ -147,8 +153,8 @@ public partial class LivingArmor : CharacterBody2D
_sprite.FlipH = Velocity.X < 0.001f && animationName == "side_walk";
_sprite.Play(animationName);
_nav.Velocity = direction * MovingSpeed;
MoveAndSlide();
//MoveAndCollide(direction);
break;
}
@ -252,5 +258,11 @@ public partial class LivingArmor : CharacterBody2D
Position = _respawnPosition;
// QueueFree(); // TODO
}
private void OnNavVelocityCompute(Vector2 safeVelocity)
{
Velocity = safeVelocity;
}
}

View file

@ -50,11 +50,14 @@ public partial class Wretched : CharacterBody2D
private State _state;
private float _timeSinceState;
private AnimatedSprite2D _sprite;
private NavigationAgent2D _nav;
private bool _isActivated;
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
_nav = (NavigationAgent2D)FindChild("NavigationAgent2D");
_nav.VelocityComputed += OnNavVelocityCompute;
CurrentState = State.Waiting;
var animationName = "NonActivatedSide";
switch (Facing)
@ -93,8 +96,8 @@ public partial class Wretched : 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();
var animationName = "ActivatedSide";
@ -108,6 +111,7 @@ public partial class Wretched : CharacterBody2D
_sprite.FlipH = Velocity.X < 0.001f && animationName == "ActivatedSide";
_sprite.Play(animationName);
_nav.Velocity = direction * MovingSpeed;
MoveAndSlide();
break;
}
@ -158,4 +162,9 @@ public partial class Wretched : CharacterBody2D
EmitSignal(SignalName.Killed);
QueueFree(); // TODO
}
private void OnNavVelocityCompute(Vector2 safeVelocity)
{
Velocity = safeVelocity;
}
}