basic capture functionality
This commit is contained in:
parent
246f3c0840
commit
c141e6a169
20 changed files with 430 additions and 14 deletions
52
scripts/world/PlayerInteractor.cs
Normal file
52
scripts/world/PlayerInteractor.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue