private void Search(int StartX, int StartY, int TargetX, int TargetY) { foreach (var item in Map) { item.Close = false; item.Open = false; item.f = 0; item.h = 0; item.Parent = null; } openQueue.Clear(); YuoGrid startYuoGrid = Map[StartX, StartY]; YuoGrid endYuoGrid = Map[TargetX, TargetY]; Min = startYuoGrid; Open(startYuoGrid, null); //最大寻路次数,防止卡死 int MaxSearchNum = 100000; while (openQueue.Count > 0) { MaxSearchNum--; if (MaxSearchNum < 0) { Debug.LogError("溢出了"); return; } FindNeighbors(Min); Close(Min); if (endYuoGrid.Open) { return; } } }
private void ComputeJump(YuoGrid grid) { //只有墙体附近可以跳跃 if (grid.CanMove) { return; } for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { //去除掉自己 if (x == 0 && y == 0) { continue; } //斜方向 if (x != 0 && y != 0) { //如果斜方向的两个相邻直线方向可以通过 if (CanMove(grid.x + x, grid.y + y) && CanMove(grid.x + x, grid.y) && CanMove(grid.x, grid.y + y)) { SetJump(Map[grid.x + x, grid.y + y], x, y); } } } } }
private void Init() { //_Map = new int[50, 50]; MapSizeX = _Map.GetLength(0); MapSizeY = _Map.GetLength(1); // for (int x = 0; x < MapSizeX; x++) // { // for (int y = 0; y < MapSizeY; y++) // { // //if (x < MapSizeX - 2 && y == MapSizeY / 2) // if (x > 1 && y == MapSizeY / 2) // _Map[x, y] = 1; // else // _Map[x, y] = 0; // } // } Map = new YuoGrid[MapSizeX, MapSizeY]; for (int x = 0; x < _Map.GetLength(0); x++) { for (int y = 0; y < _Map.GetLength(1); y++) { var grid = new YuoGrid(); grid.x = x; grid.y = y; grid.CanMove = _Map[x, y] == 0; Map[x, y] = grid; } } }
public void InitGrid(YuoGrid parent) { if (parent == null) { return; } IsMoved = true; Parent = parent; if (parent == this) { this.Parent = null; } int wi = Mathf.Abs(x - parent.x); int he = Mathf.Abs(y - parent.y); if (wi == 1 && he == 1) { g = parent.g + 14; } else { g = parent.g + 10; } h = (int)Mathf.Sqrt(wi * wi + he * he) * 10; f = g + h; }
private void SetJump(YuoGrid grid, int x, int y) { grid.CanJump = true; if (!JumpPoints.Contains(grid)) { //print("添加了一个跳点"); JumpPoints.Add(grid); grid.sr.color = Color.green; } grid.jump.AddDir(x == -1 ? Dir.Right : Dir.Left); grid.jump.AddDir(y == -1 ? Dir.Up : Dir.Down); foreach (var item in grid.jump.Dirs) { if (item == Vector2Int.up) { grid.sr.transform.GetChild(2).gameObject.SetActive(true); } if (item == Vector2Int.down) { grid.sr.transform.GetChild(3).gameObject.SetActive(true); } if (item == Vector2Int.left) { grid.sr.transform.GetChild(0).gameObject.SetActive(true); } if (item == Vector2Int.right) { grid.sr.transform.GetChild(1).gameObject.SetActive(true); } } }
private void DebugConnect(YuoGrid start, YuoGrid end) { start.sr.color = Color.yellow; var offset = Random.Range(0, 0f); Debug.DrawLine(start.sr.transform.position + Vector3.one * offset, end.sr.transform.position + Vector3.one * offset, Color.white, 9999); }
public void Close(YuoGrid grid) { if (grid.Open == true) { openQueue.Dequeue(); if (openQueue.Count > 0) { Min = openQueue.Peek(); } grid.Close = true; } }
private static YuoGrid DrawElement(Rect rect, YuoGrid value) { if (!value.CanMove) { UnityEditor.EditorGUI.DrawRect(rect, new Color(0, 1, 1)); } if (value.Close) { UnityEditor.EditorGUI.DrawRect(rect, new Color(1, 0, 0)); UnityEditor.EditorGUI.TextArea(rect.SetHeight(20), (value.f).ToString()); } return(value); }
public void Open(YuoGrid grid, YuoGrid parent) { if (grid.Open == false) { openQueue.Enqueue(grid); } grid.OpenGrid(parent); grid.Open = true; grid.Close = false; if (grid.f < Min.f) { Min = grid; } }
private static YuoGrid DrawElement(Rect rect, YuoGrid value) { if (!value.CanMove) { UnityEditor.EditorGUI.DrawRect(rect, new Color(1, 0, 0)); } if (value.IsMoved) { UnityEditor.EditorGUI.DrawRect(rect, new Color(1, 1, 0)); } if (value.Tag) { UnityEditor.EditorGUI.DrawRect(rect, new Color(1, 1, 1)); } return(value); }
public List <YuoGrid> Search(Vector2Int startPos, Vector2Int endPos) { Search(startPos.x, startPos.y, endPos.x, endPos.y); YuoGrid End = Map[MapSizeX - 1, MapSizeY - 1]; if (End.Parent == null) { return(null); } List <YuoGrid> path = new List <YuoGrid>(); while (End.Parent != null) { End = End.Parent; path.Add(End); } return(path); }
private void Start() { Init(); for (int i = 0; i < num; i++) { Search(5, 1, MapSizeX - 1, MapSizeY - 1); } YuoGrid End = Map[MapSizeX - 1, MapSizeY - 1]; List <YuoGrid> path = new List <YuoGrid>(); while (End.Parent != null) { End = End.Parent; path.Add(End); } if (End.Open) { } }
public void OpenGrid(YuoGrid parent) { if (parent == null) { return; } Parent = parent; int wi = Mathf.Abs(x - parent.x); int he = Mathf.Abs(y - parent.y); if (wi == 1 && he == 1) { g = parent.g + 14; } else { g = parent.g + 10; } h = (int)Mathf.Sqrt(wi * wi + he * he) * 10; f = g + h; }
/// <summary> /// 找到周围的点加到Openlist /// </summary> /// <param name="grid"></param> private void FindNeighbors(YuoGrid grid) { for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { //去除掉自己 if (x == 0 && y == 0) { continue; } //不能超出边界 if (!(grid.x + x).InRange(0, MapSizeX - 1) || !(grid.y + y).InRange(0, MapSizeY - 1)) { continue; } var gridTemp = Map[grid.x + x, grid.y + y]; //是否是墙体 if (!gridTemp.CanMove) { continue; } if (gridTemp.Open || gridTemp.Close) { continue; } if (x != y && x != -y) { Open(gridTemp, grid); } else if (true) { //Open(gridTemp); } } } }
private void Init() { MapSizeX = _Map.GetLength(0); MapSizeY = _Map.GetLength(1); Map = new YuoGrid[MapSizeX, MapSizeY]; for (int x = 0; x < _Map.GetLength(0); x++) { for (int y = 0; y < _Map.GetLength(1); y++) { var grid = new YuoGrid(); grid.x = x; grid.y = y; grid.CanMove = _Map[x, y] == 0; Map[x, y] = grid; var go = Instantiate(tempGo, transform); go.transform.localPosition = new Vector3(x, y); var sr = go.GetComponent <SpriteRenderer>(); grid.sr = sr; sr.color = !grid.CanMove ? Color.red : Color.black; go.AddComponent <GridTest>().grid = grid; } } JumpPoints = new List <YuoGrid>(); }
public void Add(YuoGrid grid) { Path.Add(grid); grid.IsMoved = true; }
public async void Connect() { foreach (var item in JumpPoints) { //foreach (var dir in item.jump.Dirs) foreach (var dir in Dirs) { var Dir = dir; Dir *= -1; bool ThisDirIsOver = false; int dirNum = 1; //上下方向的就往左右找,左右方向的就往上下找 Vector2Int nextDir1; Vector2Int nextDir2; if (Dir.x == 0) { nextDir1 = Vector2Int.left; nextDir2 = Vector2Int.right; } else { nextDir1 = Vector2Int.up; nextDir2 = Vector2Int.down; } if (true) { List <Branch> branches = new List <Branch>(); Branch MainBranch = new Branch() { dir = Dir, pos = new Vector2Int(item.x, item.y) }; bool over = false; while (!over) { YuoGrid grid = null; if (!MainBranch.Stop) { grid = GetGrid(MainBranch.Next()); if (grid == null) { MainBranch.Stop = true; } } if (!MainBranch.Stop) { if (!grid.CanMove) { MainBranch.Stop = true; } else { branches.Add(new Branch() { dir = nextDir1, pos = MainBranch.pos }); branches.Add(new Branch() { dir = nextDir2, pos = MainBranch.pos }); } if (grid.CanJump) { await YuoWait.WaitTimeAsync(0.1f); DebugConnect(item, grid); over = true; } } else { bool JumpOut = true; foreach (var b in branches) { if (!b.Stop) { JumpOut = false; } } if (JumpOut) { break; } } foreach (var b in branches) { if (b.Stop) { continue; } var _grid = GetGrid(b.Next()); if (_grid == null) { b.Stop = true; continue; } if (!_grid.CanMove) { b.Stop = true; } if (_grid.CanJump) { await YuoWait.WaitTimeAsync(0.1f); DebugConnect(item, _grid); over = true; } } } } else { while (!ThisDirIsOver) { //await YuoWait.WaitTimeAsync(0.01f); //往一个可通行方向一直找 Vector2Int startPos = Dir * dirNum + new Vector2Int(item.x, item.y); //print(startPos); var grid = GetGrid(startPos); if (grid == null) { break; } if (!grid.CanMove) { break; } if (grid.CanJump) { ThisDirIsOver = true; await YuoWait.WaitTimeAsync(0.1f); DebugConnect(item, grid); break; } //如果此方向这个位置没有,就开始往两边找 int posNum = 1; while (true) { grid = GetGrid(startPos + posNum * nextDir1); if (grid == null) { break; } if (!grid.CanMove) { break; } if (grid.CanJump) { ThisDirIsOver = true; await YuoWait.WaitTimeAsync(0.1f); DebugConnect(item, grid); break; } posNum++; } posNum = 1; while (true) { grid = GetGrid(startPos + posNum * nextDir2); if (grid == null) { break; } if (!grid.CanMove) { break; } if (grid.CanJump) { ThisDirIsOver = true; await YuoWait.WaitTimeAsync(0.1f); DebugConnect(item, grid); break; } posNum++; } dirNum++; } } } } }