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

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

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