Game of Life

Over dit spel...

Conway's Game of Life met volledig instelbare regelset. Ook heeft het een instelbare game snelheid en faden de blokjes mooi in en uit.

Al de blokjes zijn individuele objecten zodat ze makkelijk in en uit kunnen faden. De Game Manager bepaalt welke objecten moeten faden en vervolgens regelen de blokjes zelf hoe ze faden.

Engine: Unity/C#
Platform: Windows

GameManager.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class GameManager : MonoBehaviour
{
    [Header("Map size")]
    [SerializeField]
    int width;
    [SerializeField]
    int height;

    int maxNeighbours, minNeighbours, forNewLife;

    GameObject[,] tiles;
    [Header("Map prefabs")]
    [SerializeField]
    GameObject tile;

    [Header("Game speed")]
    [SerializeField]
    Slider slider;

    float timeScale;

    float checkTimer = 1;
    // Use this for initialization
    public void StartGame(float newWidth, float newHeight, float newMaxNeighbours, float newMinNeighbours, float newForNewLife)
    {
        width = Mathf.RoundToInt(newWidth);
        height = Mathf.RoundToInt(newHeight);
        maxNeighbours = Mathf.RoundToInt(newMaxNeighbours);
        minNeighbours = Mathf.RoundToInt(newMinNeighbours);
        forNewLife = Mathf.RoundToInt(newForNewLife);

        tiles = new GameObject[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                tiles[x, y] = Instantiate(tile, new Vector3(x, y, 0), Quaternion.identity) as GameObject;

                if (Random.Range(0, 10) == 1)
                {
                    tiles[x, y].GetComponent().Switch();
                }
            }
        }

        //Camera.main.transform.position = new Vector3(width / 2 - 0.5f, height / 2 - 0.5f, -10);

        timeScale = slider.value;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            checkTimer = 0;
        }

        checkTimer -= Time.deltaTime * timeScale;
        if (checkTimer <= 0)
        {
            List switchList = new List();
            //Complete ruleset
            for (int x = 0; x < tiles.GetLength(0); x++)
            {
                for (int y = 0; y < tiles.GetLength(1); y++)
                {
                    int neighbours = 0;
                    if (CheckNeighbour(x - 1, y - 1)) neighbours++;
                    if (CheckNeighbour(x, y - 1)) neighbours++;
                    if (CheckNeighbour(x + 1, y - 1)) neighbours++;

                    if (CheckNeighbour(x - 1, y)) neighbours++;
                    if (CheckNeighbour(x + 1, y)) neighbours++;

                    if (CheckNeighbour(x - 1, y + 1)) neighbours++;
                    if (CheckNeighbour(x, y + 1)) neighbours++;
                    if (CheckNeighbour(x + 1, y + 1)) neighbours++;

                    //Should only be switched at end of checking
                    if ((tiles[x,y].GetComponent().IsActive) && (neighbours < minNeighbours || neighbours > maxNeighbours)) switchList.Add(tiles[x, y].GetComponent());
                    if ((!tiles[x, y].GetComponent().IsActive) && (neighbours == forNewLife)) switchList.Add(tiles[x, y].GetComponent());
                }
            }

            foreach(Tile tile in switchList) tile.Switch();

            checkTimer = 1;
        }
    }

    bool CheckNeighbour(int x, int y)
    {
        if (x >= 0 && x < tiles.GetLength(0) && y >= 0 && y < tiles.GetLength(1) && tiles[x, y].GetComponent().IsActive) return true;

        else return false;
    }

    public void ChangeTimeScale()
    {
        timeScale = slider.value;
    }

    public void EmptyMap()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (tiles[x, y].GetComponent().IsActive) tiles[x, y].GetComponent().Switch();
            }
        }
    }
}

Downloads

Windows
Source