Esempio n. 1
0
 private void MoveBox(PlayBox box, Vector3 updatedPosition, Quaternion updatedRotation)
 {
     if (box.prefab != null)
     {
         DestroyImmediate(box.prefab.GetComponent <WorldAnchor>());
         box.prefab.transform.position = updatedPosition;
         box.prefab.transform.rotation = updatedRotation;
         box.prefab.AddComponent <WorldAnchor>();
         box.prefab.transform.hasChanged = false;
     }
 }
Esempio n. 2
0
 private void GenerateServerBoxes()
 {
     for (int i = 0; i < boxQuantity; i++)
     {
         PlayBox box = new PlayBox();
         box.boxId = i;
         Vector3 position = new Vector3(Random.Range(-2.0f, 2.0f), 0.2f, Random.Range(-2.0f, 2.0f));
         box.prefab = Instantiate(boxToPlay, position, Quaternion.identity);
         box.prefab.transform.parent = WorldCenter.transform;
         listOfBoxes.Add(box);
     }
 }
Esempio n. 3
0
    private void FixedUpdate()
    {
        if (!isClientConnected && !isServerStarted)
        {
            return;
        }

        int recHostId;
        int connectionId;
        int channelId;

        byte[] recBuffer  = new byte[1024];
        int    bufferSize = 1024;
        int    dataSize;
        byte   error;


        //Recibir info del otro lado de la comunicacion
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
        case NetworkEventType.Nothing:
            break;

        case NetworkEventType.ConnectEvent:
            if (!isServerStarted)
            {
                Debug.Log("I've connected as client");
            }
            else
            {
                Debug.Log("Player" + connectionId + " connected");
                OnClientConnection(connectionId);
            }

            break;

        case NetworkEventType.DataEvent:
            string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
            //Debug.Log("Recived: " + msg);
            string[] msgArray = msg.Split('|');

            //msgArray[0] = "UPDPREFTRANS"
            //msgArray[1] = 1

            switch (msgArray[0])
            {
            case "ASKNAME":

                for (int i = 2; i <= msgArray.Length - 1; i++)
                {
                    string[] alreadyConnectedPlayer = msgArray[i].Split('%');
                    AddPlayer(int.Parse(alreadyConnectedPlayer[1]), alreadyConnectedPlayer[0]);
                }
                SendNetworkMessage("PLYRNAME|" + playerName + "|", reliableChannel, connectionId);
                lastSentTime = Time.time;
                break;

            case "PLYRNAME":

                Debug.Log("Player" + connectionId + " sent " + msg);
                SpawnClientPlayer(clientsList.Find(x => x.connectionId == connectionId), msgArray[1]);

                //Send new player name to other players
                msg = "ADDPLAYER|" + msgArray[1] + "|" + connectionId;
                ResendMessageToOtherPlayers(msg, reliableChannel, connectionId);

                break;

            case "ADDPLAYER":
                AddPlayer(int.Parse(msgArray[2]), msgArray[1]);
                break;

            case "SPAWNBOX":
                Vector3    boxPosition = new Vector3(ParseFloatUnit(msgArray[1]), ParseFloatUnit(msgArray[2]), ParseFloatUnit(msgArray[3]));
                Quaternion boxRotation = new Quaternion(ParseFloatUnit(msgArray[4]), ParseFloatUnit(msgArray[5]), ParseFloatUnit(msgArray[6]), ParseFloatUnit(msgArray[7]));
                PlayBox    box         = new PlayBox();
                box.prefab = Instantiate(boxToPlay, boxPosition, boxRotation);
                box.prefab.AddComponent <WorldAnchor>();
                box.boxId = int.Parse(msgArray[8]);
                box.prefab.transform.hasChanged = false;
                box.prefab.transform.parent     = WorldCenter.transform;
                listOfBoxes.Add(box);
                break;

            case "UPDPREFTRANS":

                Vector3    updatedPosition = new Vector3(ParseFloatUnit(msgArray[1]), ParseFloatUnit(msgArray[2]), ParseFloatUnit(msgArray[3]));
                Quaternion updatedRotation = new Quaternion(ParseFloatUnit(msgArray[4]), ParseFloatUnit(msgArray[5]), ParseFloatUnit(msgArray[6]), ParseFloatUnit(msgArray[7]));
                if (!isServerStarted)
                {
                    MoveClientPlayer(clientsList.Find(x => x.connectionId == int.Parse(msgArray[8])), updatedPosition, updatedRotation);
                }
                else
                {
                    MoveClientPlayer(clientsList.Find(x => x.connectionId == connectionId), updatedPosition, updatedRotation);
                    msg = msg + "|" + connectionId;
                    ResendMessageToOtherPlayers(msg, unreliableChannel, connectionId);
                }

                break;

            case "UPDATEBOX":

                Vector3    updatedBoxPosition = new Vector3(ParseFloatUnit(msgArray[1]), ParseFloatUnit(msgArray[2]), ParseFloatUnit(msgArray[3]));
                Quaternion updatedBoxRotation = new Quaternion(ParseFloatUnit(msgArray[4]), ParseFloatUnit(msgArray[5]), ParseFloatUnit(msgArray[6]), ParseFloatUnit(msgArray[7]));
                MoveBox(listOfBoxes.Find(x => x.boxId == int.Parse(msgArray[8])), updatedBoxPosition, updatedBoxRotation);

                if (isServerStarted)
                {
                    msg = msg + "|" + connectionId;
                    ResendMessageToOtherPlayers(msg, unreliableChannel, connectionId);
                }

                break;

            case "PLAYERDC":
                Debug.Log("Player" + msgArray[1] + " disconnected");
                var itemToRemove = clientsList.Single(x => x.connectionId == int.Parse(msgArray[1]));
                Destroy(itemToRemove.playerPrefab);
                clientsList.Remove(itemToRemove);
                break;

            case "ANCHORSENDCOMPLETE":
                debugText.text    += "ANCHORSENDCOMPLETE |";
                boxDebugText.text += anchorCollection;
                ImportWorldAnchor(anchorCollection);
                break;

            default:
                BufferWorldAnchorStream(recBuffer, dataSize);
                break;
            }



            break;

        case NetworkEventType.DisconnectEvent:
            if (isServerStarted)
            {
                Debug.Log("Player" + connectionId + " disconnected");
                var itemToRemove = clientsList.Single(x => x.connectionId == connectionId);
                Destroy(itemToRemove.playerPrefab);
                clientsList.Remove(itemToRemove);
                msg = "PLAYERDC|" + connectionId;
                ResendMessageToOtherPlayers(msg, unreliableChannel, connectionId);
            }
            break;
        }
        //Debug.Log(Time.time - lastSentTime);
        if ((Time.time - lastSentTime) > networkMessageSendRate)
        {
            if (playerPrefab.transform.hasChanged)
            {
                UpdateServerCar();
                playerPrefab.transform.hasChanged = false;
                lastSentTime = Time.time;
            }
            foreach (PlayBox box in listOfBoxes)
            {
                if (box.prefab.transform.hasChanged)
                {
                    //Debug.Log("SENDING!!!");
                    UpdateServerBox(box.boxId, box.prefab.transform.position, box.prefab.transform.rotation);
                    lastSentTime = Time.time;
                    box.prefab.transform.hasChanged = false;
                }
            }
        }
    }