/** @brief GameState transitioned from GameStart to GamePlay. */ void BeginPlay() { this.SpawnShip(this.shipPrefab); // Spawn large asteroids for (uint i = 0; i < this.currentLevel * 3; i++) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, 0.0f); GameObject newAsteroid = Instantiate(this.asteroidLargePrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "LargeAsteroid" + i.ToString(); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Spawn medium asteroids for (uint i = 0; i < this.currentLevel * 3; i++) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, 0.0f); GameObject newAsteroid = Instantiate(this.asteroidMediumPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "MediumAsteroid" + i.ToString(); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } GameStateScript.gameState = GameState.GamePlay; }
/** * @brief Scene showed to players before an active game session. * @details This should show the game title, Asteroids, have an option to * PLAY GAME or see HIGH SCORES. There will be asteroids flying around * randomly in the background. */ private void GameStart() { // Reusing this is okay, since each reference (attaching to a parent // transform, for example) retains the GameObject. GameObject guiGO; // Large ASTEROIDS title: GO and its component guiGO = new GameObject("guiAsteroidsTitle"); guiGO.transform.parent = this.guiElements.transform; guiGO.transform.localPosition = new Vector3(0.5f, 0.65f, 0.0f); guiGameStartTitle = guiGO.AddComponent <GUIText>(); guiGameStartTitle.text = "ASTEROIDS"; guiGameStartTitle.anchor = TextAnchor.MiddleCenter; guiGameStartTitle.alignment = TextAlignment.Center; guiGameStartTitle.pixelOffset = Vector2.zero; guiGameStartTitle.lineSpacing = 1.0f; guiGameStartTitle.tabSize = 4.0f; guiGameStartTitle.fontSize = 50; guiGameStartTitle.fontStyle = FontStyle.Bold; // Smaller "Press SPACE" text: GO and its component guiGO = new GameObject("guiPressSpace"); guiGO.transform.parent = this.guiElements.transform; guiGO.transform.localPosition = new Vector3(0.5f, 0.4f, 0.0f); guiGameStartPlay = guiGO.AddComponent <GUIText>(); guiGameStartPlay.text = "Press SPACE to begin"; guiGameStartPlay.anchor = TextAnchor.MiddleCenter; guiGameStartPlay.alignment = TextAlignment.Center; guiGameStartPlay.pixelOffset = Vector2.zero; guiGameStartPlay.lineSpacing = 1.0f; guiGameStartPlay.tabSize = 4.0f; guiGameStartPlay.fontSize = 20; guiGameStartPlay.fontStyle = FontStyle.Normal; // Bunch of random asteroids flying around. These will not be used in // the actual game; they are just austhetics while we wait for the // player to hit SPACE. int numAsteroids = 30; // All asteroids here will be set to increasing z-depths so they don't // look funky as they overlap each other. float startingZ = 0.0f; for (; startingZ < (float)(numAsteroids / 3.0f); startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidLargePrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "LargeAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Resume at previous z-depth for (; startingZ < (float)(numAsteroids * 2.0f / 3.0f); startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidMediumPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "MediumAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Keep going at previous z-depth for (; startingZ < (float)numAsteroids; startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidSmallPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "SmallAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } GameStateScript.UserInputPlay += UserInputPlay; }