Frame/Assets/Scripts/Test.cs

108 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Game.Pathfinding;
using JetBrains.Annotations;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Game
{
public class Test : MonoBehaviour
{
// public Transform tr;
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
// var go = Game.resourceManager.LoadGameObjectSync(@"player");
// // var go = ResourceManager.Instance.LoadGameObject_MMM(@"hh");
// go.name = "123";
}
if (Input.GetKeyDown(KeyCode.D))
{
//
// ResourceManager.Instance.Release("hh");
//
// CommonHelper.FindChildDeep<Image>(tr, "imag");
// Debug.Log("找到此组件l");
//
// ResourceManager.Instance.LoadSceneAsync("Game", LoadSceneMode.Additive);
//
// EventManager.Instance.FireNow(this, new LoadingGameSceneFinishEventArgs(true));
//
//var value = this.GetType().ToString().Split(".");
// Debug.Log(value[^1]);
//
// var values = Enum.GetValues(typeof(ProcedureType));
// foreach (var value in values)
// {
// UnityEngine.Debug.Log( value.ToString());
// }
this.AAA(this.startTr.position, this.endTr.position);
}
// RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, Vector2.up);
//
// // If it hits something...
// if (hit.collider != null)
// {
// Debug.Log($"碰撞的是 {hit.collider.name} 鼠标的点在 {hit.point}");
// }
//
// Debug.Log(hit.point);
}
public Transform startTr;
public Transform endTr;
void AAA(Vector2 start, Vector2 end)
{
var gameGlobalConfig = GameObject.FindObjectOfType<GameGlobalConfig>();
BFS<WayPoint> bfs = new BFS<WayPoint>(CreateGraph(gameGlobalConfig));
var beginNode = GetNode(bfs, start);
var endNode = GetNode(bfs, end);
Debug.Log($"start is {beginNode.index}, end is {endNode.index}");
// 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;
}
}
}