- Home /
Turn-based strategy possible moves issue
I'm making a turn-based strategy game similar to advance wars. I've implemented a BFS algorithm that whenever a unit is selected, all the possible tiles it can go too will be highlighted. The tiles are prefabs that are dragged onto the scene and I have a tile manager script that locks them into coordinates (0,0), (1,0), (2,0) etc.
The issue I'm having is that some tiles are not highlighting when they should be. At first I thought this was an issue with my BFS algorithm (it still could be) but what I noticed that for the tiles that didn't highlight correctly if I remove them from the scene and then re-add them it would work until I restarted Unity.
BFS algorithm
public override List<GameObject> getReachableTiles(GameObject currentTile, int moves) {
currentTile = tm.GetTileAt ((int)currentTile.transform.position.x, (int)currentTile.transform.position.y).gameObject;
List<GameObject> tempTileList = new List<GameObject>();
if (!adjacentTiles.Contains(currentTile)) {
adjacentTiles.Add (currentTile);
}
if (tm.GetTileAt (((int)currentTile.transform.position.x -1), (int)currentTile.transform.position.y) != null
&& (!adjacentTiles.Contains(tm.GetTileAt((int)currentTile.transform.position.x -1, (int)currentTile.transform.position.y).gameObject))){ //Check for tile left of current
tempTileList.Add(tm.GetTileAt (((int)currentTile.transform.position.x -1), (int)currentTile.transform.position.y).gameObject);
Debug.Log("Check Left");
}
if (tm.GetTileAt (((int)currentTile.transform.position.x +1), (int)currentTile.transform.position.y) != null
&& (!adjacentTiles.Contains(tm.GetTileAt((int)currentTile.transform.position.x +1, (int)currentTile.transform.position.y).gameObject))){ //Check for tile right of current
tempTileList.Add(tm.GetTileAt (((int)currentTile.transform.position.x +1), (int)currentTile.transform.position.y).gameObject);
Debug.Log("Check Right");
}
if (tm.GetTileAt (((int)currentTile.transform.position.x), ((int)currentTile.transform.position.y -1)) != null
&& (!adjacentTiles.Contains (tm.GetTileAt((int)currentTile.transform.position.x, (int)currentTile.transform.position.y-1).gameObject))){ //Check for tile below of current
tempTileList.Add(tm.GetTileAt ((int)currentTile.transform.position.x, ((int)currentTile.transform.position.y -1)).gameObject);
Debug.Log("Check Down");
}
if (tm.GetTileAt (((int)currentTile.transform.position.x), ((int)currentTile.transform.position.y +1)) != null
&& (!adjacentTiles.Contains (tm.GetTileAt((int)currentTile.transform.position.x, (int)currentTile.transform.position.y+1).gameObject))){ //Check for tile Above of current
tempTileList.Add(tm.GetTileAt ((int)currentTile.transform.position.x, ((int)currentTile.transform.position.y +1)).gameObject);
Debug.Log("Check Up");
}
if (moves > 0) {
foreach(GameObject tiles in tempTileList) {
getReachableTiles (tiles, moves-1);
}
return adjacentTiles;
}
else {
return adjacentTiles;
}
Update function that changes the colour of the tiles for the possible moves of selected unit
protected virtual void Update () {
if (selected) {
renderer.material.color = isSelected;
possibleMoves = this.getReachableTiles (this.gameObject, this.getMove());
foreach (GameObject possmove in possibleMoves)
{
possmove.renderer.material.color = Color.gray;
}
}
else {
renderer.material.color = notSelected;
}
}
thank you for your reply. I have it both in the editor and print statements that output the tile at the correct location during run time, but I will investigate further.
And thanks for the tip in the BFS algorithm!
If you're building a tile based game, I HIGHLY recommend the Grids by Gamelogic.
It's brilliant, probably the single best add on for Unity I've ever used.
Thank you for the recommendation, it looks really useful.
However I'm not sure how far I would take this project as I'm just trying to build program$$anonymous$$g expertise.
Understandable. I've used it on three little projects already, it's a nice thing to have in your stable of tricks.
Having a good grid implementation is one of the glaring problems Unity has in my estimation.
Answer by Baste · Jan 14, 2015 at 04:56 PM
Check if the tiles have been moved slightly. If you have a tile that's placed at x = 1.99999, casting it's x-coordinate to an int will yield 1, not 2. That will make things easier.
And also, you should really clean up your BFS. If you define the ints currentX and currentY in the top of getReachableTiles, and use them instead of "(int)currentTile.transform.position.x", it will be a lot easier to read.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer