コード例 #1
0
    public void CmdStartGame()
    {
        if (mess.AreAllPlayersReady()) //Redundant?
        {
            List <CaptainsMessPlayer> players = GetPlayers();

            int length = players.Count;

            Assert.IsTrue(length >= 4, "There must be >=4 players!");

            int numSongsToPick = (length / 2);

            List <int> songs = new List <int>(); //List of the songID's we'll use this game.

            int numberOfSongs = AudioManagerScript.instance.GetNumSongs();
            for (int i = 0; i < numSongsToPick; i++)
            {
                int rand;
                do
                {
                    rand = Random.Range(0, numberOfSongs);
                }while (songs.Contains(rand));
                songs.Add(rand);
            }

            List <int> playerSongChoice = new List <int>(); // Final list will pull from

            // Remember, final list will have at least 2 of every choice!
            int j = 0;
            if (length % 2 != 0)
            {
                j = -1;
            }
            for (int k = 0; k < length; k++)
            {
                if (j == -1)
                {
                    playerSongChoice.Add(songs[Random.Range(0, numSongsToPick)]);
                }
                else
                {
                    playerSongChoice.Add(songs[(int)(j / 2)]);
                }
                j++;
            }

            foreach (CaptainsMessPlayer player in players)
            {
                //Recycle local J variable, don't care about last value
                j = Random.Range(0, playerSongChoice.Count);

                NetworkedPlayerScript nps = player.GetComponent <NetworkedPlayerScript>();
                //Tell the player which song they got
                nps.RpcStartGame(playerSongChoice[j]);

                //Remove that entry from list.
                playerSongChoice.RemoveAt(j);
            }
        } //Close if statement for checking if all players ready
    }