예제 #1
0
    // [Server] enforced with inline code check
    public GameObject SpawnPlayer(NetworkConnection conn, string nick)
    {
        if (!IsServerOrDemo())
        {
            return(null);
        }

        //Debug.LogFormat("Spawning Player with connectionID {0} and nick {1}", conn.connectionId, nick);

        var playerPos = worldBuilder.GetNextPlayerPosition();

        GameObject playerGO = Instantiate(playerPrefab, new Vector3(playerPos.x, 0, playerPos.z), Quaternion.identity);

        if (worldParent != null)
        {
            playerGO.transform.parent = worldParent;
        }

        PlayerController player = playerGO.GetComponent <PlayerController>();

        player.Initialize(conn.connectionId.ToString(), nick, playerColorManager.GetNextColor());

        if (Settings.Debug_PlayerAsAI)
        {
            playerGO.AddComponent <AndreAI>();
        }

        /* NOTE: Always set properties before spawning object, if not there will be a delay before all clients get the values. */
        if (NetworkServer.active)
        {
            NetworkServer.AddPlayerForConnection(conn, playerGO, 0); // playerControllerId is used if multiple players is using one connection
        }
        else
        {
            Debug.LogWarning("NetworkServer not active, it should be when spawning player");
        }

        SpawnObject(cityPrefab, playerPos.x, playerPos.z, player, conn);

        ScenarioSetup.Run(NetworkPanel.instance.GetSelectedScenarioChoice(), conn, player);

        return(playerGO);
    }