66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using Game.Pathfinding;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
|
|
namespace Game
|
|
{
|
|
[Procedure(ProcedureType.GameSceneLogicProcedure)]
|
|
class GameSceneLogicProcedure : ProcedureBase
|
|
{
|
|
public override void OnEnter()
|
|
{
|
|
base.OnEnter();
|
|
|
|
// UniTask.Create(async () =>
|
|
// {
|
|
// await ResourceManager.Instance.LoadSceneAsync(SceneType.Game.ToString());
|
|
// EventManager.Instance.FireNow(this,new LoadingGameSceneFinishEventArgs(true));
|
|
// });
|
|
|
|
var gameGlobalConfig = GameObject.FindObjectOfType<GameGlobalConfig>();
|
|
BFS<WayPoint> bfs = new BFS<WayPoint>(CreateGraph(gameGlobalConfig));
|
|
var beginNode = GetNode(bfs, new Vector2(-5.6f,-14.6f));
|
|
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))}");
|
|
}
|
|
|
|
[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;
|
|
}
|
|
|
|
public override void OnLeave()
|
|
{
|
|
base.OnLeave();
|
|
// UIManager.Instance.ShowUI(UIType.GameSceneMainUI);
|
|
// ProcedureManager.Instance.ChangeProcedure(ProcedureType.GameSceneLogicProcedure);
|
|
}
|
|
}
|
|
} |