MatthewGB

GOL

Feb 19th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Simulate : MonoBehaviour {
  5.  
  6.     bool simulate = true;
  7.  
  8.     int size = 100;
  9.     float diameter;
  10.  
  11.     public CellGO[] cells = new CellGO[100];
  12.  
  13.  
  14.     // Use this for initialization
  15.     void Start ()
  16.     {
  17.         diameter = Mathf.Sqrt(size);
  18.  
  19.         int i = 0;
  20.         while (i < size)
  21.         {
  22.             GameObject go = Instantiate(GameObject.Find("Cell"), new Vector3(cells[i].x, 0, cells[i].y), Quaternion.identity) as GameObject;
  23.             cells[i] = go.AddComponent<>
  24.             cells[i].alive = 1;
  25.             cells[i].x = i / 10;
  26.             cells[i].y = i % 10;
  27.             cells[i].go = go;
  28.             print(("Made cell " + i + " with location x" + cells[i].x + " and y "+cells[i].y));
  29.             i += 1;
  30.         }
  31.     }
  32.  
  33.  
  34.  
  35.     // Update is called once per frame
  36.     void Update()
  37.     {
  38.         if (Input.GetKeyDown("g"))
  39.         {
  40.             simulate = !simulate;
  41.         }
  42.         if (simulate)
  43.         {
  44.             int i = 0;
  45.             while (i < size)
  46.             {
  47.                 int count = 0;
  48.  
  49.                 try
  50.                 {
  51.                     count += cells[i + 1].alive;
  52.                     count += cells[i - 1].alive;
  53.                     count += cells[i + size].alive;
  54.                     count += cells[i - size].alive;
  55.                 }
  56.                 catch { }
  57.                 cells[i].near = count;
  58.  
  59.                 if (cells[i].alive == 1)
  60.                 {
  61.                     if (count == 0 || count == 1)
  62.                     {
  63.                         cells[i].changeAlive(0);
  64.                     }
  65.                     if (count > 3)
  66.                     {
  67.                         cells[i].changeAlive(0);
  68.                     }
  69.                 }
  70.                 else
  71.                 {
  72.                     if (count == 3)
  73.                     {
  74.                         cells[i].changeAlive(1);
  75.                     }
  76.                 }
  77.                 i += 1;
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment