public void FindNearestTarget() { List <GameObject> targets = GameObject.Find("GameMaster").GetComponent <MapData>().getPlayers(); GameObject nearest = null; float nearestDistance = Mathf.Infinity; foreach (GameObject obj in targets) { //computeAdjacencyList(); foreach (GameObject resetTile in tiles) { resetTile.GetComponent <Tile>().distance = -1; resetTile.GetComponent <Tile>().visited = false; } obj.GetComponent <PlayerMove>().currentTile = getTargetTile(obj); Tile t = obj.GetComponent <PlayerMove>().currentTile; TileList process = new TileList(); t.distance = 0; process.Add(t); t.visited = true; while (process.Count > 0) { bool finishLoop = false; process.Sort(process.getComparator()); Tile currentTile = process[0]; process.RemoveAt(0); this.getTargetTile(gameObject); if (currentTile.Equals(this.currentTile)) { finishLoop = true; process.Clear(); if (currentTile.distance - currentTile.getMoveCost() + 1 < nearestDistance) { nearest = obj; nearestDistance = currentTile.distance - currentTile.getMoveCost() + 1; distanceToTarget = nearestDistance; } } foreach (Tile tile in currentTile.adjacencyList) { if (!tile.visited && tile.isWalkable && finishLoop == false) { tile.parent = currentTile; tile.visited = true; tile.distance = currentTile.distance + tile.getMoveCost(); process.Add(tile); } } } } target = nearest; }
public void BuildPathToTargetTile() { foreach (GameObject resetTile in tiles) { resetTile.GetComponent <Tile>().distance = -1; resetTile.GetComponent <Tile>().visited = false; } TileList process = new TileList(); currentTile.distance = 0; process.Add(currentTile); currentTile.visited = true; currentTile.parent = null; while (process.Count > 0) { process.Sort(process.getComparator()); Tile t = process[0]; process.RemoveAt(0); bool endLoop = false; if (t.isWalkable && !endLoop) { foreach (Tile tile in t.adjacencyList) { string tagOn = ""; foreach (Collider2D collider in tile.GetOnTopOf()) { if (collider.gameObject.tag != null) { if (collider.gameObject.tag == "Player" || collider.gameObject.tag == "Enemy") { tagOn = collider.gameObject.tag; } //Debug.Log(tagOn); } } if (!tile.visited && (tile.walkable || tagOn == gameObject.tag) && t.distance + tile.getMoveCost() <= move && !endLoop) { tile.parent = t; tile.visited = true; tile.distance = t.distance + tile.getMoveCost(); process.Add(tile); } if (tile.Equals(tileToMoveTo)) { endLoop = true; } } } } }
public void findSelectableTiles() //Needs updates for flying (Peg / Draco Knights) and climbing units (Brigands) { computeAdjacencyList(); getCurrentTile(); TileList process = new TileList(); currentTile.distance = 0; process.Add(currentTile); currentTile.visited = true; currentTile.selectable = true; if (!selectableTiles.Contains(currentTile)) { selectableTiles.Add(currentTile); } //Source tile's parent is null while (process.Count > 0) { process.Sort(process.getComparator()); Tile t = process[0]; process.RemoveAt(0); if (t.walkable && !selectableTiles.Contains(t)) { selectableTiles.Add(t); } if (canWalk && !canFly) { string tagOn = ""; foreach (Collider2D collider in t.GetOnTopOf()) { if (collider.gameObject.tag != null) { if (collider.gameObject.tag == "Player" || collider.gameObject.tag == "Enemy") { tagOn = collider.gameObject.tag; } //Debug.Log(tagOn); } } if (t.walkable || tagOn == gameObject.tag || t.Equals(currentTile)) { if (tagOn == gameObject.tag) { t.selectableHighlight = true; selectableHighlightList.Add(t); } foreach (Tile tile in t.adjacencyList) { if (!tile.visited && tile.isWalkable && t.distance + tile.getMoveCost() <= move) { tile.parent = t; tile.visited = true; tile.distance = t.distance + tile.getMoveCost(); process.Add(tile); } } } } } foreach (Tile t in selectableTiles) { t.selectable = true; } }
public void findSelectableTiles() { computeAdjacencyList(); getCurrentTile(); TileList process = new TileList(); currentTile.distance = 0; process.Add(currentTile); currentTile.visited = true; //currentTile.parent = null while (process.Count > 0) { process.Sort(process.getComparator()); Tile t = process[0]; process.RemoveAt(0); selectableTiles.Add(t); t.selectable = true; if (canWalk && !canFly) { if (t.walkable) { bool edge = true; foreach (Tile tile in t.adjacencyList) { if (tile.walkable && t.distance + tile.getMoveCost() <= move) { edge = false; } if (!tile.visited && tile.walkable && t.distance + tile.getMoveCost() <= move) { tile.parent = t; tile.visited = true; tile.distance = t.distance + tile.getMoveCost(); process.Add(tile); /*else if (t.distance <= move + attackRange){ * tile.visited = true; * attackableTiles.Add(t); * t.attackable = true; * * } */ } /* * else if (t.getMoveCost() > 1 && tile.walkable && t.distance + tile.getMoveCost() <= move) * { * * if (!selectableTiles.Contains(tile)) * { * Debug.Log("here"); * tile.parent = t; * tile.visited = true; * foreach (Tile moreTile in tile.adjacencyList) * { * moreTile.visited = false; * } * tile.distance = 1 + t.distance + tile.getMoveCost() - 1; * process.Add(tile); * } * } */ } if (edge) { //edgeTiles.Add(t); } } } } }
public void FindActionToTake() { Tile tileToMoveTo = null; float nearestDistance = Mathf.Infinity; if (!attackableTiles.Contains(target.GetComponent <PlayerMove>().getTargetTile(target))) { foreach (Tile t in edgeTiles) { foreach (GameObject resetTile in tiles) { resetTile.GetComponent <Tile>().distance = -1; resetTile.GetComponent <Tile>().visited = false; } TileList process = new TileList(); t.distance = 0; process.Add(t); t.visited = true; while (process.Count > 0) { bool finishLoop = false; process.Sort(process.getComparator()); Tile currentTile = process[0]; process.RemoveAt(0); //Debug.Log(currentTile.transform.position); //if (currentTile.transform.position == new Vector3(1.5f, 7.5f, 0)){ // Debug.DrawLine(currentTile.transform.position, currentTile.transform.position + Vector3.up, Color.white, 10f); // foreach (Tile temp in currentTile.adjacencyList){ // } //} if (currentTile.Equals(target.GetComponent <PlayerMove>().currentTile)) { finishLoop = true; process.Clear(); if (currentTile.distance - currentTile.getMoveCost() + 1 < nearestDistance) { tileToMoveTo = t; nearestDistance = currentTile.distance - currentTile.getMoveCost() + 1; } } foreach (Tile tile in currentTile.adjacencyList) { if (!tile.visited && tile.isWalkable && finishLoop == false) { tile.parent = currentTile; tile.visited = true; tile.distance = currentTile.distance + tile.getMoveCost(); process.Add(tile); } } } } this.tileToMoveTo = tileToMoveTo; } else { this.tileToMoveTo = null; canAttack = true; } }