Basic pathfinding working for crying ghosts

This commit is contained in:
2026-01-17 23:37:33 -06:00
parent dd4ce5fcf4
commit 3a63bb6709
46 changed files with 691 additions and 34 deletions

View File

@@ -0,0 +1,78 @@
using Godot;
using System.Diagnostics.CodeAnalysis;
public partial class CryingGhost : CharacterBase
{
private AnimatedSprite2D _animatedSprite = default!;
private NavigationAgent2D _navigationAgent = default!;
private Area2D _detectionArea = default!;
private Timer _targetTimer = default!;
protected Node2D? Target { get; private set; }
[MemberNotNullWhen(true, nameof(Target))]
protected bool HasTarget => Target is not null;
public override void _Ready()
{
_animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
_detectionArea = GetNode<Area2D>("DetectionArea");
_targetTimer = GetNode<Timer>("TargetTimer");
_animatedSprite?.Play("idle");
_detectionArea.BodyEntered += OnBodyEntered;
_detectionArea.BodyExited += OnBodyExited;
_targetTimer.Timeout += OnTimeout;
_navigationAgent.VelocityComputed += OnVelocityComputed;
}
public override void _PhysicsProcess(double delta)
{
if (!HasTarget)
{
Velocity = Vector2.Zero;
}
else
{
var nextPosition = _navigationAgent.GetNextPathPosition();
var direction = (nextPosition - GlobalPosition).Normalized();
_navigationAgent.Velocity = direction * Speed;
}
MoveAndSlide();
}
void OnBodyEntered(Node2D body)
{
if (body.IsInGroup("Player"))
{
Target = body;
_navigationAgent.TargetPosition = Target.GlobalPosition;
_targetTimer.Start();
}
}
void OnBodyExited(Node2D body)
{
if (body == Target)
{
Target = null;
_navigationAgent.TargetPosition = GlobalPosition;
_targetTimer.Stop();
}
}
void OnTimeout()
{
if (HasTarget)
{
_navigationAgent.TargetPosition = Target.GlobalPosition;
}
}
void OnVelocityComputed(Vector2 safeVelocity)
{
Velocity = safeVelocity;
}
}