コード例 #1
0
        // called in server to start the game spawning the initial units
        private void SpawnUnits()
        {
            List <SpawnPoint> spawnPoints = JuloFind.allWithComponent <SpawnPoint>();

            foreach (SpawnPoint sp in spawnPoints)
            {
                int role = sp.playerId;
                if (playerMap.ContainsKey(role))
                {
                    PlayerWrapper  wrapper = playerMap[role];
                    DualGamePlayer player  = wrapper.player;

                    if (role >= 0 && role < numPlayers)
                    {
                        SpawnUnit(player, sp.unitId, sp.transform);
                    }
                    else
                    {
                        Debug.LogWarningFormat("Invalid role");
                    }
                }
                else
                {
                    Debug.LogWarningFormat("Ignoring unit for player {0}", role);
                }
            }
        }
コード例 #2
0
        public bool ThereIsEnoughPlayers()
        {
            if (state != DNMState.Host)
            {
                Debug.LogError("Invalid state");
                return(false);
            }
            if (gameState != GameState.NoGame)
            {
                Debug.LogError("Invalid state");
                return(false);
            }

            int numPlayers = 0;

            for (int i = 0; i < currentMaxPlayers; i++)
            {
                if (playerMap.ContainsKey(i))
                {
                    PlayerWrapper wrapper = playerMap[i];
                    if (wrapper != null && wrapper.player != null /* && wrapper.player.role == i*/)
                    {
                        numPlayers++;
                    }
                    else
                    {
                        Debug.LogErrorFormat("Invalid player: {0}.({1})({2})({3})", i, wrapper != null, wrapper.player != null, wrapper.player.role);
                    }
                }
            }

            bool ret = numPlayers >= currentMinPlayers;

            return(ret);
        }
コード例 #3
0
        public bool TryToStartGame()
        {
            if (state != DNMState.Host)
            {
                Debug.LogError("Invalid state");
                return(false);
            }
            if (gameState != GameState.NoGame)
            {
                Debug.LogError("Invalid state");
                return(false);
            }
            if (currentSize < 1 || currentSize < currentScene.minSize || currentSize > currentScene.maxSize)
            {
                Debug.LogError("Invalid size");
                return(false);
            }

            //currentPlayers = new List<DualGamePlayer>();
            gamePlayers = new Dictionary <int, PlayerWrapper>();

            for (int i = 0; i < currentMaxPlayers; i++)
            {
                if (playerMap.ContainsKey(i))
                {
                    PlayerWrapper wrapper = playerMap[i];
                    if (wrapper != null && wrapper.player != null && wrapper.player.role == i)
                    {
                        gamePlayers[i] = wrapper;
                    }
                    else
                    {
                        Debug.LogErrorFormat("Invalid player: {0}.({1})({2})({3})", i, wrapper != null, wrapper.player != null, wrapper.player.role);
                    }
                }
            }

            int numPlayers = gamePlayers.Count;

            if (numPlayers >= currentMinPlayers)
            {
                // start game
                //Debug.LogFormat("Starting game: {0} (size={1}, players={2})", currentSceneName, currentSize, numPlayers);

                gameState = GameState.LoadingGame;
                SetServerInfo("Loading", "");

                // this sets all clients as not-ready
                ServerChangeScene(currentScene.assetName);

                return(true);
            }
            else
            {
                Debug.Log("Not enough players");
                return(false);
            }
        }
コード例 #4
0
        private void SwitchRoles(int role1, int role2)
        {
            PlayerWrapper player1 = playerMap[role1];
            PlayerWrapper player2 = playerMap[role2];

            playerMap.Remove(role1);
            playerMap.Remove(role2);

            playerMap.Add(role2, player1);
            playerMap.Add(role1, player2);

            player1.player.role = role2;
            player2.player.role = role1;
        }
コード例 #5
0
        // only server
        public DualGamePlayer GetPlayer(int role)
        {
            if (playerMap.ContainsKey(role))
            {
                PlayerWrapper wrapper = playerMap[role];

                if (wrapper != null && wrapper.player != null)
                {
                    return(wrapper.player);
                }
                else
                {
                    Debug.LogError("Invalid player");
                    return(null);
                }
            }
            Debug.LogWarning("Missing player");
            return(null);
        }
コード例 #6
0
        /**** Server internal ****/

        private void ChangeRole(int oldRole, int newRole)
        {
            if (playerMap.ContainsKey(newRole))
            {
                Debug.LogError("Occupied");
            }
            else if (!playerMap.ContainsKey(oldRole))
            {
                Debug.LogError("Inexistent");
            }
            else
            {
                PlayerWrapper playerWrap = playerMap[oldRole];
                playerMap.Remove(oldRole);

                playerMap.Add(newRole, playerWrap);

                playerWrap.player.role = newRole;
            }
        }
コード例 #7
0
        // called on server when a client disconnected
        public override void OnServerDisconnect(NetworkConnection connectionToClient)
        {
            bool playerFound;

            do
            {
                playerFound = false;
                int keyToRemove = -1;
                foreach (KeyValuePair <int, PlayerWrapper> pair in playerMap)
                {
                    int           role          = pair.Key;
                    PlayerWrapper playerWrapper = pair.Value;

                    if (playerWrapper == null || playerWrapper.player == null)
                    {
                        Debug.LogErrorFormat("Invalid player");
                    }
                    else if (role != playerWrapper.player.role)
                    {
                        Debug.LogErrorFormat("Wrong role: {0} != {1}", role, playerWrapper.player.role);
                    }
                    else if (playerWrapper.connectionId == connectionToClient.connectionId)
                    {
                        playerFound = true;
                        keyToRemove = role;
                        break;
                    }
                }

                if (playerFound)
                {
                    playerMap.Remove(keyToRemove);
                    // auto up
                }
            } while(playerFound);

            NetworkServer.DestroyPlayersForConnection(connectionToClient);
        }
コード例 #8
0
        // called on server when a client requests to add a player for it
        // TODO should receive a message with initial name/color
        public override void OnServerAddPlayer(NetworkConnection connectionToClient, short playerControllerId, NetworkReader extraMessage)
        {
            string playerName = "Guest";

            if (extraMessage != null)
            {
                var s = extraMessage.ReadMessage <NewPlayerMessage>();
                playerName = s.playerName;
            }
            else
            {
                Debug.LogWarning("Mo message");
            }

            if (state != DNMState.Host)
            {
                Debug.LogErrorFormat("Invalid state: {0}", state);
                return;
            }

            DualGamePlayer newPlayer = null;

            bool isLocal = (connectionToClient.connectionId == 0);

            if (isLocal)
            { // if the player is local it should be cached
                if (gameState != GameState.NoGame)
                {
                    Debug.LogErrorFormat("Invalid state: {0}", gameState);
                }
                else if (playerControllerId < cachedLocalPlayers.Count)
                {
                    newPlayer = cachedLocalPlayers[playerControllerId];
                }
                else
                {
                    JuloDebug.Err("Player " + playerControllerId + " should be cached");
                    //return;
                }
            }
            else
            { // if is remote should be created
                if (connectionToClient.playerControllers.Count > 0)
                {
                    Debug.LogWarning("Already a player for that connection");
                    return;
                }

                bool isPlaying = gameState != GameState.NoGame;

                newPlayer            = CreatePlayer(connectionToClient.connectionId, playerControllerId);
                newPlayer.playerName = playerName;

                int newRole = -1;

                if (isPlaying || joinAsSpectator)
                {
                    // join as spectator
                    newRole = maximumSpectatorRole + 1;
                }
                else
                {
                    // try to join as player
                    bool assigned = false;

                    // try to enter as player
                    for (int i = 0; !assigned && i < currentMaxPlayers; i++)
                    {
                        if (!playerMap.ContainsKey(i))
                        {
                            newRole  = i;
                            assigned = true;
                        }
                    }

                    if (!assigned)
                    {
                        newRole = maximumSpectatorRole + 1;
                    }
                }

                maximumSpectatorRole = Math.Max(maximumSpectatorRole, newRole);

                newPlayer.role = newRole;
            }

            if (newPlayer != null)
            {
                PlayerWrapper wrapper = new PlayerWrapper(connectionToClient.connectionId, playerControllerId, newPlayer);

                //Debug.LogFormat("ADDING {0}: {1}-{2}", newRole, connectionToClient.connectionId, playerControllerId);
                playerMap.Add(newPlayer.role, wrapper);

                newPlayer.gameObject.SetActive(true);
                NetworkServer.AddPlayerForConnection(connectionToClient, newPlayer.gameObject, playerControllerId);
            }
            else
            {
                JuloDebug.Warn("Player could not be added: " + connectionToClient.connectionId + ":" + playerControllerId);
            }
        }