Game Of Life For Unity

Following Unity package is taken and translated from the book(Processing language) Generative Art : A practical guide using Processing by Matt Pearson. Really helping me a lot to understand generating, graphical algorithms..

Purpose was learning OOP approach to these kind of algorithms. Any feedback, support, ideas, comments are welcome. I hope Matt Pearson won't sue me... Enjoy the GameOfLife.unitypackage for lazies..

// Code is translated from the book(Processing language)
// Generative Art : A practical guide using Processing
// by Matt Pearson
// He owns all the copyrights etc.
// I just made a C# translation for exercise purposes.
// feel free to try it.

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic; // added for using List namespace
    public class GameOfLife : MonoBehaviour {

    private Cell[,] _cellArray;
    static int _cellSize = 10;
    public int _numX, _numY;
    private int canvasW = 300;
    private int canvasH = 300;
    private float posScale = .4f;
    public GameObject cellPrefab;

    private Cell cell;

    public class Cell {

        public float x, y;
        public bool state;
        public bool nextState;
        public Transform parentTransform;
        public GameObject cellFab;
        private GameObject cellRef;
        public List<Cell> neighbors;
        public Material stateMat;
        public Material unstateMat;

        public Cell (float x, float y, Transform transform, GameObject cellFab){

            // class properties should be used
            // with 'this' to be accesbile from outside

            this.x = x;
            this.y = y;
            this.parentTransform = transform;
            this.cellFab = cellFab;

            if (Random.Range(0f,2f) &gt; 1) {
              nextState = true;
            }
            else {
              nextState = false;
            }

            state = nextState;

            neighbors = new List<Cell>();

            stateMat = Resources.Load("StateMat", typeof(Material)) as Material;
            unstateMat = Resources.Load("UnStateMat", typeof(Material)) as Material;

            Spawn();
            MoveStuff();
        }

        public void Spawn(){
            cellRef = Instantiate(cellFab, parentTransform.position, parentTransform.rotation) as GameObject;
        }

        public void MoveStuff(){
            cellRef.transform.position = new Vector3(this.x, this.y, cellRef.transform.position.z);
            cellRef.transform.parent = parentTransform;
        }

        public void AddNeighbor( Cell cell ){
            neighbors.Add(cell);
        }

        public void CalculateNextState(){

            int liveCount = 0;

            for (int i=0; i < neighbors.Count; i++) {
              if (neighbors[i].state == true) {
                liveCount++;
              }
            }

            if (state == true) {
              if ((liveCount == 2) || (liveCount == 3)) {
                nextState = true;
              }
              else {
                nextState = false;
              }
            }
            else {
              if (liveCount == 3) {
                nextState = true;
              }
              else {
                nextState = false;
              }
            }

        }

        public void DrawCell (){

            state = nextState;

            if (state == true) {
                // add black material
                cellRef.renderer.sharedMaterial = stateMat;
            }
            else {
                // add white material
                cellRef.renderer.sharedMaterial = unstateMat;
            }
        }

    } // † End Of Cell Class † //

    // Use this for initialization
    void Start () {

        _numX = (int)Mathf.Floor(canvasW/_cellSize);
        _numY = (int)Mathf.Floor(canvasH/_cellSize);
        InitGame();

    }

    void InitGame(){

        _cellArray = new Cell [_numX,_numY];

        for(int x=0; x<_numX; x++){
            for(int y=0; y<_numY; y++){

                Transform containerPos = gameObject.transform;
                cell = new Cell(x * .5f, y * .5f, containerPos, cellPrefab);
                _cellArray[x,y] = cell;

            }
        }

        for (int x = 0; x < _numX; x++) {
            for (int y = 0; y < _numY; y++) {

                int above = y-1;
                int below = y+1;
                int left = x-1;
                int right = x+1;

                if (above < 0) {
                    above = _numY-1;
                }

                if (below == _numY) {
                    below = 0;
                }

                if (left < 0) {
                    left = _numX-1;
                }

                if (right == _numX) {
                    right = 0;
                }

                _cellArray[x,y].AddNeighbor(_cellArray[left,above]);
                _cellArray[x,y].AddNeighbor(_cellArray[left,y]);
                _cellArray[x,y].AddNeighbor(_cellArray[left,below]);
                _cellArray[x,y].AddNeighbor(_cellArray[x,below]);
                _cellArray[x,y].AddNeighbor(_cellArray[right,below]);
                _cellArray[x,y].AddNeighbor(_cellArray[right,y]);
                _cellArray[x,y].AddNeighbor(_cellArray[right,above]);
                _cellArray[x,y].AddNeighbor(_cellArray[x,above]);

            }
        }
    }

    // Update is called once per frame
    void Update () {

        for (int x=0;x < _numX;x++) {
            for (int y=0;t_numY;y++) {
                _cellArray[x,y].CalculateNextState();
            }
        }

        for (int x=0;x&lt;_numX;x++) {
            for (int y=0;y < _numY;y++) {
                _cellArray[x,y].DrawCell();
            }
        }

    }

}
comments powered by Disqus