using System.Collections.Generic; using System.Linq; using Game.Player; using JetBrains.Annotations; using UnityEngine; namespace Game.Pathfinding; public interface IBFSManager { List FindPath(Vector2 startPos, Vector2 endPos); List FindPath(Vector2 endPos); Node GetNode(Vector2 position); // void Test(Vector2 position); } public class BFSManager : ManagerBase, IBFSManager { private BFS bfs; private IPlayerManager _playerManager; protected override void OnInit() { base.OnInit(); // var gameGlobalConfig = GameObject.FindObjectOfType(); // bfs = new BFS(this.CreateGraph(gameGlobalConfig)); // var beginNode = GetNode(bfs, new Vector2(-29f, -16.5f)); // var endNode = GetNode(bfs, new Vector2(-15.8f, 44.3f)); // var findPath = bfs.FindPath(beginNode, endNode); // var wayPoints = new List(); // findPath.GetDatas(wayPoints); // Debug.Log($"Indices:{string.Join(',', findPath.GetNodes().Select(x => x.index))}"); // Debug.Log($"Positions:{string.Join(',', findPath.GetNodes().Select(x => x.data.position))}"); this._playerManager = Game.playerManager; } protected override void OnUpdate(float dateTime) { base.OnUpdate(dateTime); if (this.bfs == null) { var gameGlobalConfig = GameObject.FindObjectOfType(); bfs = new BFS(this.CreateGraph(gameGlobalConfig)); } } public List FindPath(Vector2 startPos, Vector2 endPos) { var beginNode = GetNode(bfs, startPos); var endNode = GetNode(bfs, endPos); var findPath = bfs.FindPath(beginNode, endNode); var wayPoints = new List(); findPath.GetDatas(wayPoints); return wayPoints; } public List FindPath(Vector2 endPos) { var player = this._playerManager.currentPlayer; var pos = player.self.transform.position; var startPos = new Vector2(pos.x, pos.y); var beginNode = GetNode(bfs, startPos); var endNode = GetNode(bfs, endPos); // UnityEngine.Debug.Log($"frome {beginNode.index} to {endNode.index} "); var findPath = bfs.FindPath(beginNode, endNode); var wayPoints = new List(); findPath.GetDatas(wayPoints); return wayPoints; } // public void Test(Vector2 endPos) // { // var player = this._playerManager.currentPlayer; // var pos = player.self.transform.position; // var startPos = new Vector2(pos.x, pos.y); // var beginNode = GetNode(bfs, startPos); // var endNode = GetNode(bfs, endPos); //// UnityEngine.Debug.Log($"frome {beginNode.index} to {endNode.index} "); // } [CanBeNull] public Node GetNode(Vector2 position) { float distance = float.MaxValue; Node targetStartNode = null; bfs.GetNode(node => { var magnitude = ((Vector2)node.data.position - position).magnitude; if (magnitude < distance) { distance = magnitude; targetStartNode = node; } return false; }); return targetStartNode; } [CanBeNull] private static Node GetNode(BFS bfs, Vector2 position) { float distance = float.MaxValue; Node targetStartNode = null; bfs.GetNode(node => { var magnitude = ((Vector2)node.data.position - position).magnitude; if (magnitude < distance) { distance = magnitude; targetStartNode = node; } return false; }); return targetStartNode; } private Graph CreateGraph(GameGlobalConfig gameGlobalConfig) { var graph = new WayPointGraph(); graph.Initialize(gameGlobalConfig.nodeMaps); return graph; } }