コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        if (!gameOver)
        {
            // Once the game has gotten down to one player left, turn off UI and set gameOver
            if (PhotonNetwork.PlayerList.Length <= 1 || World.currentWorld.playersAlive <= 1)
            {
                mainCanvas.SetActive(false);
                Panel_GameOver.SetActive(true);
                Panel_EscapeMenu.SetActive(false);
                gameOver = true;

                Cursor.visible   = true;
                Cursor.lockState = CursorLockMode.None;
            }
            else if (Input.GetKeyDown("escape"))
            {
                // Turning in game escape menu on and off

                escapemenu = !escapemenu;

                if (escapemenu)
                {
                    camController.SetInputsActive(false);
                    carController.SetInputsActive(false);

                    Panel_EscapeMenu.SetActive(true);

                    Cursor.visible   = true;
                    Cursor.lockState = CursorLockMode.None;
                }
                else
                {
                    camController.SetInputsActive(true);
                    carController.SetInputsActive(true);

                    Panel_EscapeMenu.SetActive(false);
                    Cursor.visible   = false;
                    Cursor.lockState = CursorLockMode.Confined;
                    Cursor.lockState = CursorLockMode.Locked;
                }
            }
        }
    }
コード例 #2
0
    // Events called in other scripts are processed here. One client will send an event and all players will call it
    public void OnEvent(EventData photonEvent)
    {
        byte eventCode = photonEvent.Code;

        // Event for when cars collide and bomb must transfer from one car to another
        if (eventCode == CarCollision)
        {
            object[] data = (object[])photonEvent.CustomData;

            // Get the Photon ID's of both cars involved in car collision
            short bombHolderID = (short)data[0];
            short targetID     = (short)data[1];

            // Get the Gameobjects of both cars
            GameObject bombHolder = PhotonNetwork.GetPhotonView(bombHolderID).gameObject;
            GameObject target     = PhotonNetwork.GetPhotonView(targetID).gameObject;

            // Getting the car components of the gameobjects so we can set and remove bombs
            Car bombHolderCar = bombHolder.GetComponent <Car>();
            Car targetCar     = target.GetComponent <Car>();

            // If the target car isn't dead (so the bomb doesn't get lost)
            if (bombHolderCar.hasBomb && !targetCar.dead)
            {
                bombHolderCar.hasBomb = false;
                targetCar.hasBomb     = true;                       // transfer bomb to car collided with

                bombHolderCar.RemoveBomb();
                targetCar.SetBomb();

                bombHolderCar.timeHeld = targetCar.timeHeld = 0;    // set timeHeld back to zero so when it gets bomb again it has to wait to transfer

                // Timer decrease is increased everytime the bomb transfers, e.g. (maxTime - timerDecrease)
                World.currentWorld.timerDecrease += 1;

                World.currentWorld.startTime = PhotonNetwork.Time;

                // Decrease timer's max time everytime transfer occurs
                World.currentWorld.bombTimer = World.currentWorld.maxBombTimer = Mathf.Clamp(30 - World.currentWorld.timerDecrease, 5, 30);
            }
        }
        else if (eventCode == BombHolderDies)
        {
            object[] data = (object[])photonEvent.CustomData;

            short bombHolderID = (short)data[0];
            short targetID     = (short)data[1];
            bool  isGameOver   = (bool)data[2];

            GameObject bombHolder = PhotonNetwork.GetPhotonView(bombHolderID).gameObject;
            GameObject target     = PhotonNetwork.GetPhotonView(targetID).gameObject;

            Car bombHolderCar = bombHolder.GetComponent <Car>();
            Car targetCar     = target.GetComponent <Car>();

            if (World.currentWorld.playerList.Exists(t => t.playerGameObject == bombHolder))
            {
                World.UnityPlayer bombPlayer = World.currentWorld.playerList.Find(t => t.playerGameObject == bombHolder);
                World.currentWorld.playerList.Remove(bombPlayer);
            }


            bombHolderCar.RemoveBomb();

            bombHolderCar.SetCarDead();

            playerRanks.Push(PhotonNetwork.GetPhotonView(bombHolderID).Owner);

            if (!isGameOver)
            {
                targetCar.SetBomb();                                // transfer bomb to car collided with

                bombHolderCar.timeHeld = targetCar.timeHeld = 0;    // set timeHeld back to zero so when it gets bomb again it has to wait to transfer

                // Timer decrease is increased everytime the bomb transfers, e.g. (maxTime - timerDecrease)
                World.currentWorld.timerDecrease += 1;

                World.currentWorld.startTime = PhotonNetwork.Time;

                // Decrease timer's max time everytime transfer occurs
                World.currentWorld.bombTimer = World.currentWorld.maxBombTimer = Mathf.Clamp(30 - World.currentWorld.timerDecrease, 5, 30);
            }
            else
            {
                targetCar.RemoveBomb();
            }
        }
        else if (eventCode == FirstBombHolder)
        {
            object[] data = (object[])photonEvent.CustomData;

            short targetID = (short)data[0];

            GameObject target = PhotonNetwork.GetPhotonView(targetID).gameObject;

            Car targetCar = target.GetComponent <Car>();

            targetCar.SetBomb();

            targetCar.timeHeld = 0;    // set timeHeld back to zero so when it gets bomb again it has to wait to transfer

            World.currentWorld.startTime = PhotonNetwork.Time;

            // Decrease timer's max time everytime transfer occurs
            World.currentWorld.bombTimer = World.currentWorld.maxBombTimer = Mathf.Clamp(30 - World.currentWorld.timerDecrease, 5, 30);

            // Timer decrease is increased everytime the bomb transfers, e.g. (maxTime - timerDecrease)
            World.currentWorld.timerDecrease += 1;
        }
        else if (eventCode == DisplayLeaderboard)
        {
            // Add the last living player (the winner) to the player ranks stack
            playerRanks.Push(PhotonNetwork.GetPhotonView(World.currentWorld.playerList[0].playerID).Owner);

            int stackSize = playerRanks.Count;

            // List the end game leaderboard based on when the player was pushed to the playerRank stack
            for (int i = 0; i < stackSize; i++)
            {
                leaderboardListings[i].SetPlayerInfo(playerRanks.Pop());
            }
        }
        else if (eventCode == GameStart)
        {
            // Allow player control of their car and show ability UI once everyone has connected
            if (PhotonNetwork.LocalPlayer.TagObject != null)
            {
                GameObject playerObj = (GameObject)PhotonNetwork.LocalPlayer.TagObject;

                chargeMeter.SetActive(true);
                waitingText.SetActive(false);

                Car_Control carControl = playerObj.GetComponent <Car_Control>();

                carControl.SetInputsActive(true);
            }
        }
    }