IndieGame/client/Assets/Scripts/System/AStar/AStarTest.cs

244 lines
7.3 KiB
C#
Raw Permalink Normal View History

2024-10-11 10:12:15 +08:00

using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Ether
{
public class AStarTest : SerializedMonoBehaviour
{
public GameObject gridList;
public GameObject gridItemTemp;
public Sprite normalGrid;
public Sprite startGrid;
public Sprite obstacleGrid;
public Sprite targetGrid;
public Sprite pathGrid;
public int width = 14;
public int height = 9;
public ButtonEx startBtn;
public ButtonEx targetBtn;
public ButtonEx obstacleBtn;
public ButtonEx buildBtn;
public ButtonEx resetBtn;
private ItemData start;
private ItemData target;
private AStar astar;
List<Vector2Int> obstacleList = new List<Vector2Int>();
ClickType curClickType = ClickType.obstacle;
private void Start()
{
gridItemTemp.SetActive(false);
GenerateAstarGrid();
startBtn.OnClick.AddListener(() =>
{
curClickType = ClickType.start;
});
targetBtn.OnClick.AddListener(() =>
{
curClickType = ClickType.target;
});
obstacleBtn.OnClick.AddListener(() =>
{
curClickType = ClickType.obstacle;
});
buildBtn.OnClick.AddListener(() =>
{
if (astar == null)
{
astar = new AStar(width, height, obstacleList);
}
else
{
astar.Reset(width, height, obstacleList);
}
foreach (var itemData in itemList)
{
if (itemData.isPathPos)
{
itemData.img.sprite = normalGrid;
}
}
Stack<Vector2Int> stepList = new Stack<Vector2Int>();
astar.BuildPath(start.pos, target.pos, stepList);
if (stepList.Count > 0)
{
foreach (var step in stepList)
{
foreach (var itemData in itemList)
{
if (itemData.pos == step)
{
if (itemData.pos != start.pos && itemData.pos != target.pos)
{
itemData.isPathPos = true;
itemData.img.sprite = pathGrid;
}
}
}
}
}
});
resetBtn.OnClick.AddListener(() =>
{
GenerateAstarGrid();
});
}
enum ClickType
{
start,
target,
obstacle,
}
class ItemData
{
public GameObject item;
public Image img;
public Vector2Int pos;
public bool isPathPos;
}
List<ItemData> itemList = new List<ItemData>();
private void GenerateAstarGrid()
{
foreach (var itemData in itemList)
{
itemData.item.SetActive(false);
}
gridList.SetSize(gridItemTemp.GetSize() * new Vector2(width, height));
int index = 0;
for (int i = 0; i < height; i++)
{
for (int k = 0; k < width; k++)
{
ItemData itemData;
if (index < itemList.Count)
{
itemData = itemList[index];
itemData.pos = new Vector2Int(k, i);
itemData.isPathPos = false;
}
else
{
GameObject item = Instantiate(gridItemTemp, gridList.transform);
item.name = index.ToString();
itemData = new ItemData()
{
item = item,
img = item.GetComponent<Image>(),
pos = new Vector2Int(k, i)
};
itemList.Add(itemData);
}
itemData.item.SetActive(true);
itemData.img.sprite = normalGrid;
ButtonEx btn = itemData.item.GetComponent<ButtonEx>();
btn.OnClick.RemoveAllListeners();
btn.OnClick.AddListener(() =>
{
Debug.Log("点击了pos:" + itemData.pos);
switch (curClickType)
{
case ClickType.start:
if (start != null)
{
start.img.sprite = normalGrid;
}
start = itemData;
start.img.sprite = startGrid;
break;
case ClickType.target:
if (target != null)
{
target.img.sprite = normalGrid;
}
target = itemData;
target.img.sprite = targetGrid;
break;
case ClickType.obstacle:
if (obstacleList.Contains(itemData.pos))
{
obstacleList.Remove(itemData.pos);
itemData.img.sprite = normalGrid;
}
else
{
obstacleList.Add(itemData.pos);
itemData.img.sprite = obstacleGrid;
}
break;
default:
break;
}
});
index++;
}
}
}
//[Button("开始寻路显示")]
//public void StartAStar()
//{
// Stack<Vector2Int> stepList = new Stack<Vector2Int>();
// astar.BuildPath(start, target, stepList);
// if (stepList.Count > 0)
// {
// foreach (var step in stepList)
// {
// displayTileMap.SetTile((Vector3Int)(step), displayTile);
// }
// }
//}
//private void OnDrawGizmos()
//{
// Gizmos.color = Color.red;
// for (int i = 0; i <= width; i++)
// {
// Gizmos.DrawLine(new Vector3(0, i, 0), new Vector3(height, i, 0));
// }
// for (int i = 0; i <= height; i++)
// {
// Gizmos.DrawLine(new Vector3(i, 0, 0), new Vector3(i, width, 0));
// }
//}
}
}