// Function to print next generation private GridCell[,] ExecuteGameOfLife(GridCell[,] grid, int M, int N, SpawningPhase phase) { GridCell[,] future = new GridCell[M, N]; future = grid; future = FindNeighbours(grid, M, N); for (int x = 0; x < M; x++) { for (int y = 0; y < N; y++) { // Implementing the Rules of Life switch (phase) { case SpawningPhase.BIG: future = GameOfLifeBig(x, y, future); break; case SpawningPhase.MEDIUM: future = GameOfLifeMedium(x, y, future); break; } } } return(future); }
private IEnumerator IterateGameOfLife(GridCell[,] grid, int M, int N, SpawningPhase phase, int iterateAmount) { yield return(new WaitForSeconds(0.00f)); if (hasIterated < iterateAmount) { hasIterated++; this.grid = ExecuteGameOfLife(grid, M, N, phase); StartCoroutine(IterateGameOfLife(grid, M, N, phase, iterateAmount)); } else { SpawnObjects(); } }
IEnumerator SpawnObsPattern(float duration) { float currentPhaseTime = 0f; // Randomize and Spawn Patterns while (spawning && duration > currentPhaseTime) { GameObject pattern = obsPatterns[Random.Range(0, obsPatterns.Length)]; Instantiate( pattern, spawnPosition.position, Quaternion.identity ); // Accumulate Time currentPhaseTime += waveInterval; yield return(new WaitForSeconds(waveInterval)); } currentPhase = SpawningPhase.Collectibles; }
private void GoToNextPhase(SpawningPhase oldPhase) { hasIterated = 0; totalAlive = 0; switch (oldPhase) { case SpawningPhase.BIG: currentSpawningPhase = SpawningPhase.MEDIUM; aliveThreshold = aliveThresholdMedium; break; case SpawningPhase.MEDIUM: currentSpawningPhase = SpawningPhase.MISC; aliveThreshold = aliveThresholdMisc; break; case SpawningPhase.MISC: transform.rotation = originalRotation; insideContext.SetActive(true); return; } for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { SetUpGrid(x, y); } } if (totalAlive < 3) { GoToNextPhase(oldPhase); } grid = FindNeighbours(grid, columns, rows); StartCoroutine(IterateGameOfLife(grid, columns, rows, currentSpawningPhase, 5)); }
IEnumerator SpawnCollectibles(float duration) { float currentPhaseTime = 0f; // Randomize and Spawn Patterns while (spawning && duration > currentPhaseTime) { GameObject collectible = collectibles[Random.Range(0, collectibles.Length)]; // Collectibles are Spawned in Bulk into Groups Instantiate( collectible, spawnPosition.position, Quaternion.identity ); // - Pause between Groups currentPhaseTime += waveInterval; yield return(new WaitForSecondsRealtime(waveInterval)); // - Move on to new Patterns until duration Over } // Switch Phase currentPhase = SpawningPhase.Obstacles; }
// Sets up grid and spawns objects using game of life public void Initialize() { originalRotation = transform.rotation; transform.rotation = Quaternion.identity; // Calculating grid size horizontalCellSize = (float)gridSizeX / (float)columns; verticalCellSize = (float)gridSizeZ / (float)rows; grid = new GridCell[columns, rows]; gridCellsOffset = new Vector3(-gridSizeX / 2, gridCellsOffset.y, -gridSizeZ / 2); // Resetting Game of Life variables aliveThreshold = aliveThresholdBig; currentSpawningPhase = SpawningPhase.BIG; totalAlive = 0; // Spawning grid for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { GameObject gridCell = Instantiate(gridCellPrefab); gridCell.name = "Cell: " + i + ", " + j; gridCell.transform.position = new Vector3(i * horizontalCellSize + horizontalCellSize * 0.5f, 0f, j * verticalCellSize + verticalCellSize * 0.5f) + transform.position + gridCellsOffset; gridCell.transform.parent = cellParent; GridCell cellInfo = gridCell.GetComponent <GridCell>(); cellInfo.vertical = i; cellInfo.horizontal = j; cellInfo.scale = new Vector3(horizontalCellSize, 0.1f, verticalCellSize); if (cellInfo.vertical < columns / 2) { cellInfo.rotation = GridCell.RotateTowards.TOP; } else if (cellInfo.vertical >= columns / 2) { cellInfo.rotation = GridCell.RotateTowards.BOTTOM; } if (cellInfo.horizontal == 0) { if (cellInfo.vertical > 0) { if (cellInfo.vertical <= rows - 1) { cellInfo.rotation = GridCell.RotateTowards.LEFT; } } } else if (cellInfo.horizontal == columns - 1) { if (cellInfo.vertical > 0 && cellInfo.vertical < rows - 1) { cellInfo.rotation = GridCell.RotateTowards.RIGHT; } } grid[i, j] = cellInfo; SetUpGrid(i, j); cells.Add(cellInfo); } } grid = FindNeighbours(grid, columns, rows); StartCoroutine(IterateGameOfLife(grid, columns, rows, currentSpawningPhase, 15)); }