// Update is called once per frame void FixedUpdate() { // Once you have selected an item hide the store menu if (Selected != -1) { IGS.SetActive(false); } LogIn(); // Boolean which tells if it's a single player game SinglePlayer = GetComponent <TitleScreenController>().SinglePlayer; // Get all the players currently in the game Players = GameObject.FindGameObjectsWithTag("Player"); // Find the game mode selection sprites if (_gameMode.Length == 0) { _gameMode = GameObject.FindGameObjectsWithTag("Mode"); } // When you quit a game or the host disconnects reset all the background variables and return to the home screen if (LoggedIn && (Input.GetKey(KeyCode.Escape) || (_networkController.Client && !_networkController.Server && Players.Length <= 1))) { Clear(); foreach (GameObject g in _background) { g.GetComponent <BackgroundController>().New(0); } foreach (GameObject z in GameObject.FindGameObjectsWithTag("Zone")) { Destroy(z); } } // Called whilst in the lobby but the game has not started yet WaitScreen(); // Handles player rankings RankingManager(); // Countdown between game initialization and it starting StartCounting(); // Handles Game mode for multiplayer GameType(); if (_running) { Run(); } }
//Clear data if not connected to a server or when you quit the game public void Clear() { Selected = -1; Chosen = false; _storeLoaded = false; // Reset the game mode sprites _gameMode = new GameObject[0]; // Reset the score menu SharedScore.GetComponent <PlaceController>().Reset(); SharedScore.SetActive(false); // Hide all the sprites IGS.SetActive(false); Countdown[0].SetActive(false); Countdown[1].SetActive(false); Countdown[2].SetActive(false); Wait.SetActive(false); Host.SetActive(false); Tab.SetActive(false); foreach (Transform child in Dots.transform) { child.gameObject.SetActive(false); } foreach (Transform t in Ability.GetComponentsInChildren <Transform>()) { t.GetComponent <SpriteRenderer>().enabled = false; } // destroy the mode selectors. They will automatically delete themselves anyway but there is a delay GameObject[] modes = GameObject.FindGameObjectsWithTag("Mode"); foreach (GameObject m in modes) { Destroy(m); } // Reset the dodgeball generators foreach (GameObject DBG in DBGS) { DBG.GetComponent <DBGenerator>().Counter = 0; DBG.GetComponent <DBGenerator>().InGame = false; } // Destroy all the dodgeballs foreach (GameObject b in GameObject.FindGameObjectsWithTag("DodgeBall")) { Destroy(b); } // Destroy all the players in a single player game. Automatically done // in online games if (SinglePlayer) { foreach (GameObject p in GameObject.FindGameObjectsWithTag("Player")) { Destroy(p); } } // Reset variables SinglePlayer = false; _running = false; Started = false; StartCounter = -1; // Go to the home page GetComponent <TitleScreenController>().Home(); // Reset the network controller if (_networkController.Client && !_networkController.Server) { _networkController.Client = false; _networkController.Server = false; _networkController.Players = 0; _networkManager.StopClient(); } // If you are the server host tell all the clients to disconnect if (_networkController.Server) { NetController.GetComponent <NetworkController>().RpcDisconnect(GameObject.FindGameObjectsWithTag("Player").Length); } // Hide all end screen sprites GetComponent <WinnerScreen>().Clear(); }
//The countdown between when TAB is pressed and when the game actually starts void StartCounting() { // StartCounter is -1 until Tab is pressed and after the countdown it is 0 if (StartCounter > 0) { // Countdown has just started if (StartCounter == 61) { IGS.SetActive(false); // If its a co-op or single player game turn on the big score counter if (_mode == 2) { SharedScore.SetActive(true); if (!SinglePlayer) { SharedScore.GetComponent <PlaceController>().RandomColour = true; } } // Hide all the sprites and title bars foreach (Transform child in Dots.transform) { child.gameObject.SetActive(false); } Wait.SetActive(false); Host.SetActive(false); Tab.SetActive(false); foreach (GameObject m in _gameMode) { m.SetActive(false); } _running = true; // Start the clock _time = Time.time; // If the user is the server tell all the clients to spawn 5 dodgeballs // will also affect itself if the user is also a client if (_networkController.Server) { _networkController.RpcDodge(true); } // If it is a single player game spawn 5 dodgeballs if (SinglePlayer) { GetComponent <SingleController>().Dodge(true); } } // Countdown to 0 is displayed on the screen else if (StartCounter == 41) { Countdown[2].SetActive(false); } else if (StartCounter == 21) { Countdown[1].SetActive(false); } if (StartCounter == 1) { Started = true; Countdown[0].SetActive(false); // If youre the host spawn the points and block zones if (_networkController.Server || SinglePlayer) { foreach (GameObject z in Zones) { GameObject currentZone = GameObject.Instantiate(z); // Spawn on the server if its an online game if (_networkController.Server) { NetworkServer.Spawn(currentZone); } } } } StartCounter--; } }
// Whilst your waiting for other players this handles the sprites void WaitScreen() { // If you are the server, a client or are in a single player game and the game has not yet started if ((_networkController.Server || _networkController.Client || SinglePlayer) && StartCounter == -1) { // If you joined let open the store if (Selected == -1 && !IGS.activeSelf) { GameObject[] modes = GameObject.FindGameObjectsWithTag("Mode"); foreach (GameObject m in modes) { if (m.GetComponent <ButtonController>().On) { IGS.SetActive(true); _storeLoaded = false; } } } Debug.Log(Chosen); // Initialise the store to match the game mode if (!_storeLoaded && (Chosen || SinglePlayer)) { _storeLoaded = true; if (SinglePlayer) { IGS.GetComponent <InGameStore>().Change(0); } else if (_mode == 1) { IGS.GetComponent <InGameStore>().Change(2); } else { IGS.GetComponent <InGameStore>().Change(1); } } // If its a single player game instantiate a player object if (Players.Length == 0 && SinglePlayer) { GameObject.Instantiate(PlayerOffline); } if (SinglePlayer && Selected == -1) { IGS.SetActive(true); } // Only need to get the local player once if (Player == null) { // Set _player to the local players controller foreach (GameObject p in Players) { if (p.GetComponent <PlayerController>().Client || p.GetComponent <PlayerController>().Server || SinglePlayer) { Player = p.GetComponent <PlayerController>(); Player.ID = PlayerID; } } } // Display the countdown to start the game foreach (GameObject c in Countdown) { c.SetActive(true); } // If it is a multiplayer game display the amount of players in game if (!SinglePlayer) { foreach (Transform child in Dots.transform) { child.gameObject.SetActive(true); } } // If you are the games host display the title bar saying you can start the game if (_networkController.Server || SinglePlayer) { Tab.SetActive(true); } // Otherwise show the "waiting" title bar else { Wait.SetActive(true); Host.SetActive(true); } } }