52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
using Godot;
|
|
using System.Collections.Generic;
|
|
|
|
public partial class PlayerInteractor : Area2D
|
|
{
|
|
private readonly List<Creature> _nearbyCreatures = new();
|
|
private GameRoot _gameRoot = null!;
|
|
|
|
public override void _Ready()
|
|
{
|
|
AreaEntered += OnAreaEntered;
|
|
AreaExited += OnAreaExited;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (!Input.IsActionJustPressed("interact"))
|
|
return;
|
|
|
|
var creature = GetClosestCreature();
|
|
if (creature == null) return;
|
|
|
|
// Ask GameRoot to start capture
|
|
GameRoot.Instance.RequestCapture(creature);
|
|
}
|
|
|
|
private void OnAreaEntered(Area2D area)
|
|
{
|
|
if (area is Creature c && !c.IsCaptured)
|
|
_nearbyCreatures.Add(c);
|
|
}
|
|
|
|
private void OnAreaExited(Area2D area)
|
|
{
|
|
if (area is Creature c)
|
|
_nearbyCreatures.Remove(c);
|
|
}
|
|
|
|
private Creature? GetClosestCreature()
|
|
{
|
|
Creature? best = null;
|
|
float bestDist = float.MaxValue;
|
|
|
|
foreach (var c in _nearbyCreatures)
|
|
{
|
|
if (!IsInstanceValid(c)) continue;
|
|
float d = GlobalPosition.DistanceSquaredTo(c.GlobalPosition);
|
|
if (d < bestDist) { bestDist = d; best = c; }
|
|
}
|
|
return best;
|
|
}
|
|
}
|