Example #1
0
    void SpawnPlayer(PlayerSpawnData playerSpawnData)
    {
        GameObject   go     = Instantiate(PlayerPrefab);
        ClientPlayer player = go.GetComponent <ClientPlayer>();

        player.Initialize(playerSpawnData.Id, playerSpawnData.Name);
        players.Add(playerSpawnData.Id, player);
    }
    void SpawnPlayer(NetworkingData.PlayerSpawnData data)
    {
        GameObject   go     = Instantiate(PlayerPrefab);
        ClientPlayer player = go.GetComponent <ClientPlayer>();

        player.SpriteArray = SpriteArray;
        player.Prefab      = go;
        player.Initialize(data.Id, data.Name, data.SpriteRowIndex, data.Position.X, data.Position.Y, go);
        if (player.isOwn)
        {
            myPlayer = player;
        }
        players.Add(data.Id, player);
        Debug.Log($"Spawn player {data.Name} at [{data.Position.X}, {data.Position.Y}]");
    }
        public void AddPlayerToLobby(int playerId, string playerName, PlayerColor playerColor, Vector2 playerPosition, bool isPlayerHost)
        {
            bool isControlledPlayerConnecting = playerId == connectionToServer.myPlayerId;

            GameObject   chosenPlayerPrefab = isControlledPlayerConnecting ? clientControllablePlayerPrefab : playerPrefab;
            ClientPlayer clientPlayer       = Instantiate(chosenPlayerPrefab, playerPosition, Quaternion.identity).GetComponent <ClientPlayer>();

            clientPlayer.name = isControlledPlayerConnecting ? $"Player{playerId} (ClientControllable)" : $"Player{playerId}";
            clientPlayer.Initialize(playerId, playerName, playerColor, isPlayerHost);
            clientPlayer.transform.parent        = lobby.playersContainer.transform;
            clientPlayer.animator.isLookingRight = playerId < 5;

            playersManager.AddPlayer(playerId, clientPlayer);

            if (isControlledPlayerConnecting)
            {
                InitializeControlledPlayer(playersManager.players[playerId].GetComponent <ClientControllablePlayer>());
            }
        }