Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PopControl : MonoBehaviour
- {
- public GridManager G_M;
- public GameObject g_Manager;
- public bool Reset;
- //
- // Start is called before the first frame update
- void Start()
- {
- G_M = g_Manager.GetComponent<GridManager>();
- PlaceCells();
- G_M.simEnabled = true;
- InvokeRepeating("GameIteration", 0, G_M.refreshRate);
- }
- // Update is called once per frame
- void Update()
- {
- ResetGrid();
- }
- void GameIteration()
- {
- if (G_M.simEnabled == true)
- {
- ControlPop();
- CountNeighbors();
- }
- }
- void PlaceCells()
- {
- for (int y = 0; y < G_M.ScreenHeight; y++)
- {
- for (int x = 0; x < G_M.ScreenWidth; x++)
- {
- Cell cell = Instantiate(Resources.Load("Prefabs/Cell", typeof(Cell)), new Vector2(x, y), Quaternion.identity) as Cell; //instantiate the cell
- G_M.cellgrid[x, y] = cell;
- G_M.cellgrid[x, y].CellAliveColor = G_M.CellAliveColorVal;
- G_M.cellgrid[x, y].CellDeadColor = G_M.CellDeadColorVal;
- G_M.cellgrid[x, y].SetAlive(RandomAliveCell());
- }
- }
- }
- bool RandomAliveCell() //determines if a cell is alive or not randomly
- {
- int rand = UnityEngine.Random.Range(0, 100);
- if (rand > G_M.aliveChance)
- {
- return true;
- }
- return false;
- }
- void ControlPop()
- {
- for (int y = 0; y < G_M.ScreenHeight; ++y)
- {
- for (int x = 0; x < G_M.ScreenWidth; ++x)
- {
- //-Rules
- //any live cell with 2 or 3 alive neighbors is active
- //any dead cell with 2 or 3 alive neighbors becomes active
- //all other live cells become inactive in the next generation
- //all other inactive cells stay dead
- if (G_M.cellgrid[x, y].isAlive)
- {
- //Cell is active
- if (G_M.cellgrid[x, y].numNeighbors != 2 && G_M.cellgrid[x, y].numNeighbors != 3)
- {
- G_M.cellgrid[x, y].SetAlive(false);
- }
- }
- else
- {
- //Cell is inactive
- if (G_M.cellgrid[x, y].numNeighbors == 3)
- {
- G_M.cellgrid[x, y].SetAlive(true);
- }
- }
- }
- }
- }
- public void ToggleReset()
- {
- Reset = !Reset;
- }
- void CreateNewGrid()
- {
- PlaceCells();
- }
- void ResetGrid()
- {
- if (Reset == true)
- {
- G_M.gridSquares.AddRange(GameObject.FindGameObjectsWithTag("Square"));
- foreach (GameObject square in G_M.gridSquares)
- {
- Destroy(square);
- }
- G_M.gridSquares.Clear();
- CreateNewGrid();
- Reset = false;
- }
- }
- void CountNeighbors()
- {
- for (int y = 0; y < G_M.ScreenHeight; ++y)
- {
- for (int x = 0; x < G_M.ScreenWidth; x++)
- {
- int numNeighbors = 0;
- //North
- if (y + 1 < G_M.ScreenHeight)
- {
- if (G_M.cellgrid[x, y + 1].isAlive)
- {
- numNeighbors++;
- }
- }
- //East
- if (x + 1 < G_M.ScreenWidth)
- {
- if (G_M.cellgrid[x + 1, y].isAlive)
- {
- numNeighbors++;
- }
- }
- //South
- if (y - 1 >= 0)
- {
- if (G_M.cellgrid[x, y - 1].isAlive)
- {
- numNeighbors++;
- }
- }
- //West
- if (x - 1 >= 0)
- {
- if (G_M.cellgrid[x - 1, y].isAlive)
- {
- numNeighbors++;
- }
- }
- //North East
- if (x + 1 < G_M.ScreenWidth && y + 1 < G_M.ScreenHeight)
- {
- if (G_M.cellgrid[x + 1, y + 1].isAlive)
- {
- numNeighbors++;
- }
- }
- //North West
- if (x - 1 >= 0 && y + 1 < G_M.ScreenHeight)
- {
- if (G_M.cellgrid[x - 1, y + 1].isAlive)
- {
- numNeighbors++;
- }
- }
- //SouthEast
- if (x + 1 < G_M.ScreenWidth && y - 1 >= 0)
- {
- if (G_M.cellgrid[x + 1, y - 1].isAlive)
- {
- numNeighbors++;
- }
- }
- //SouthWest
- if (x - 1 >= 0 && y - 1 >= 0)
- {
- if (G_M.cellgrid[x - 1, y - 1].isAlive)
- {
- numNeighbors++;
- }
- }
- G_M.cellgrid[x, y].numNeighbors = numNeighbors;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement