Esempio n. 1
0
    //Runs only whilst the game is going on
    void Run()
    {
        // Get the current time
        float time = Time.time;
        // Get the time in milliseconds (Used for adding score to the player)
        int milliseconds = ((int)((time - _time) * 100));

        // Time rounded to 1 d.p
        time = ((float)((int)(10.0f * (time - _time))) / 10.0f);
        // Spawn a new ball every 5 seconds up to 100 seconds
        if (_oldTime != time && time % 5.0f == 0.0f && time <= 100.0f && time != 0.0f)
        {
            // If server tell all clients to spawn a ball
            if (_networkController.Server)
            {
                _networkController.RpcDodge(false);
            }
            // If single player spawn a ball
            else
            {
                GetComponent <SingleController>().Dodge(false);
            }
        }
        // Unless at least one player has more than 0 health game over will stay true
        bool gameOver = true;

        for (int k = 0; k < Players.Length; k++)
        {
            PlayerController pCont = Players[k].GetComponent <PlayerController>();
            pCont.Mode = _mode;
            if (pCont.Health != 0)
            {
                // Set game over galse if a player has health
                gameOver = false;
                // If the game has started
                if (milliseconds > 2)
                {
                    // If the player is still in the game keep incrementing its time
                    pCont.Time = time;
                    // increment players score for still being alive
                    if (_networkController.Server)
                    {
                        pCont.RpcScores(milliseconds - _timeDiff);
                    }
                    else if (SinglePlayer)
                    {
                        SharedScore.GetComponent <PlaceController>().Score += milliseconds - _timeDiff;
                    }
                }
            }
        }
        // Send the game data to the database
        if (Player.State == 1)
        {
            // Make sure there is a valid user connected
            if (SUGARManager.CurrentUser != null)
            {
                long score;
                // In versus send the individual users score
                if (_mode == 1)
                {
                    // Get local players score
                    score = Player.ScoreDisplay.GetComponent <PlaceController>().Score;
                    SUGARManager.GameData.Send("score", score);
                }
                // In single player send the individual users score
                else if (SinglePlayer)
                {
                    score = (long)((int)SharedScore.GetComponent <PlaceController>().Score);
                    SUGARManager.GameData.Send("score", score);
                }
                // In coop send the shared score
                else
                {
                    score = (long)((int)SharedScore.GetComponent <PlaceController>().Score);
                    SUGARManager.GameData.Send("scoreCo", score);
                    score = score / Players.Length;
                }
                // Coin cap of 9999999
                if (score + Requested["coins"] > 9999999)
                {
                    score = 9999999 - (long)Requested["coins"];
                }
                // Add the players score to their coin amount
                SUGARManager.Resource.Add("coins", score, success =>
                {
                    GetResource("coins");
                    Debug.Log(success);
                });
                // Send the players time as game data for the leaderboards and achievements
                SUGARManager.GameData.Send("time", time);
                // Send the players score as game data for the leaderboards and achievements
                if (_mode == 2)
                {
                }
                else
                {
                    SUGARManager.GameData.Send("scoreCo", score);
                }
            }
            // Change the players state so they don't send game data multiple times
            Player.State = 2;
        }
        // Once the game is over display the end game screen (with the scores)
        // until the counter ends
        if (gameOver)
        {
            if (_networkController.Server || SinglePlayer)
            {
                foreach (GameObject g in GameObject.FindGameObjectsWithTag("Zone"))
                {
                    Destroy(g);
                }
            }
            // Offset is 4 for coop and single player and 0 for vs
            GetComponent <WinnerScreen>().Offset = (_mode - 1) * 4;
            GetComponent <WinnerScreen>().Run(Players);
            if (GameOverCounter == 0)
            {
                GameOverCounter = 270;
                Lights.GetComponent <ParticleController>().Play();
            }
            else
            {
                GameOverCounter--;
            }
        }
        else
        {
            if (GameOverCounter > 0)
            {
                GameOverCounter = 0;
            }
        }
        // Once the game has finished and the end screen has been displayed for enough time return to the lobby
        if (gameOver && GameOverCounter == 0)
        {
            if (_networkController.Server)
            {
                _networkController.RpcResets();
            }
            else if (SinglePlayer)
            {
                GetComponent <SingleController>().Resets();
            }
            GameOver();
        }
        // If the game hasn't started set the start time to the current time
        if (!Started)
        {
            _time = Time.time;
        }

        // Store the old time values
        _timeDiff = milliseconds;
        _oldTime  = time;
    }
Esempio n. 2
0
    //Reset between games
    public void GameOver()
    {
        // Reset the dodgeball generators
        foreach (GameObject DBG in DBGS)
        {
            DBG.GetComponent <DBGenerator>().Counter = 0;
            DBG.GetComponent <DBGenerator>().InGame  = false;
        }
        Selected = -1;
        Chosen   = false;
        if (_gameMode.Length > 0)
        {
            _gameMode[0].GetComponent <ButtonController>().On = false;
            _gameMode[1].GetComponent <ButtonController>().On = false;
        }
        _storeLoaded = false;
        // Show lobby sprites and reset variables
        StartCounter = -1;
        _running     = false;
        Started      = false;
        Countdown[0].SetActive(true);
        Countdown[1].SetActive(true);
        Countdown[2].SetActive(true);
        if (SinglePlayer)
        {
            Tab.SetActive(true);
        }
        else
        {
            foreach (GameObject m in _gameMode)
            {
                m.SetActive(true);
            }
            foreach (Transform child in Dots.transform)
            {
                child.gameObject.SetActive(true);
            }
            if (_networkController.Server)
            {
                Tab.SetActive(true);
            }
            else
            {
                Wait.SetActive(true);
                Host.SetActive(true);
            }
        }
        // Hide the score menu
        SharedScore.SetActive(false);
        // Destroy all the dodgeballs
        GameObject[] balls = GameObject.FindGameObjectsWithTag("DodgeBall");
        foreach (GameObject b in balls)
        {
            Destroy(b);
        }
        // Set all players scores to 0
        foreach (GameObject p in Players)
        {
            p.GetComponent <PlayerController>().ScoreDisplay.GetComponent <PlaceController>().Score = 0;
        }
        // Reset the score menu
        SharedScore.GetComponent <PlaceController>().Reset();
        SharedScore.SetActive(false);

        // Clear the end game screen
        GetComponent <WinnerScreen>().Clear();
    }
Esempio n. 3
0
    //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();
    }
Esempio n. 4
0
 //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--;
     }
 }