//initalize the backdrop public void Init() { pixels = new PixelBackdrop[sizeX, sizeY]; //loop though the entire pixel array and assign the corralating pixels to it, then initalize them for (int idxY = 0; idxY <= sizeY - 1; idxY++) { for (int idxX = 0; idxX <= sizeX - 1; idxX++) { pixels[idxX, idxY] = gameObject.transform.GetChild(idxY).transform.GetChild(idxX).GetComponent <PixelBackdrop>(); } } for (int idxY = 0; idxY <= sizeY - 1; idxY++) { for (int idxX = 0; idxX <= sizeX - 1; idxX++) { PixelBackdrop.Neighbors currentNeighbors = GetPixelNeighbors(idxX, idxY); pixels[idxX, idxY].Init(currentNeighbors); } } InitGameOfLife(); initalized = true; }
//locates and returns all pixel neighbors, returns null if there are no neighbors around the pixel public PixelBackdrop.Neighbors GetPixelNeighbors(int xIndex, int yIndex) { //check each side and corner of a pixel to see if there should be a pixel, then assign it. else assign null PixelBackdrop.Neighbors neighbors = new PixelBackdrop.Neighbors(); neighbors.top = IsThereAPixelAtIndex(xIndex, yIndex - 1) ? pixels[xIndex, yIndex - 1] : null; neighbors.topRight = IsThereAPixelAtIndex(xIndex + 1, yIndex - 1) ? pixels[xIndex + 1, yIndex - 1] : null; neighbors.right = IsThereAPixelAtIndex(xIndex + 1, yIndex) ? pixels[xIndex + 1, yIndex] : null; neighbors.bottomRight = IsThereAPixelAtIndex(xIndex + 1, yIndex + 1) ? pixels[xIndex + 1, yIndex + 1] : null; neighbors.bottom = IsThereAPixelAtIndex(xIndex, yIndex + 1) ? pixels[xIndex, yIndex + 1] : null; neighbors.bottomLeft = IsThereAPixelAtIndex(xIndex - 1, yIndex + 1) ? pixels[xIndex - 1, yIndex + 1] : null; neighbors.left = IsThereAPixelAtIndex(xIndex - 1, yIndex) ? pixels[xIndex - 1, yIndex] : null; neighbors.topLeft = IsThereAPixelAtIndex(xIndex - 1, yIndex - 1) ? pixels[xIndex - 1, yIndex - 1] : null; return(neighbors); }