Advertisement
Placido_GDD

PopControl

Jan 6th, 2022
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PopControl : MonoBehaviour
  6. {
  7.    
  8.     public GridManager G_M;
  9.     public GameObject g_Manager;
  10.     public bool Reset;
  11.                                            //
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         G_M = g_Manager.GetComponent<GridManager>();
  17.         PlaceCells();
  18.         G_M.simEnabled = true;
  19.         InvokeRepeating("GameIteration", 0, G_M.refreshRate);
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update()
  24.     {
  25.         ResetGrid();
  26.     }
  27.  
  28.     void GameIteration()
  29.     {
  30.         if (G_M.simEnabled == true)
  31.         {
  32.             ControlPop();
  33.             CountNeighbors();
  34.         }
  35.     }
  36.  
  37.  
  38.  
  39.     void PlaceCells()
  40.     {
  41.         for (int y = 0; y < G_M.ScreenHeight; y++)
  42.         {
  43.             for (int x = 0; x < G_M.ScreenWidth; x++)
  44.             {
  45.                 Cell cell = Instantiate(Resources.Load("Prefabs/Cell", typeof(Cell)), new Vector2(x, y), Quaternion.identity) as Cell; //instantiate the cell
  46.                 G_M.cellgrid[x, y] = cell;
  47.                 G_M.cellgrid[x, y].CellAliveColor = G_M.CellAliveColorVal;
  48.                 G_M.cellgrid[x, y].CellDeadColor = G_M.CellDeadColorVal;
  49.                 G_M.cellgrid[x, y].SetAlive(RandomAliveCell());
  50.             }
  51.         }
  52.  
  53.     }
  54.     bool RandomAliveCell() //determines if a cell is alive or not randomly
  55.     {
  56.         int rand = UnityEngine.Random.Range(0, 100);
  57.         if (rand > G_M.aliveChance)
  58.         {
  59.             return true;
  60.         }
  61.         return false;
  62.     }
  63.     void ControlPop()
  64.     {
  65.        
  66.         for (int y = 0; y < G_M.ScreenHeight; ++y)
  67.         {
  68.             for (int x = 0; x < G_M.ScreenWidth; ++x)
  69.             {
  70.                 //-Rules
  71.                 //any live cell with 2 or 3 alive neighbors is active
  72.                 //any dead cell with 2 or 3 alive neighbors becomes active
  73.                 //all other live cells become inactive in the next generation
  74.                 //all other inactive cells stay dead
  75.                 if (G_M.cellgrid[x, y].isAlive)
  76.                 {
  77.                     //Cell is active
  78.                     if (G_M.cellgrid[x, y].numNeighbors != 2 && G_M.cellgrid[x, y].numNeighbors != 3)
  79.                     {
  80.                         G_M.cellgrid[x, y].SetAlive(false);
  81.                     }
  82.                 }
  83.                 else
  84.                 {
  85.                     //Cell is inactive
  86.                     if (G_M.cellgrid[x, y].numNeighbors == 3)
  87.                     {
  88.                         G_M.cellgrid[x, y].SetAlive(true);
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     public void ToggleReset()
  96.     {
  97.         Reset = !Reset;
  98.     }
  99.     void CreateNewGrid()
  100.     {
  101.         PlaceCells();
  102.  
  103.     }
  104.     void ResetGrid()
  105.     {
  106.         if (Reset == true)
  107.         {
  108.  
  109.             G_M.gridSquares.AddRange(GameObject.FindGameObjectsWithTag("Square"));
  110.            
  111.             foreach (GameObject square in G_M.gridSquares)
  112.             {
  113.                 Destroy(square);
  114.             }
  115.             G_M.gridSquares.Clear();
  116.             CreateNewGrid();
  117.             Reset = false;
  118.         }
  119.     }
  120.     void CountNeighbors()
  121.     {
  122.         for (int y = 0; y < G_M.ScreenHeight; ++y)
  123.         {
  124.             for (int x = 0; x < G_M.ScreenWidth; x++)
  125.             {
  126.                 int numNeighbors = 0;
  127.  
  128.                 //North
  129.                 if (y + 1 < G_M.ScreenHeight)
  130.                 {
  131.                     if (G_M.cellgrid[x, y + 1].isAlive)
  132.                     {
  133.                         numNeighbors++;
  134.                     }
  135.                 }
  136.                 //East
  137.                 if (x + 1 < G_M.ScreenWidth)
  138.                 {
  139.                     if (G_M.cellgrid[x + 1, y].isAlive)
  140.                     {
  141.                         numNeighbors++;
  142.                     }
  143.  
  144.                 }
  145.                 //South
  146.                 if (y - 1 >= 0)
  147.                 {
  148.                     if (G_M.cellgrid[x, y - 1].isAlive)
  149.                     {
  150.                         numNeighbors++;
  151.                     }
  152.                 }
  153.                 //West
  154.                 if (x - 1 >= 0)
  155.                 {
  156.                     if (G_M.cellgrid[x - 1, y].isAlive)
  157.                     {
  158.                         numNeighbors++;
  159.                     }
  160.                 }
  161.                 //North East
  162.                 if (x + 1 < G_M.ScreenWidth && y + 1 < G_M.ScreenHeight)
  163.                 {
  164.                     if (G_M.cellgrid[x + 1, y + 1].isAlive)
  165.                     {
  166.                         numNeighbors++;
  167.                     }
  168.                 }
  169.                 //North West
  170.                 if (x - 1 >= 0 && y + 1 < G_M.ScreenHeight)
  171.                 {
  172.                     if (G_M.cellgrid[x - 1, y + 1].isAlive)
  173.                     {
  174.                         numNeighbors++;
  175.                     }
  176.                 }
  177.                 //SouthEast
  178.                 if (x + 1 < G_M.ScreenWidth && y - 1 >= 0)
  179.                 {
  180.                     if (G_M.cellgrid[x + 1, y - 1].isAlive)
  181.                     {
  182.                         numNeighbors++;
  183.                     }
  184.                 }
  185.                 //SouthWest
  186.                 if (x - 1 >= 0 && y - 1 >= 0)
  187.                 {
  188.                     if (G_M.cellgrid[x - 1, y - 1].isAlive)
  189.                     {
  190.                         numNeighbors++;
  191.                     }
  192.                 }
  193.                 G_M.cellgrid[x, y].numNeighbors = numNeighbors;
  194.  
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement