void Start() { map = new SearchMapNode[MapManager.getInstance().MAP_HEIGHT][]; for (int i = 0; i < MapManager.getInstance().MAP_HEIGHT; i++) { map [i] = new SearchMapNode[MapManager.getInstance().MAP_WIDTH]; for (int j = 0; j < MapManager.getInstance().MAP_WIDTH; j++) { map [i] [j] = new SearchMapNode(i, j, !MapManager.getInstance().staticBlocks[i][j]); } } setTarget(); //reSearchPath (); }
public override bool Equals(object obj) { if (obj == null) { return(false); } if ((obj.GetType().Equals(this.GetType())) == false) { return(false); } SearchMapNode temp = null; temp = (SearchMapNode)obj; return(this.row.Equals(temp.row) && this.col.Equals(temp.col)); }
public void reSearchPath() { Vector3Int startPos = MapManager.getInstance().tilemap.WorldToCell(transform.position); Vector3Int endPos = MapManager.getInstance().tilemap.WorldToCell(target.transform.position); SearchMapNode startNode = map [-startPos.y] [startPos.x]; SearchMapNode endNode = map [-endPos.y] [endPos.x]; if (search(startNode, endNode)) { movementCtrl.path = pathPos; movementCtrl.hasPath = true; } else { // movementCtrl.path = new Vector3(); movementCtrl.hasPath = false; } }
public void searchFixedPath(Vector2Int targetPos) { if (!findObcOnPath(targetPos)) { //Debug.Log ("无障碍"); enemyCtrl.path = new List <Vector3>(); enemyCtrl.path.Add(target.transform.position); enemyCtrl.hasPath = true; enemyCtrl.pathIdx = 0; return; } //Vector3Int startPos = MapManager.getInstance ().tilemap.WorldToCell (transform.position); //Vector3Int endPos = MapManager.getInstance ().tilemap.WorldToCell (targetPos); Vector2Int startPos = MapManager.getInstance().WorldToCell(new Vector2Int(owner.posXInt, owner.posYInt)); Vector2Int endPos = MapManager.getInstance().WorldToCell(targetPos); if (startPos.y >= 0 && startPos.y < map.Length && startPos.x >= 0 && startPos.x < map [0].Length && endPos.y >= 0 && endPos.y < map.Length && endPos.x >= 0 && endPos.x < map [0].Length) { } else { return; } SearchMapNode startNode = map [startPos.y] [startPos.x]; SearchMapNode endNode = map [endPos.y] [endPos.x]; if (search(startNode, endNode)) { enemyCtrl.path = pathPos; enemyCtrl.hasPath = true; enemyCtrl.pathIdx = 0; } else { enemyCtrl.path = new List <Vector3>(); enemyCtrl.hasPath = false; enemyCtrl.pathIdx = 0; } }
// Use this for initialization void Start() { map = new SearchMapNode[maxI][]; //images = new Image[maxI][]; for (int i = 0; i < maxI; i++) { map [i] = new SearchMapNode[maxJ]; //images [i] = new Image[maxJ]; for (int j = 0; j < maxJ; j++) { map [i] [j] = new SearchMapNode(i, j, true); // GameObject o = GameObject.Instantiate (image,UILayer); // o.transform.localPosition = new Vector3 (10*j,-10*i,0); // images [i] [j] = o.GetComponent<Image> (); // UIEventListener mListener = o.AddComponent<UIEventListener> (); // int finalI = i; // int finalJ = j; // mListener.ClickEvent += delegate(GameObject gb,PointerEventData eventData) { // // map[finalI][finalJ].walkable = !map[finalI][finalJ].walkable; // o.GetComponent<Image>().color = map[finalI][finalJ].walkable ? Color.white : Color.red; // }; } } // UIEventListener m2Listener = btn.gameObject.AddComponent<UIEventListener> (); // // m2Listener.ClickEvent += delegate(GameObject gb,PointerEventData eventData) { // foreach(Transform child in dotLayer){ // GameObject.Destroy(child.gameObject); // } // search(); // }; //Debug.DrawLine(new Vector3(0,0,0), new Vector3(100,100,100),Color.yellow,100000); //findObcOnPath (new Vector2(0.4f,2.6f),new Vector2(4.7f,0.4f)); setTarget(); reSearchPath(); }
public bool search(SearchMapNode startNode, SearchMapNode endNode) { openList.Clear(); closedList.Clear(); SearchMapNode node = startNode; node.parent = null; int tryCount = 0; float time = System.Environment.TickCount; while (!node.Equals(endNode)) { tryCount++; int startY = node.row - 1 > 0 ? node.row - 1 : 0; int startX = node.col - 1 > 0 ? node.col - 1 : 0; int endX = node.col + 1 <= MapManager.getInstance().MAP_WIDTH - 1 ? node.col + 1 : MapManager.getInstance().MAP_WIDTH - 1; int endY = node.row + 1 <= MapManager.getInstance().MAP_HEIGHT - 1 ? node.row + 1 : MapManager.getInstance().MAP_HEIGHT - 1; for (int i = startY; i <= endY; i++) { for (int j = startX; j <= endX; j++) { // SearchMapNode test = map[i][j]; if (test.Equals(node)) { continue; } float costF = 1f; if (!test.walkable || !map[node.row][test.col].walkable || !map[test.row][node.col].walkable) { continue; } // if (MapManager.getInstance ().dynamicBlocks [test.row] [test.col]) { // costF = 5f; // } float cost = 1f; if (!(node.row == test.row || node.col == test.col)) { cost = 1.4f; } float g = node.g + cost * costF; float h = getH(test, endNode); float f = g + h; if (openList.Contains(test)) { if (test.f > f) { test.f = f; test.g = g; test.h = h; test.parent = node; } } else if (!closedList.Contains(test)) { test.f = f; test.g = g; test.h = h; test.parent = node; openList.Push(test); } } } closedList.Add(node); if (openList.Count == 0) { return(false); } node = openList.Pop(); } SearchMapNode p = endNode; pathPos.Clear(); while (p.parent != null) { Vector3 pos = MapManager.getInstance().tilemap.CellToWorld(new Vector3Int(p.col, -p.row, 0)); pos.x += MapManager.TILE_WIDTH * 0.5f * 0.01f; pos.y += MapManager.TILE_HEIGHT * 0.5f * 0.01f; pos.z = 0; pathPos.Insert(0, pos); p = p.parent; } //Debug.Log ("Spend Times :" + (System.Environment.TickCount - time)); return(true); }
public float getH(SearchMapNode node, SearchMapNode endNode) { return(Mathf.Abs(node.row - endNode.row) + Mathf.Abs(node.col - endNode.col)); }