Add `Map.GetCharacters()`

refactor
John Montagu, the 4th Earl of Sandvich 2024-06-06 07:59:29 -07:00
parent 4c780da552
commit a67dedf3f9
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
2 changed files with 26 additions and 16 deletions

View File

@ -104,27 +104,25 @@ public partial class NPC : Character
{ {
float bestScore = float.MaxValue; float bestScore = float.MaxValue;
Character bestChar = null; Character bestChar = null;
// NOTE: this relies on all Characters being under the Entities node
foreach (Node node in GetParent().GetChildren()) foreach (var character in World.Instance.CurrentMap.GetCharacters())
{ {
if (node is Character character) bool isFriendly = ((IFaction)character).AlignsWith(this);
if (isFriendly || !character.IsAlive)
{ {
bool isFriendly = character.Faction == Faction; continue;
if (isFriendly || character.Health <= 0) }
{
continue;
}
float score = 0; float score = 0;
score -= Position.DistanceTo(character.Position); score += GlobalPosition.DistanceSquaredTo(character.GlobalPosition);
if (score < bestScore) if (score < bestScore)
{ {
bestScore = score; bestScore = score;
bestChar = character; bestChar = character;
}
} }
} }
return bestChar; return bestChar;
} }
@ -147,7 +145,7 @@ public partial class NPC : Character
} }
float score = 0; float score = 0;
score -= Position.DistanceTo(character.Position); score -= Position.DistanceSquaredTo(character.Position);
if (score < bestScore) if (score < bestScore)
{ {

View File

@ -1,5 +1,6 @@
using Godot; using Godot;
using SupaLidlGame.Extensions; using SupaLidlGame.Extensions;
using System.Collections.Generic;
namespace SupaLidlGame.Scenes; namespace SupaLidlGame.Scenes;
@ -71,6 +72,17 @@ public partial class Map : TileMap
GetNode<Audio.AudioManager>("/root/AudioManager").PlayBackground(Music); GetNode<Audio.AudioManager>("/root/AudioManager").PlayBackground(Music);
} }
public IEnumerable<Characters.Character> GetCharacters()
{
foreach (var child in Entities.GetChildren())
{
if (child is Characters.Character c)
{
yield return c;
}
}
}
public Node SpawnEntity(PackedScene scene) public Node SpawnEntity(PackedScene scene)
{ {
var instance = scene.Instantiate(); var instance = scene.Instantiate();