Lights Out on a Pentagon Cairo Grid (Monster in the Closet)

This is a version of Lights Out on a Cairo Grid.

Note: You need the Unity Web Plugin to play this example.
[button url=”https://gamelogic.co.za/examples/grids/monster-cairo/”]PLAY GAME[/button]

Source Code

public class LightsOutCairo: GLMonoBehaviour, IResetable
{
   public SplicedCell cellPrefab;
   public GameObject root;

   private Vector2 cellDimensions = new Vector2(128+4, 128+4);
   private CairoGrid grid;
   private IMap3D map;

   private Color offColor = ExampleUtils.colors[4];
   private Color onColor = ExampleUtils.colors[6];   

   public void Start()
   {
      Reset();
   }

   public void Update()
   {
      if (Input.GetMouseButtonDown(0))
      {
         Vector3 worldPosition = ExampleUtils.ScreenToWorld_NGUI(root, Input.mousePosition);

         var gridPoint = map[worldPosition];

         if (grid.Contains(gridPoint))
         {
            ToggleCellAt(gridPoint);
         }
      }
   }

   public void Reset()
   {
      root.transform.DestroyChildren();
      BuildGrid();
      InitGame();
   }

   private void BuildGrid()
   {
      grid = CairoGrid.Default(4, 6);

      map = new CairoMap(cellDimensions)
         .WithWindow(ExampleUtils.ScreenRect)
         .AlignMiddelCenter(grid)
         .AnchorCellMiddelCenter()
         .To3DXY();

      foreach (var point in grid)
      {
         SplicedCell cell = Instantiate(cellPrefab);

         Vector3 worldPoint = map[point];

         cell.transform.parent = root.transform;
         cell.transform.localScale = Vector3.one;
         cell.transform.localPosition = worldPoint;

         cell.SetColor(offColor);
         cell.SetText("");
         cell.SetOrientation(point.I);

         grid[point] = cell;
      }
   }

   private void InitGame()
   {
      //Initialiase with random pattern
      grid.SampleRandom(9).ForEach(ToggleCellAt);      
   }   

   private void ToggleCellAt(CairoPoint gridPoint)
   {
      foreach (var point in grid.GetNeighbors(gridPoint))
      {
         grid[point].HighlightOn = !grid[point].HighlightOn;
         grid[point].SetColor(grid[point].HighlightOn ? onColor : offColor);
      }
   }
}

1 thought on “Lights Out on a Pentagon Cairo Grid (Monster in the Closet)”

  1. Pingback: Grids Version 1.2 Now Available in the Asset Store | Gamelogic

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top