void OnConnect(NetworkConnection c) //done
    {
        Debug.Log("Accepted a connection");
        // Send a handshake message to Set ID
        HandshakeMsg m = new HandshakeMsg();

        m.player.id = c.InternalId.ToString();
        Assert.IsTrue(c.IsCreated);
        SendToClient(JsonUtility.ToJson(m), c);

        // Send List of Players to new Client
        SpawnedPlayersList playersInServer = new SpawnedPlayersList();

        foreach (var client in clientLookUpTable)
        {
            playersInServer.players.Add(client.Value);
        }
        Assert.IsTrue(c.IsCreated);
        SendToClient(JsonUtility.ToJson(playersInServer), c);

        // Send new Client to All existing Players
        NewPlayerMsg newPlayer = new NewPlayerMsg();

        newPlayer.player.id = c.InternalId.ToString();
        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);
            SendToClient(JsonUtility.ToJson(newPlayer), m_Connections[i]);
        }

        m_Connections.Add(c);
        clientLookUpTable[c.InternalId.ToString()]           = new NetworkObjects.NetworkPlayer();
        clientLookUpTable[c.InternalId.ToString()].heartBeat = Time.time;
    }
Exemplo n.º 2
0
 // Spawns all players from the sever
 void SpawnPlayers(SpawnedPlayersList spawnMsg) //done
 {
     for (int i = 0; i < spawnMsg.players.Count; i++)
     {
         GameObject player = Instantiate(playerPrefab);
         playerLookUpTable[spawnMsg.players[i].id] = player;
         player.transform.position = spawnMsg.players[i].cubPos;
         player.GetComponent <PlayerController>().clientControlled = false;
         player.GetComponent <PlayerController>().networkClient    = this;
     }
 }
Exemplo n.º 3
0
    void OnData(DataStreamReader stream)//done
    {
        NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(bytes);
        string        recMsg = Encoding.ASCII.GetString(bytes.ToArray());
        NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg);

        switch (header.cmd)
        {
        case Commands.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            Debug.Log("Handshake message received Set clients ID");
            SetupClient(hsMsg);
            break;

        case Commands.PLAYER_UPDATE:
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            Debug.Log("Player update message received!");
            break;

        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            UpdateAllPlayers(suMsg);
            Debug.Log("Server update message received!");
            break;

        case Commands.SPAWNED_PLAYERS:
            SpawnedPlayersList spawnedPlayers = JsonUtility.FromJson <SpawnedPlayersList>(recMsg);
            SpawnPlayers(spawnedPlayers);
            Debug.Log("Spawned all Players from Server");
            break;

        case Commands.NEW_PLAYER:
            NewPlayerMsg newPlayer = JsonUtility.FromJson <NewPlayerMsg>(recMsg);
            SpawnNewPlayer(newPlayer);
            Debug.Log("Spawned new Player from Server");
            break;

        case Commands.DROPPED_PLAYER:
            DroppedPlayersList droppedPlayers = JsonUtility.FromJson <DroppedPlayersList>(recMsg);
            DestroyDroppedPlayers(droppedPlayers);
            Debug.Log("Dropped dead players from the Server");
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }