void Start() { // Turn off scripts we don't want for any players in the scene List <GameObject> players = new List <GameObject> (GameObject.FindGameObjectsWithTag("Player")); foreach (GameObject player in players) { FireShots playerFireShots = player.GetComponent <FireShots> (); Rigidbody2D playerRB = player.GetComponent <Rigidbody2D> (); SpaceShipMovement playerMovement = player.GetComponent <SpaceShipMovement> (); if (playerFireShots) { playerFireShots.enabled = false; } if (playerRB) { playerRB.Sleep(); } if (playerMovement) { playerMovement.enabled = false; } } }
// This is what we call when hit "Play" from the UI public void StartGame() { // Hide the start UI infoPanel.SetActive(false); // Turn on scripts we don't want for any players in the scene List <GameObject> players = new List <GameObject> (GameObject.FindGameObjectsWithTag("Player")); foreach (GameObject player in players) { FireShots playerFireShots = player.GetComponent <FireShots> (); Rigidbody2D playerRB = player.GetComponent <Rigidbody2D> (); SpaceShipMovement playerMovement = player.GetComponent <SpaceShipMovement> (); if (playerFireShots) { playerFireShots.enabled = true; } if (playerRB) { playerRB.WakeUp(); } if (playerMovement) { playerMovement.enabled = true; } } GameState.currentState = GameState.AvailableStates.Playing; }
void DisableComponents() { // Disable everything until sound has played PlayerSounds pS = gameObject.GetComponent <PlayerSounds> (); FireShots fS = gameObject.GetComponent <FireShots> (); SpaceShipMovement sS = gameObject.GetComponent <SpaceShipMovement> (); Rigidbody2D rb = gameObject.GetComponent <Rigidbody2D> (); Collider2D coll = gameObject.GetComponent <Collider2D> (); fS.enabled = false; sS.enabled = false; coll.enabled = false; rb.Sleep(); foreach (Transform child in transform) { child.gameObject.SetActive(false); } pS.gunSource.Stop(); pS.thrustersSource.Stop(); }
// Called when the player dies public void End() { // We instantiate lots of players for tiling, we need to turn scripts off List <GameObject> players = new List <GameObject> (GameObject.FindGameObjectsWithTag("Player")); foreach (GameObject player in players) { FireShots playerFireShots = player.GetComponent <FireShots> (); Rigidbody2D playerRB = player.GetComponent <Rigidbody2D> (); SpaceShipMovement playerMovement = player.GetComponent <SpaceShipMovement> (); if (playerFireShots) { playerFireShots.enabled = false; } if (playerRB) { playerRB.Sleep(); } if (playerMovement) { playerMovement.enabled = false; } } // Did we get a new high score? if (scoreCtrl.newHighScore && scoreCtrl.currentScore > 0) { highScoreNotification.SetActive(true); } //Show the end UI endPanel.SetActive(true); // Prevent opening the pause menu, etc GameState.currentState = GameState.AvailableStates.Ended; }
// Disable OR enable things on pause or unpause void SetComponentStats(bool state = true) { pausePanel.SetActive(!state); // If we're pausing, clear the rigidbody's dictionary so we can store all the latest velocities for un-pausing if (!state) { previousVelocities = new Dictionary <Rigidbody2D, Vector2> (); } objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Asteroid")); objectsToDisable.AddRange(GameObject.FindGameObjectsWithTag("IncomingAsteroid")); foreach (GameObject asteroid in objectsToDisable) { rb = asteroid.GetComponent <Rigidbody2D> (); coll = asteroid.GetComponent <Collider2D> (); asteroidScript = asteroid.GetComponent <Asteroid> (); incomingAsteroid = asteroid.GetComponent <IncomingAsteroid> (); if (rb) { if (state == true) { rb.WakeUp(); if (previousVelocities.ContainsKey(rb)) { rb.velocity = previousVelocities [rb]; } } else { previousVelocities [rb] = rb.velocity; rb.Sleep(); } } if (coll) { coll.enabled = state; } if (asteroidScript) { asteroidScript.enabled = state; } if (incomingAsteroid) { incomingAsteroid.enabled = state; } } // TODO: could be cached as players should always be visible objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Player")); foreach (GameObject player in objectsToDisable) { rb = player.GetComponent <Rigidbody2D> (); coll = player.GetComponent <Collider2D> (); fireShotScript = player.GetComponent <FireShots> (); moveScript = player.GetComponent <SpaceShipMovement> (); if (rb) { if (state == true) { rb.WakeUp(); if (previousVelocities.ContainsKey(rb)) { rb.velocity = previousVelocities [rb]; } } else { previousVelocities [rb] = rb.velocity; rb.Sleep(); } } if (coll) { coll.enabled = state; } if (fireShotScript) { fireShotScript.enabled = state; } if (moveScript) { moveScript.enabled = state; } } objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Laser")); foreach (GameObject laser in objectsToDisable) { rb = laser.GetComponent <Rigidbody2D> (); coll = laser.GetComponent <Collider2D> (); if (rb) { if (state == true) { rb.WakeUp(); if (previousVelocities.ContainsKey(rb)) { rb.velocity = previousVelocities [rb]; } } else { previousVelocities [rb] = rb.velocity; rb.Sleep(); } } if (coll) { coll.enabled = state; } } }