I am attempting to replicate the puzzle game “Lights Out”, a live example of which can be found HERE.
In this game, a 3x3 (or 5x5 etc) is created from a series of toggleable elements which, when toggled, will also switch all those adjacent to it.
I have attempted this with 2 simple scripts, the first below is attached to each “light” in the 3x3 grid, the second script is a parent controller, attached the grid itself, and has the “light” elements assigned to it in order.
This in its current state is not working correctly, but on top of that, it is in no way dynamic, and I was hoping someone who has a little more experience dealing with grid-based stuff would have a suggestion on making this better (or at least work).
//Attached to each individual light
using UnityEngine;
using UnityEngine.UI;
public class LightsOutLight : MonoBehaviour
{
public bool status;
public Color32 activeColor = new Color32(200, 255, 255, 255);
public Color32 inactiveColor = new Color32(160, 0, 0, 255);
public void ChangeActivity()
{
status = !status;
GetComponent<Image>().color = status ? activeColor : inactiveColor;
}
}
//Attached to the grid parent
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsOut : MonoBehaviour
{
public List<LightsOutLight> lights;
void Update()
{
for(int i = 0; i < lights.Count; i++)
{
switch (i)
{
#region 0
case 0:
//1 & 3
if(lights[0].status)
{
lights[1].ChangeActivity();
lights[3].ChangeActivity();
}
break;
#endregion
#region 1
case 1:
//0, 2 & 4
if (lights[1].status)
{
lights[0].ChangeActivity();
lights[2].ChangeActivity();
lights[4].ChangeActivity();
}
break;
#endregion
#region 2
case 2:
//1 & 5
if (lights[2].status)
{
lights[1].ChangeActivity();
lights[5].ChangeActivity();
}
break;
#endregion
#region 3
case 3:
//0, 4 & 6
if (lights[3].status)
{
lights[0].ChangeActivity();
lights[4].ChangeActivity();
lights[6].ChangeActivity();
}
break;
#endregion
#region 4
case 4:
//1, 3, 5 & 7
if (lights[4].status)
{
lights[1].ChangeActivity();
lights[3].ChangeActivity();
lights[5].ChangeActivity();
lights[7].ChangeActivity();
}
break;
#endregion
#region 5
case 5:
//2, 4 & 8
if (lights[5].status)
{
lights[2].ChangeActivity();
lights[4].ChangeActivity();
lights[8].ChangeActivity();
}
break;
#endregion
#region 6
case 6:
//3 & 7
if (lights[6].status)
{
lights[3].ChangeActivity();
lights[7].ChangeActivity();
}
break;
#endregion
#region 7
case 7:
//4, 6 & 8
if (lights[7].status)
{
lights[4].ChangeActivity();
lights[6].ChangeActivity();
lights[8].ChangeActivity();
}
break;
#endregion
#region 8
case 8:
//5 & 7
if (lights[8].status)
{
lights[5].ChangeActivity();
lights[7].ChangeActivity();
}
break;
#endregion
}
}
}
}