Example #1
0
    // Start is called before the first frame update
    void Start()
    {
        grid = new CellScript[gridWidth, gridHeight];

        //Using nested for loops, instantiate cubes with cell scripts in a way such that
        //	each cell will be places in a top left oriented coodinate system.
        //	I.e. the top left cell will have the x, y coordinates of (0,0), and the bottom right will
        //	have the coodinate (gridWidth-1, gridHeight-1)
        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                //Create a cube, position/scale it, add the CellScript to it.
                Vector3 pos = new Vector3(x * (cellDimension + cellSpacing),
                                          0,
                                          y * (cellDimension + cellSpacing));
                GameObject cellObj = Instantiate(cellPrefab, pos, Quaternion.identity);
                CellScript cs      = cellObj.AddComponent <CellScript>();
                cs.x     = x;
                cs.y     = y;
                cs.alive = (Random.value > 0.5f) ? true : false;
                cs.updateColor();

                cellObj.transform.position   = pos;
                cellObj.transform.localScale = new Vector3(cellDimension,
                                                           cellDimension,
                                                           cellDimension);
                //Finally add the cell to it's place in the two dimensional array
                grid[x, y] = cs;
            }
        }
        //Initialize the timer
        generationTimer = generationRate;
    }
Example #2
0
    // Start is called before the first frame update
    void Start()
    {
        initValues();

        this.grid    = new CellScript[this.gridHeight, this.gridWidth];
        this.cubeObj = new GameObject[this.gridHeight, this.gridWidth];

        //Using nested for loops, instantiate cubes with cell scripts in a way such that
        //	each cell will be places in a top left oriented coodinate system.
        //	I.e. the top left cell will have the x, y coordinates of (0,0), and the bottom right will
        //	have the coodinate (gridHeight-1, gridWidth-1)
        for (int row = 0; row < this.gridHeight; row++)
        {
            for (int col = 0; col < this.gridWidth; col++)
            {
                //Create a cube, position/scale it, add the CellScript to it.
                Vector3 pos = new Vector3(row * (this.cellDimension + this.cellSpacing), 0, col * (this.cellDimension + this.cellSpacing));

                GameObject cellObj = Instantiate(this.cellPrefab, pos, Quaternion.identity);
                this.cubeObj[row, col] = Instantiate(this.cubePrefab, pos + new Vector3(0, 3.3f, 0), Quaternion.identity);

                CellScript cs = cellObj.AddComponent <CellScript>();
                cs.value = Random.Range(0, 2);                 //0 or 1
                cs.updateColor();

                cellObj.transform.position   = pos;
                cellObj.transform.localScale = new Vector3(cellDimension, cellDimension, cellDimension);
                //Finally add the cell to it's place in the two dimensional array
                this.grid[row, col] = cs;
            }
        }
        //Initialize the timer
        this.generationTimer = this.generationRate;
    }
Example #3
0
    public bool everAliveReproduction = true; //true默认可以繁殖

    // Start is called before the first frame update
    void Start()
    {
        // Instantiate a 2D array
        grid = new CellScript[gridWidth, gridHeight];

        GameObject cellFather = new GameObject();

        cellFather.name = "cellFather";

        // Instantiate a grid of cubes ("cells") and position it according to its
        // place in the 2 dimensional array ("grid").
        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                Vector3    pos     = new Vector3(x * (cellDimension + padding), 0, y * (cellDimension + padding));
                GameObject cellObj = Instantiate(cellPrefab, pos, Quaternion.identity);
                cellObj.transform.SetParent(cellFather.transform);
                cellObj.transform.localScale = new Vector3(cellDimension, cellDimension, cellDimension);
                CellScript cs = cellObj.GetComponent <CellScript>();
                cs.possibilitiesOfAlive = possibilitiesOfEachCellBeingAlive;
                cs.x       = x;
                cs.y       = y;
                grid[x, y] = cs;
            }
        }
    }
Example #4
0
    List <int[]> CheckNeighbors(int[] CUR, CellScript[,] tempGrid)
    {
        List <int[]> answers = new List <int[]> ();

        int[] temp = new int[2];
        if (CUR[0] - 1 >= 0 && tempGrid[CUR[0] - 1, CUR[1]].visited == false)
        {
            temp[0] = CUR[0] - 1;
            temp[1] = CUR[1];
            answers.Add((int[])temp.Clone());
        }
        if (CUR[0] + 1 < numRows && tempGrid[CUR[0] + 1, CUR[1]].visited == false)
        {
            temp[0] = CUR[0] + 1;
            temp[1] = CUR[1];
            answers.Add((int[])temp.Clone());
        }
        if (CUR[1] - 1 >= 0 && tempGrid[CUR[0], CUR[1] - 1].visited == false)
        {
            temp[0] = CUR[0];
            temp[1] = CUR[1] - 1;
            answers.Add((int[])temp.Clone());
        }
        if (CUR[1] + 1 < numCols && tempGrid[CUR[0], CUR[1] + 1].visited == false)
        {
            temp[0] = CUR[0];
            temp[1] = CUR[1] + 1;
            answers.Add((int[])temp.Clone());
        }
        return(answers);
    }
Example #5
0
    void Start()
    {
        var fileLines = File.ReadLines(config.PathLevel(num));

        var fileLinesArray = fileLines.ToArray();
        var sizeLine       = fileLinesArray[0].Split(' ');
        var v = int.Parse(sizeLine[0]);
        var h = int.Parse(sizeLine[1]);

        helper = new CellScript[v, h];


        for (var i = 0; i < v; i++)
        {
            var line = fileLinesArray[i + 1];
            for (var j = 0; j < h; j++)
            {
                helper[i, j] = Instantiate(prefab, transform);
                helper[i, j].transform.position = new Vector3(i, 0, j);

                helper[i, j].Create(line[j] - '0');
                if (line[j] - '0' == 2)
                {
                    playerPosX = i;
                    playerPosZ = j;
                }
            }
        }

        //Queue((int)PlayerSkript.Self.transform.position.x, (int)PlayerSkript.Self.transform.position.z);
    }
Example #6
0
    protected override void Awake()
    {
        base.Awake();

        cell      = new CellScript[gridSize, gridSize];
        pathStack = new Stack <PathScript>(totalPathCount);
        nodeStack = new Stack <CellScript>(gridSize);
    }
    bool LoadMap()
    {
        // Check for errors
        if (_mapSize <= 0) {
            Debug.Log("[Error] " + this + ".LoadMap() : _mapSize must be supperior to one for this script to work.");
            return false;
        }

        _cellArray = new CellScript[_mapSize, _mapSize];

        return true;
    }
Example #8
0
    bool LoadMap()
    {
        // Check for errors
        if (_mapSize <= 0)
        {
            Debug.Log("[Error] " + this + ".LoadMap() : _mapSize must be supperior to one for this script to work.");
            return(false);
        }

        _cellArray = new CellScript[_mapSize, _mapSize];

        return(true);
    }
 // Start is called before the first frame update
 void Start()
 {
     grid = new CellScript[gridWidth, gridHeight];
     for (int x = 0; x < gridWidth; x++)
     {
         for (int y = 0; y < gridHeight; y++)
         {
             Vector3    pos     = new Vector3(x * cellDimension + padding, 0, y * cellDimension + padding);
             GameObject cellObj = Instantiate(cellPrefab, pos, Quaternion.identity);
             cellObj.transform.localScale = new Vector3(cellDimension, cellDimension, cellDimension);
             CellScript cs = cellObj.GetComponent <CellScript>();
             grid[x, y] = cs;
         }
     }
 }
Example #10
0
    // Use this for initialization
    void Start()
    {
        //Instantiate the empty two dimesnional array
        grid            = new CellScript[GameManager.numCellsW, GameManager.numCellsH];
        scoreValue.text = "Score: " + score;

        //Using 'nested for loops', instantiate cubes with cell scripts in a way such that
        //	each cell will be places in a top left oriented coodinate system.
        //	I.e. the top left cell will have the x, y coordinates of (0,0), and the bottom right will
        //	have the coodinate (numCellsW-1, numCellsH-1)
        for (int y = 0; y < GameManager.numCellsH; y++)
        {
            for (int x = 0; x < GameManager.numCellsW; x++)
            {
                if (x == 2 && y == 28)
                {
                    GameObject cube       = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    CellScript cellScript = cube.AddComponent <CellScript> ();
                    cellScript.x            = x;
                    cellScript.y            = y;
                    GameManager.grid [x, y] = cellScript;
                    //Create the position of the cube that represents the cell, based on the cell's x,y coordinate
                    //	Note that by making the x,y positions be something like "x * (cellWidth + cellSpacing)" we
                    //	can have arbitrarily sized cells with spacing.
                    Vector3 pos = new Vector3(x * (cellWidth + cellSpacing), y * (cellHeight + cellSpacing), 0);
                    cube.transform.position = pos;
                    cellScript.isGoalCell   = true;
                    cellScript.alive        = false;
                }
                else
                {
                    GameObject cube       = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    CellScript cellScript = cube.AddComponent <CellScript> ();
                    cellScript.x            = x;
                    cellScript.y            = y;
                    GameManager.grid [x, y] = cellScript;
                    //Create the position of the cube that represents the cell, based on the cell's x,y coordinate
                    //	Note that by making the x,y positions be something like "x * (cellWidth + cellSpacing)" we
                    //	can have arbitrarily sized cells with spacing.
                    Vector3 pos = new Vector3(x * (cellWidth + cellSpacing), y * (cellHeight + cellSpacing), 0);
                    cube.transform.position = pos;
                }
            }
        }
    }
Example #11
0
    List <int[]> CheckDirections(int[] CUR, CellScript[,] tempGrid)
    {
        List <int[]> answers = new List <int[]> ();

        int[] temp = new int[2];
        if (CUR[0] - 1 >= 0 && tempGrid[CUR[0] - 1, CUR[1]].back == true)
        {
            if (tempGrid[CUR[0] - 1, CUR[1]].WallsUp() >= 3 || tempGrid[CUR[0] - 1, CUR[1]].visited == true)
            {
                temp[0] = CUR[0] - 1;
                temp[1] = CUR[1];
                answers.Add((int[])temp.Clone());
            }
        }
        if (CUR[0] + 1 < numRows && tempGrid[CUR[0] + 1, CUR[1]].front == true)
        {
            if (tempGrid[CUR[0] + 1, CUR[1]].WallsUp() >= 3 || tempGrid[CUR[0] + 1, CUR[1]].visited == true)
            {
                temp[0] = CUR[0] + 1;
                temp[1] = CUR[1];
                answers.Add((int[])temp.Clone());
            }
        }
        if (CUR[1] - 1 >= 0 && tempGrid[CUR[0], CUR[1] - 1].right == true)
        {
            if (tempGrid[CUR[0], CUR[1] - 1].WallsUp() >= 3 || tempGrid[CUR[0], CUR[1] - 1].visited == true)
            {
                temp[0] = CUR[0];
                temp[1] = CUR[1] - 1;
                answers.Add((int[])temp.Clone());
            }
        }
        if (CUR[1] + 1 < numCols && tempGrid[CUR[0], CUR[1] + 1].left == true)
        {
            if (tempGrid[CUR[0], CUR[1] + 1].WallsUp() >= 3 || tempGrid[CUR[0], CUR[1] + 1].visited == true)
            {
                temp[0] = CUR[0];
                temp[1] = CUR[1] + 1;
                answers.Add((int[])temp.Clone());
            }
        }
        return(answers);
    }
Example #12
0
    public void CollectChildren()
    {
        Children.Clear();
        foreach (Transform child in transform)
        {
            Children.Add(child.gameObject);
        }

        cells  = new GameObject[RowsX, ColsZ];
        cellsS = new CellScript[RowsX, ColsZ];
        foreach (GameObject Cell in Children)
        {
            int xPos = (int)(Cell.transform.localPosition.x);
            int zPos = (int)(Cell.transform.localPosition.z);

            cells [(xPos / 2), (zPos / 2)]  = Cell;
            cellsS [(xPos / 2), (zPos / 2)] = cells [(xPos / 2), (zPos / 2)].GetComponent <CellScript> ();
        }
    }
Example #13
0
    public int maxY = 0; //y position of highest cell

    // Start is called before the first frame update
    void Start()
    {
        //Instantiating 2D array for cell scripts
        grid = new CellScript[gridWidth, gridHeight];

        /* Using nested for loops, instantiate cubes with cell scripts in a way such that
         * each cell will be places in a top left oriented coodinate system.
         * The top left cell will have the x, y coordinates of (0,0), and the bottom right will
         * have the coodinate (gridWidth-1, gridHeight-1).
         */
        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                //Create a cube, position/scale it, add the CellScript to it.
                Vector3 pos = new Vector3(x * (cellDimension + cellSpacing), 0, y * (cellDimension + cellSpacing));

                //Creates a new game object like we do in the Unity engine, but in code form
                GameObject cellObj = Instantiate(cellPrefab, pos, Quaternion.identity);

                //Casting the cell script from the Unity engine, type of component
                CellScript cs = cellObj.AddComponent <CellScript>();
                cs.x = x;
                cs.y = y;

                //Question ?: true - do first thing, false - do the second thing
                cs.alive = (Random.value > 0.5f) ? true : false;

                cs.updateColor();

                //Position
                cellObj.transform.position   = pos;
                cellObj.transform.localScale = new Vector3(cellDimension, cellDimension, cellDimension);

                grid[x, y] = cs; //adding it to 2D array
            }
        }
        generationTimer = generationRate;
        alertText.text  = "Step onto the white and press toggle.";
    }
Example #14
0
    // Initialize grid of specified size
    private void CreateGrid()
    {
        // Create cell objects to from grid
        grid = new CellScript[gridSize,gridSize];
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                Vector3 cellPos = new Vector3(i * cellSize[0], 0, j * cellSize[1]);
                GameObject newCell = Instantiate(gridCell, cellPos, Quaternion.identity) as GameObject;
                CellScript newCellScript = newCell.GetComponent<CellScript>();
                newCellScript.Init();
                newCellScript.SetGridPosition(i, j);
                newCellScript.SetVisible(false);
                newCell.transform.localScale = new Vector3(cellSize[0], 0.5f, cellSize[1]);
                grid[i,j] = newCellScript;
            }
        }

        // Create bases for each player
        // Create base on grid for Player 1
        GameObject player1Base = Instantiate(playerBase, new Vector3(0,0.5f,10), Quaternion.identity) as GameObject;
        BaseScript player1BaseScript = player1Base.GetComponent<BaseScript>();
        gameScript.bases.Add(player1BaseScript);
        player1BaseScript.Init();
        player1BaseScript.myPlayerType = GameScript.PlayerType.Player1;
        Debug.Log("Player1 Playertype: " + player1BaseScript.myPlayerType);

        for (int i = 0; i < 10; i++) {
            grid[0, 10 + i].SetBase(Color.magenta);
            player1BaseScript.GetSection(i).renderer.material.color = Color.magenta;
            player1BaseScript.cells.Add(grid[0,10+i]);
        }

        // Create base on grid for Player 2
        GameObject player2Base = Instantiate(playerBase, new Vector3(29,0.5f,10), Quaternion.identity) as GameObject;
        BaseScript player2BaseScript = player2Base.GetComponent<BaseScript>();
        gameScript.bases.Add(player2BaseScript);
        player2BaseScript.Init();
        player2BaseScript.myPlayerType = GameScript.PlayerType.Player2;
        Debug.Log("Player2 Playertype: " + player2BaseScript.myPlayerType);

        for (int i = 0; i < 10; i++) {
            grid[29, 10 + i].SetBase(Color.cyan);
            player2BaseScript.GetSection(i).renderer.material.color = Color.cyan;
            player2BaseScript.cells.Add(grid[29,10+i]);
        }

        refreshReef();
    }