Frame/Assets/Scripts/BFS/BFSManager.cs

127 lines
4.0 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Game.Player;
using JetBrains.Annotations;
using UnityEngine;
namespace Game.Pathfinding;
public interface IBFSManager
{
List<WayPoint> FindPath(Vector2 startPos, Vector2 endPos);
List<WayPoint> FindPath(Vector2 endPos);
Node<WayPoint> GetNode(Vector2 position);
void Test(Vector2 position);
}
public class BFSManager : ManagerBase, IBFSManager
{
private BFS<WayPoint> bfs;
private IPlayerManager _playerManager;
protected override void OnInit()
{
base.OnInit();
// var gameGlobalConfig = GameObject.FindObjectOfType<GameGlobalConfig>();
// bfs = new BFS<WayPoint>(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<WayPoint>();
// 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<GameGlobalConfig>();
bfs = new BFS<WayPoint>(this.CreateGraph(gameGlobalConfig));
}
}
public List<WayPoint> 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<WayPoint>();
findPath.GetDatas(wayPoints);
return wayPoints;
}
public List<WayPoint> 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<WayPoint>();
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<WayPoint> GetNode(Vector2 position)
{
float distance = float.MaxValue;
Node<WayPoint> 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<WayPoint> GetNode(BFS<WayPoint> bfs, Vector2 position)
{
float distance = float.MaxValue;
Node<WayPoint> 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<WayPoint> CreateGraph(GameGlobalConfig gameGlobalConfig)
{
var graph = new WayPointGraph();
graph.Initialize(gameGlobalConfig.nodeMaps);
return graph;
}
}