diff --git a/Characters/NPC.cs b/Characters/NPC.cs index a5806a3..f3f3d6b 100644 --- a/Characters/NPC.cs +++ b/Characters/NPC.cs @@ -104,27 +104,25 @@ public partial class NPC : Character { float bestScore = float.MaxValue; 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; - if (isFriendly || character.Health <= 0) - { - continue; - } + continue; + } - float score = 0; - score -= Position.DistanceTo(character.Position); + float score = 0; + score += GlobalPosition.DistanceSquaredTo(character.GlobalPosition); - if (score < bestScore) - { - bestScore = score; - bestChar = character; - } + if (score < bestScore) + { + bestScore = score; + bestChar = character; } } + return bestChar; } @@ -147,7 +145,7 @@ public partial class NPC : Character } float score = 0; - score -= Position.DistanceTo(character.Position); + score -= Position.DistanceSquaredTo(character.Position); if (score < bestScore) { diff --git a/Scenes/Map.cs b/Scenes/Map.cs index 8f6916e..50136d1 100644 --- a/Scenes/Map.cs +++ b/Scenes/Map.cs @@ -1,5 +1,6 @@ using Godot; using SupaLidlGame.Extensions; +using System.Collections.Generic; namespace SupaLidlGame.Scenes; @@ -71,6 +72,17 @@ public partial class Map : TileMap GetNode("/root/AudioManager").PlayBackground(Music); } + public IEnumerable GetCharacters() + { + foreach (var child in Entities.GetChildren()) + { + if (child is Characters.Character c) + { + yield return c; + } + } + } + public Node SpawnEntity(PackedScene scene) { var instance = scene.Instantiate();