示例#1
0
 private void CheckForNewPlayers()
 {
     PlayerIndex[] freePads = GamePadManager.GetFreeControllers();
     for (int i = 0; i < freePads.Length; i++)
     {
         if (!IndexInUse(freePads[i]))
         {
             GamePadState state = GamePad.GetState(freePads[i]);
             if (state.IsConnected)
             {
                 if (state.Buttons.Start == ButtonState.Pressed)
                 {
                     PlayerState newPlayerState = new PlayerState(freePads[i], state, true, 0);
                     GlobalReferences.PlayerStates.Add(newPlayerState);
                     GamePadManager.Connect((int)freePads[i]);
                     if (GlobalReferences.PlayerStates.Count > 1)
                     {
                         FollowingCamera cam = GameObject.FindObjectOfType <FollowingCamera>();
                         if (cam != null)
                         {
                             if (GlobalReferences.PlayerStates.Count >= 3)
                             {
                                 SpawnPlayer(newPlayerState, cam.transform.position + new Vector3(0, 3, 0));
                             }
                             else
                             {
                                 SpawnPlayer(newPlayerState, cam.transform.position + new Vector3(1, 3, 1));
                             }
                         }
                         else
                         {
                             SpawnPlayer(newPlayerState, Player.LastCheckpointPosition + new Vector3(0, 3, 0));
                         }
                     }
                     else
                     {
                         SpawnPlayer(newPlayerState, new Vector3(0, 3, 0));
                     }
                 }
             }
         }
     }
 }
    private void CheckPlayerJoins()
    {
        PlayerIndex[] freePads = GamePadManager.GetFreeControllers();
        for (int i = 0; i < freePads.Length; i++)
        {
            PlayerIndex  index     = freePads[i];
            GamePadState state     = GamePad.GetState(index);
            GamePadState prevState = state;
            if (state.Buttons.Start == ButtonState.Pressed && !changeMenu)
            {
                GamePadManager.Connect((int)index);
                GlobalReferences.PlayerStates.Add(new PlayerState(index, state));

                if (index == PlayerIndex.One)
                {
                    playerOneJoin.enabled       = false;
                    charSelectPlayerOne.enabled = true;
                    playerJoined[0]             = true;
                }

                if (index == PlayerIndex.Two)
                {
                    playerTwoJoin.enabled       = false;
                    charSelectPlayerTwo.enabled = true;
                    playerJoined[1]             = true;
                }

                if (index == PlayerIndex.Three)
                {
                    playerThreeJoin.enabled       = false;
                    charSelectPlayerThree.enabled = true;
                    playerJoined[2] = true;
                }

                if (index == PlayerIndex.Four)
                {
                    playerFourJoin.enabled       = false;
                    charSelectPlayerFour.enabled = true;
                    playerJoined[3] = true;
                }
            }
        }
    }
    private void CheckPlayerJoins()
    {
        PlayerIndex[] freePads = GamePadManager.GetFreeControllers();
        for (int i = 0; i < freePads.Length; i++)
        {
            PlayerIndex  index = freePads[i];
            GamePadState state = GamePad.GetState(index);
            if (state.Buttons.A == ButtonState.Pressed)
            {
                GamePadManager.Connect((int)index);
                GlobalReferences.PlayerStates.Add(new PlayerState(index, state));
                //TODO: Change menu
                if (index == PlayerIndex.One)
                {
                    Player1join.enabled     = false;
                    Player1assigned.enabled = true;
                }

                if (index == PlayerIndex.Two)
                {
                    Player2join.enabled     = false;
                    Player2assigned.enabled = true;
                }

                if (index == PlayerIndex.Three)
                {
                    Player3join.enabled     = false;
                    Player3assigned.enabled = true;
                }

                if (index == PlayerIndex.Four)
                {
                    Player4join.enabled     = false;
                    Player4assigned.enabled = true;
                }
            }
        }
    }
    // Update is called once per frame
    private void Update()
    {
        if (!hasPlayerIndex)
        {
            PlayerIndex[] freeControllers = GamePadManager.GetFreeControllers();
            for (int i = 0; i < freeControllers.Length; i++)
            {
                if (freeControllers[i] != index)
                {
                    for (int j = 0; j < GlobalReferences.PlayerStates.Count; j++)
                    {
                        if (GlobalReferences.PlayerStates[i].Index == freeControllers[i])
                        {
                            return;
                        }
                    }

                    for (int l = 0; l < GlobalReferences.PlayerStates.Count; l++)
                    {
                        if (GlobalReferences.PlayerStates[l].Index == index)
                        {
                            index = freeControllers[i];
                            GlobalReferences.PlayerStates[l] = new PlayerState(index, GlobalReferences.PlayerStates[i]);
                            hasPlayerIndex = true;
                            GamePadManager.Connect((int)index);
                            uiScript.CreateUI(index, uiScript.UICanvas.transform);
                            return;
                        }
                    }
                }
                else
                {
                    hasPlayerIndex = true;
                    GamePadManager.Connect((int)index);
                    uiScript.CreateUI(index, uiScript.UICanvas.transform);
                }
            }
        }

        if (hasPlayerIndex)
        {
            GamePadState state = GamePad.GetState(Index);
            if (state.IsConnected)
            {
                if (elapsedImmortal < maxImmortality)
                {
                    healthContainer.isImmortal = true;
                }
                else
                {
                    healthContainer.isImmortal = false;
                }

                UpdateTimers();
                if (isDead)
                {
                    RevivePlayer();
                }
                else
                {
                    if (!Freeze)
                    {
                        Input(state);
                        UpdateRotation();
                    }
                }
                prevState = state;
            }
            else
            {
                uiScript.RemoveUI(index);
                hasPlayerIndex = false;
                GamePadManager.Disconnect(Index);
                GlobalReferences.CurrentGameState = GlobalReferences.GameState.ConnectionLost;
            }
            prevState = state;
        }
        else
        {
            prevState = GamePad.GetState(Index);
        }

        stepTimer += Time.deltaTime;
    }