// Start is called before the first frame update void Start() { int index = 0; foreach (Transform child in transform) { PathTileData refPathTileData = child.gameObject.GetComponent <PathTileData>(); if (refPathTileData == null) { Debug.LogError("[StairwayToHeavenManager] PathTileData not FOUND"); return; } refPathTileData.ITileIndex = index; m_dicStairwayToHeaven.Add(index, refPathTileData); index++; } }
void Start() { int index = 0; foreach (Transform child in transform) { PathTileData refPathTileData = child.gameObject.GetComponent <PathTileData>(); if (refPathTileData == null) { Debug.LogError("[PathManager] PathTileData not FOUND"); return; } //Set the index to the tile so as to get the position from PathTileData refPathTileData.ITileIndex = index; m_dicPathTileTypes.Add(index, refPathTileData); switch (refPathTileData.EnumPathTileType) { case GameUtility.Base.ePathTileType.BlueStart: m_refBlueStart = refPathTileData; break; case GameUtility.Base.ePathTileType.YellowStart: m_refYellowStart = refPathTileData; break; case GameUtility.Base.ePathTileType.RedStart: m_refRedStart = refPathTileData; break; case GameUtility.Base.ePathTileType.GreenStart: m_refGreenStart = refPathTileData; break; } index++; } }
void FindPath(Dictionary <Vector3, PathTileData> openList, Dictionary <Vector3, PathTileData> closedList, Vector3 centeredTarget, List <Vector3> tileSet, List <Vector3> path) { lock (path) { PathTileData data = new PathTileData(); Vector3 key = new Vector3(); bool foundPath = false; while (openList.Count > 0) { key = openList.Keys.ToList()[0]; float max = openList[key].totalDistance; foreach (Vector3 k in openList.Keys.ToList()) { if (openList[k].totalDistance < max) { max = openList[k].totalDistance; key = k; } } data = openList[key]; openList.Remove(key); closedList.Add(key, data); if (key == centeredTarget) { foundPath = true; break; } PathTileData junk = new PathTileData(); if (tileSet.Contains(new Vector3(key.x + 1, key.y, 0)) && !closedList.TryGetValue(new Vector3(key.x + 1, key.y, 0), out junk)) { if (openList.TryGetValue(new Vector3(key.x + 1, key.y, 0), out junk)) { if (junk.totalDistance > closedList[key].pathLength + 1 + Vector3.Distance(new Vector3(key.x + 1, key.y, 0), centeredTarget)) { openList[new Vector3(key.x + 1, key.y, 0)] = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x + 1, key.y, 0), centeredTarget)); } } else { PathTileData tile = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x + 1, key.y, 0), centeredTarget)); openList.Add(new Vector3(key.x + 1, key.y, 0), tile); } } if (tileSet.Contains(new Vector3(key.x - 1, key.y, 0)) && !closedList.TryGetValue(new Vector3(key.x - 1, key.y, 0), out junk)) { if (openList.TryGetValue(new Vector3(key.x - 1, key.y, 0), out junk)) { if (junk.totalDistance > closedList[key].pathLength + 1 + Vector3.Distance(new Vector3(key.x - 1, key.y, 0), centeredTarget)) { openList[new Vector3(key.x - 1, key.y, 0)] = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x - 1, key.y, 0), centeredTarget)); } } else { PathTileData tile = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x - 1, key.y, 0), centeredTarget)); openList.Add(new Vector3(key.x - 1, key.y, 0), tile); } } if (tileSet.Contains(new Vector3(key.x, key.y + 1, 0)) && !closedList.TryGetValue(new Vector3(key.x, key.y + 1, 0), out junk)) { if (openList.TryGetValue(new Vector3(key.x, key.y + 1, 0), out junk)) { if (junk.totalDistance > closedList[key].pathLength + 1 + Vector3.Distance(new Vector3(key.x, key.y + 1, 0), centeredTarget)) { openList[new Vector3(key.x, key.y + 1, 0)] = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x, key.y + 1, 0), centeredTarget)); } } else { PathTileData tile = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x, key.y + 1, 0), centeredTarget)); openList.Add(new Vector3(key.x, key.y + 1, 0), tile); } } if (tileSet.Contains(new Vector3(key.x, key.y - 1, 0)) && !closedList.TryGetValue(new Vector3(key.x, key.y - 1, 0), out junk)) { if (openList.TryGetValue(new Vector3(key.x, key.y - 1, 0), out junk)) { if (junk.totalDistance > closedList[key].pathLength + 1 + Vector3.Distance(new Vector3(key.x, key.y - 1, 0), centeredTarget)) { openList[new Vector3(key.x, key.y - 1, 0)] = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x, key.y - 1, 0), centeredTarget)); } } else { PathTileData tile = new PathTileData(key, closedList[key].pathLength + 1, 1 + closedList[key].pathLength + Vector3.Distance(new Vector3(key.x, key.y - 1, 0), centeredTarget)); openList.Add(new Vector3(key.x, key.y - 1, 0), tile); } } } if (foundPath) { path.Clear(); path.Add(key); key = data.parent; while (closedList[key].parent.x > -9000) { path.Insert(0, key); key = closedList[key].parent; } } } }
public void InitializePath(Vector3 pos, Vector3 target, int map, List <Vector3> returnPath) { /* bool playerOnMap = false; * Vector3 centeredPlayerPos = new Vector3(); * if (map == 0 && SceneManager.GetActiveScene().name.Equals("House") || map == 1 && !SceneManager.GetActiveScene().name.Equals("House")) { * Vector3 playerPos = GameObject.FindGameObjectWithTag("Player").transform.position; * centeredPlayerPos = new Vector3(Mathf.Sign(playerPos.x) * (Mathf.Abs((int)playerPos.x) + 0.5f), Mathf.Sign(playerPos.y) * (Mathf.Abs((int)playerPos.y) + 0.5f), 0); * playerOnMap = true; * }*/ Dictionary <Vector3, PathTileData> openList, closedList; Vector3 centeredTarget = new Vector3(Mathf.Sign(target.x) * (Mathf.Abs((int)target.x) + 0.5f), Mathf.Sign(target.y) * (Mathf.Abs((int)target.y) + 0.5f), 0); openList = new Dictionary <Vector3, PathTileData>(); closedList = new Dictionary <Vector3, PathTileData>(); List <Vector3> temp; Vector3 tempPos = new Vector3(Mathf.Sign(pos.x) * (Mathf.Abs((int)pos.x) + 0.5f), Mathf.Sign(pos.y) * (Mathf.Abs((int)pos.y) + 0.5f), 0); if (map == 0) { temp = houseTiles; } else { temp = worldTiles; } PathTileData data = new PathTileData(new Vector3(-9999, -9999, -9999), 0, Vector3.Distance(tempPos, centeredTarget)); closedList.Add(tempPos, data); Vector3 checkTile = new Vector3(tempPos.x + 1, tempPos.y, 0); if (temp.Contains(checkTile)) { // if ((playerOnMap && checkTile != centeredPlayerPos) || !playerOnMap) { PathTileData tile = new PathTileData(tempPos, 1, 1 + Vector3.Distance(new Vector3(tempPos.x + 1, tempPos.y, 0), centeredTarget)); openList.Add(new Vector3(tempPos.x + 1, tempPos.y, 0), tile); //} } checkTile = new Vector3(tempPos.x - 1, tempPos.y, 0); if (temp.Contains(checkTile)) { // if ((playerOnMap && checkTile != centeredPlayerPos) || !playerOnMap) { PathTileData tile = new PathTileData(tempPos, 1, 1 + Vector3.Distance(new Vector3(tempPos.x - 1, tempPos.y, 0), centeredTarget)); openList.Add(new Vector3(tempPos.x - 1, tempPos.y, 0), tile); // } } checkTile = new Vector3(tempPos.x, tempPos.y + 1, 0); if (temp.Contains(checkTile)) { // if ((playerOnMap && checkTile != centeredPlayerPos) || !playerOnMap) { PathTileData tile = new PathTileData(tempPos, 1, 1 + Vector3.Distance(new Vector3(tempPos.x, tempPos.y + 1, 0), centeredTarget)); openList.Add(new Vector3(tempPos.x, tempPos.y + 1, 0), tile); // } } checkTile = new Vector3(tempPos.x, tempPos.y - 1, 0); if (temp.Contains(checkTile)) { // if ((playerOnMap && checkTile != centeredPlayerPos) || !playerOnMap) { PathTileData tile = new PathTileData(tempPos, 1, 1 + Vector3.Distance(new Vector3(tempPos.x, tempPos.y - 1, 0), centeredTarget)); openList.Add(new Vector3(tempPos.x, tempPos.y - 1, 0), tile); // } } Thread thread = new Thread(() => FindPath(openList, closedList, centeredTarget, temp, returnPath)); thread.Start(); }