I'm trying to make the pulse system in a "build the pipeline" puzzle game to check if tiles are connected to the starting tile.
There is a weird bug that prevents tiles on the right and tiles on the top of any powered up tile (including starting one) to change state, but it works fine with tiles on the left and bottom.
I tried to log this and it happened that the cycle is going trough these tiles, but their state doesn't change. Here is an example:
Here is the code. If I remove this bit:
else
p.GetComponent<piece>().ConnectedToStart = false;
then it is changing the state as it should, but I have no idea how to disable tiles that should be unpowered in any other way. Also, the question is still there - why does it only work for the tiles on the left and bottom?
public class Puzzle
{
public int winValue;
public int curValue;
public int width;
public int height;
public piece[,] pieces;
}
public Puzzle puzzle;
GameObject[] pieces;
void Start()
{
pieces = GameObject.FindGameObjectsWithTag("Piece");
}
void Update()
{
CheckForStartConnections();
}
public void CheckForStartConnections()
{
foreach (var p in pieces)
{
if (p.GetComponent<piece>().StartPiece)
Pulse((int)p.transform.position.x, (int)p.transform.position.y);
else
p.GetComponent<piece>().ConnectedToStart = false;
}
}
void Pulse(int w, int h)
{
puzzle.pieces[w, h].GetComponent<piece>().ConnectedToStart = true;
//compares left
if (w != 0)
if (puzzle.pieces[w, h].values[3] == 1 && puzzle.pieces[w - 1, h].values[1] == 1)
{
if (!puzzle.pieces[w - 1, h].GetComponent<piece>().ConnectedToStart)
Pulse((int)puzzle.pieces[w - 1, h].transform.position.x, (int)puzzle.pieces[w - 1, h].transform.position.y);
}
//compares bottom
if (h != 0)
if (puzzle.pieces[w, h].values[2] == 1 && puzzle.pieces[w, h - 1].values[0] == 1)
{
if (!puzzle.pieces[w, h - 1].GetComponent<piece>().ConnectedToStart)
Pulse((int)puzzle.pieces[w, h - 1].transform.position.x, (int)puzzle.pieces[w, h - 1].transform.position.y);
}
//compares top
if (h != puzzle.height - 1)
if ((puzzle.pieces[w, h].values[0] == 1 && puzzle.pieces[w, h + 1].values[2] == 1))
{
if (!puzzle.pieces[w, h + 1].GetComponent<piece>().ConnectedToStart)
{
Pulse((int)puzzle.pieces[w, h + 1].transform.position.x, (int)puzzle.pieces[w, h + 1].transform.position.y);
}
}
//compares right
if (w != puzzle.width - 1)
if (puzzle.pieces[w, h].values[1] == 1 && puzzle.pieces[w + 1, h].values[3] == 1)
{
if (!puzzle.pieces[w + 1, h].GetComponent<piece>().ConnectedToStart)
{
Pulse((int)puzzle.pieces[w + 1, h].transform.position.x, (int)puzzle.pieces[w + 1, h].transform.position.y);
}
}
}
```