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;
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)
{

View File

@ -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<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)
{
var instance = scene.Instantiate();