Hello all,
Detect whether the pentamino X collides with the game grid (made up of small red planes).
If it collides, you need to update the grid (grid int [, ]) and perform a grid update like the code does (with the numbers 0 and 1 in the labels) if you move the pentaminoX around the screen.
Maybe you need to play with the box collider (I’m not so sure).
My question is the following: how do you update the grid int [, ] (and the corresponding texts at the top left of the screen) when you move the pentamino X along the game grid (and not outside the grid).
Here’s my code:
private void Update()
{
var changedFigures = Figures.Where(i => i.hasChanged);
if (changedFigures.Count() != 0)
{
ResetGrid();
foreach (var figure in changedFigures)
{
foreach (Transform box in figure)
{
int x = Mathf.FloorToInt(box.position.x / 1.6f);
int z = Mathf.FloorToInt(box.position.z / 1.6f);
if (PositionIsValid(x, z))
{
grid[z, x] = 1;
}
}
}
}
}
private void ResetGrid()
{
for (int z = 0; z < GridSizeZ; z++)
{
for (int x = 0; x < GridSizeX; x++)
{
grid[z, x] = 0;
}
}
}
private void OnGUI()
{
Rect rect = new Rect(10, (GridSizeZ * 20), 20, 20);
for (int z = 0; z < GridSizeZ; z++)
{
for (int x = 0; x < GridSizeX; x++)
{
GUI.color = grid[z, x] == 1 ? Color.yellow : Color.black;
GUI.Label(rect, grid[z, x].ToString());
rect.x += 20;
}
rect.x = 10;
rect.y -= 20;
}
}
private bool PositionIsValid(float x, float z)
{
return x >= 0 && x < GridSizeX && z >= 0 && z < GridSizeZ;
}
and a video that tells you more:
As you can see, the labels are updated even though pentamino X is outside the game grid (made up of small red planes) and not in the grid (what I would like).
Thanks for your help,
A+