#region
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
using Debug = UnityEngine.Debug;
#endregion
public class VAStar : MonoBehaviour
{
#region Fields
public int GridHeight;
public int GridWidth;
private List<VGrid> closedList;
private Dictionary<int, VGrid> gridDictionary;
private List<GameObject> obstacleList;
private List<VGrid> openList;
private System.Diagnostics.Stopwatch watch;
public delegate void OnPathComplete ( List<Vector3> posList );
public OnPathComplete onPathComplete;
#endregion
// Use this for initialization
#region Public Methods and Operators
public void StartFindPath ( Vector3 startPos, Vector3 endPos, OnPathComplete delegateComplete )
{
watch.Start();
this.onPathComplete = delegateComplete;
int startGridId = this.GetGridIdByPos(startPos);
int endGridId = this.GetGridIdByPos(endPos);
if (startGridId == -1 || endGridId == -1)
{
return;
}
this.openList.Add(this.gridDictionary[startGridId]);
this.Test(startGridId, endGridId);
}
#endregion
#region Methods
private int GetColumnCount ()
{
return Screen.width / this.GridWidth;
}
private Vector3 GetGridCenterById ( int gridId )
{
//Like This:
//8 9 10 11
//4 5 6 7
//0 1 2 3
return new Vector3(
gridId % this.GetColumnCount() * this.GridWidth + (this.GridWidth / 2),
gridId / this.GetColumnCount() * this.GridHeight + (this.GridHeight / 2),
0);
}
private int GetGridIdByPos ( Vector3 pos )
{
if (this.gridDictionary != null && this.gridDictionary.Values.Count != 0)
{
foreach (KeyValuePair<int, VGrid> kv in this.gridDictionary)
{
Rect gridRect = new Rect(
kv.Value.Center.x - this.GridWidth / 2,
kv.Value.Center.y - this.GridHeight / 2,
this.GridWidth,
this.GridHeight);
if (gridRect.Contains(pos))
{
return kv.Key;
}
}
}
return -1;
}
private int GetGridIdByRowAndColumn ( int rowId, int columnId )
{
if (rowId < 0 ||
A*算法的C#实现(纯代码)
最新推荐文章于 2023-06-06 20:04:06 发布