Beispiel #1
0
        public override void Run(float delta)
        {
            var bot   = this.Node;
            var nodes = this.group.GetChildren();

            if (nodes.Count > 0)
            {
                Node2D closest = null;
                foreach (var node in nodes)
                {
                    if (node is T thing && !bot.World.Claimed.Contains(thing.GetInstanceId()))
                    {
                        if (closest == null)
                        {
                            closest = thing;
                            continue;
                        }

                        if (thing.GlobalPosition.DistanceTo(bot.GlobalPosition) < closest.GlobalPosition.DistanceTo(bot.GlobalPosition))
                        {
                            closest = thing;
                        }
                    }
                }

                if (closest != null)
                {
                    bot.Path = bot.World.GetNode <Map>("Map").GetPath(bot.Position, closest.Position).ToList();
                    bot.World.Claimed.Add(closest.GetInstanceId());
                    GD.Print($"FindNearest: Found a {typeof(T)}:{closest.Name}!");

                    this.Status = TaskStatus.Succeeded;
                }
            }

            if (this.Status != TaskStatus.Succeeded)
            {
                GD.Print($"FindNearest: Didn't find a {typeof(T)}!");
                this.Status = TaskStatus.Failed;
            }
        }