Turn base strategy possible move recurrsion bug
the 1st 4 surrounding tiles is perfectly fine. however, when it start looking for the surrounding nodes after. path finding go all over the place
1st column is with recursion and it is bugged, second is without and it works fine (but cannot search anymore tiles)
http://i.imgur.com/6HSRJ8M.jpg
green is all the places you can move to. purple is the clickable tiles you can select (1st 4 surrounding tiles)
objective of the program:
get 4 clickable tile surrounding the unit (up, down, right, left)
find all the possible spot they can move to by recursively searching the surrounding tiles to the 4 clickable tiles
then move to one of the 4 click able tiles and restart from step one until there is no move left
explanation of program:
the 1st run will get the 4 surrounding tiles and make them clickable, it then recursively get 3 surrounding (unclickable) tiles from each of the 4 original tile, then it would get 3 more from each until moves = 0. when i click on a tile, it would rerun the entire program to get the next 4 surrounding clickable tile and find farthest possible move the unit can get to.
int DirectionFrom tells you where it came from last (1 = up, 2=right,
3=left, 4=down). if went down last,
it would not search the node above.the grid is created by a forloop, it will always be perfect square
starting from 0,0.
//FieldManager
//the 4 nodes from the last position
//to see if they should be clickable
public void PathFinder(int move, int jump, Vector3 pos, int DirectionFrom, bool check){
//find the nodes -> call a funtion there to check ->
findU = ((int)(pos.y)) + 1;
findD = ((int)(pos.y)) - 1;
findR = ((int)(pos.x)) + 1;
findL = ((int)(pos.x)) - 1;
if ((DirectionFrom != 1) && (findD >= 0)) {
childname = pos.x+"-"+findD;
transform.FindChild(childname).GetComponent<TileInfo>().CheckMove(move, jump, pos, 4, check);
}
if((DirectionFrom != 2) && (findL >= 0)) {
childname = findL+"-"+pos.y;
transform.FindChild(childname).GetComponent<TileInfo>().CheckMove(move, jump, pos, 3, check);
}
if((DirectionFrom != 3) && (findR <= FieldX)) {
childname = findR+"-"+pos.y;
transform.FindChild(childname).GetComponent<TileInfo>().CheckMove(move, jump, pos, 2, check);
}
if((DirectionFrom != 4) && (findU <= FieldY)) {
childname = pos.x+"-"+findU;
transform.FindChild(childname).GetComponent<TileInfo>().CheckMove(move, jump, pos, 1, check);
}
}
//TileInfo
public void Set_DC(Color c){
if (selectable) {
rend.material.color = Color.magenta;
default_color = Color.magenta;
}
else {
rend.material.color = c;
default_color = c;
}
}
public void CheckMove(int move, int jump, Vector3 pos, int DirectionFrom, bool click){
if (jump >= (((pos.z * (-1)) - transform.position.z))) {
if (click) { selectable = true; }
Set_DC(Color.green);
NewMove = move - 1;
//IF I DELETE THE FOLLOWING CONDITION, THE PROGRAM WORKS FINE FOR THE 1ST 4 TILES
if ((NewMove) > 0){
transform.GetComponentInParent<FieldManager>().PathFinder(NewMove, jump, transform.position, DirectionFrom, false);
}
}
}
Your answer
