Exemplo n.º 1
0
        public void TestJsonDeserialization()
        {
            JsonSerial        serializer = new JsonSerial();
            DataContractModel model      = serializer.DeserializeDataContract();

            Assert.AreEqual(model.Name, serializer.Model.Name);
        }
Exemplo n.º 2
0
        public void TestJsonSerialization()
        {
            JsonSerial serializer = new JsonSerial();

            serializer.SerializeDataContract();
            Assert.IsTrue(File.Exists(serializer.OutputPath));
        }
    void updateEnv(string data)
    {
        //Debug.Log("Updating: " + data);
        JsonSerial json = JsonSerial.CreateFromJSON(data);

        if (json.Sailboat.name != null)
        {
            if (!sail.ContainsKey(json.Sailboat.name))
            {
                sail[json.Sailboat.name]      = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                sail[json.Sailboat.name].name = json.Sailboat.name;
                //sail[json.Sailboat.name] = Instantiate(Resources.Load("Sailboat")) as GameObject;
            }
            sail[json.Sailboat.name].transform.position    = new Vector3((float)json.Sailboat.x, (float)0.0, (float)json.Sailboat.y);
            sail[json.Sailboat.name].transform.eulerAngles = new Vector3((float)json.Sailboat.roll, -(float)json.Sailboat.yaw, (float)json.Sailboat.pitch);
        }
        if (json.Buoy.name != null)
        {
            if (!buoy.ContainsKey(json.Buoy.name))
            {
                buoy[json.Buoy.name]      = GameObject.CreatePrimitive(PrimitiveType.Cube);
                buoy[json.Buoy.name].name = json.Buoy.name;
                //buoy[json.Buoy.name] = Instantiate(Resources.Load("Buoy")) as GameObject;
            }
            buoy[json.Buoy.name].transform.position = new Vector3((float)json.Buoy.x, (float)json.Buoy.z, (float)json.Buoy.y);
        }
    }
Exemplo n.º 4
0
    public void EnviarDirecao(Vector3 direc, Vector3 foiceP, Quaternion foiceR)
    {
        JsonSerial js = new JsonSerial();

        js.pp = foiceP;
        js.rr = foiceR;
        string dataAsJson = JsonUtility.ToJson(js);
        string m          = "Direcao|" + meuID + "|" + direc.x + "|" + direc.y + "|" + direc.z + "|" + dataAsJson;

        if (meuID == 1)
        {
            Enviar(m, unreliableChannel);
        }
        else
        {
            EnviarPlayer(m, 1, unreliableChannel);
        }
    }
Exemplo n.º 5
0
    public void EnviarPosicao(float x, float y, float z, Vector3 foiceP, Quaternion foiceR)
    {
        JsonSerial js = new JsonSerial();

        js.pp = foiceP;
        js.rr = foiceR;
        string dataAsJson = JsonUtility.ToJson(js);
        string msg        = "Posicao|" + meuID + "|" + x + "|" + y + "|" + z + "|" + dataAsJson;

        if (meuID == 1)
        {
            Enviar(msg, unreliableChannel);
        }
        else
        {
            EnviarPlayer(msg, 1, unreliableChannel);
        }
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        if (isClient && !conectou)
        {
            int              connectionId;
            int              channelId;
            byte             error;
            byte[]           recBuffer  = new byte[1024];
            int              bufferSize = 1024;
            int              dataSize;
            NetworkEventType networkEvent;
            do
            {
                networkEvent = NetworkTransport.ReceiveFromHost(hostId, out connectionId,
                                                                out channelId, recBuffer, bufferSize, out dataSize, out error);
                Debug.Log(networkEvent + " teste Broadcast");

                if (networkEvent == NetworkEventType.BroadcastEvent)
                {
                    string hostIP;
                    int    hostPort;
                    NetworkTransport.GetBroadcastConnectionInfo(hostId, out hostIP, out hostPort, out error);

                    NetworkTransport.GetBroadcastConnectionMessage(hostId, recBuffer, bufferSize,
                                                                   out dataSize, out error);
                    OnReceivedBroadcast(hostIP, GetString(recBuffer));
                }
            } while (networkEvent != NetworkEventType.Nothing);
        }

        if (conectou)
        {
            NetworkEventType recData;
            do
            {
                int    recHostId;
                int    connectionId;
                int    channelId;
                byte[] recBuffer  = new byte[1024];
                int    bufferSize = 1024;
                int    dataSize;
                byte   error;
                recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
                switch (recData)
                {
                case NetworkEventType.ConnectEvent:
                    if (meuID == 2)
                    {
                        EnviarPlayer("Conectou|" + connectionId + "|" + GlobalClass.HostArena + "|" + GlobalClass.pSkin, connectionId, reliableChannel);
                        Debug.Log("Conexao com : " + connectionId);
                        NetworkTransport.StopBroadcastDiscovery();
                        p1text.color = Color.green;
                        preparados   = true;
                    }
                    else
                    {
                        Debug.Log("me conectei com id: " + connectionId);
                        p2text.color        = Color.green;
                        preparados          = true;
                        clienteSkin.sprite  = materials[GlobalClass.pSkin].sprite;
                        clienteSkin.enabled = true;
                        Enviar("SkinCliente|" + GlobalClass.pSkin, reliableChannel);
                    }
                    goto case NetworkEventType.Nothing;

                case NetworkEventType.DataEvent:
                    string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
                    //Debug.Log(msg);
                    string[] sepEnvio = msg.Split('|');
                    switch (sepEnvio[0])
                    {
                    /*case "Preparados":
                     *  preparados = true;
                     *  //p1text.color = Color.green;
                     *  //p2text.color = Color.green;
                     *  break;*/
                    case "Conectou":
                        meuID = int.Parse(sepEnvio[1]);
                        GlobalClass.HostArena = int.Parse(sepEnvio[2]);
                        mapB.NewLayer();
                        numP.text = "Player " + meuID.ToString();
                        if (meuID == 1)
                        {
                            player1.GetComponent <Character>().nPlayer = 1;
                            player1.GetComponent <Character>().setMaterials(meuID, materials[GlobalClass.pSkin]);
                            player2.GetComponent <Character>().nPlayer = 2;
                            player2.GetComponent <Character>().setMaterials(meuID, materials[int.Parse(sepEnvio[3])]);
                            hostSkin.sprite  = materials[int.Parse(sepEnvio[3])].sprite;
                            hostSkin.enabled = true;
                            SetArena();
                            //player2.GetComponent<Character>().setMaterial(2);
                            //player1.GetComponent<Character>().setMaterial(1);
                            p1text.text  = "Eu";
                            p1text.color = Color.green;
                            p2text.text  = "Inimigo";
                        }
                        break;

                    case "SkinCliente":
                        if (meuID == 2)       //só o host recebe
                        {
                            Debug.Log("recebi_Skin_do_Cliente " + sepEnvio[1]);
                            player1.GetComponent <Character>().setMaterials(meuID, materials[int.Parse(sepEnvio[1])]);
                            clienteSkin.sprite  = materials[int.Parse(sepEnvio[1])].sprite;
                            clienteSkin.enabled = true;
                        }
                        break;

                    case "Direcao":
                        float x, y, z;
                        x = float.Parse(sepEnvio[2]);
                        y = float.Parse(sepEnvio[3]);
                        z = float.Parse(sepEnvio[4]);
                        JsonSerial dFoice = JsonUtility.FromJson <JsonSerial>(sepEnvio[5]);
                        Vector3    dir    = new Vector3(x, y, z);
                        if (meuID == 2)
                        {
                            player1.GetComponent <Character>().ChangeDirection(dir);
                            player1.GetComponent <Character>().SetFoiceT(dFoice.pp, dFoice.rr);
                        }
                        else
                        {
                            player2.GetComponent <Character>().ChangeDirection(dir);
                            player2.GetComponent <Character>().SetFoiceT(dFoice.pp, dFoice.rr);
                        }
                        break;

                    case "Pronto":
                        if (meuID == 1)
                        {
                            player2Ready   = true;
                            check2.enabled = true;
                            p2text.color   = Color.green;
                        }
                        else
                        {
                            p1text.color   = Color.green;
                            player1Ready   = true;
                            check1.enabled = true;
                            //EnviarPlayer("Preparados|", 1, reliableChannel);
                            //preparados = true;
                        }
                        break;

                    case "Comecar":
                        ComecarPartida();
                        break;

                    case "Posicao":
                        float xp, yp, zp;
                        xp = float.Parse(sepEnvio[2]);
                        yp = float.Parse(sepEnvio[3]);
                        zp = float.Parse(sepEnvio[4]);
                        JsonSerial pFoice = JsonUtility.FromJson <JsonSerial>(sepEnvio[5]);
                        if (meuID == 2)
                        {
                            //player1
                            player1.GetComponent <Character>().SetPosition(xp, yp, zp);
                            player1.GetComponent <Character>().SetFoiceT(pFoice.pp, pFoice.rr);
                        }
                        else
                        {
                            //player2
                            player2.GetComponent <Character>().SetPosition(xp, yp, zp);
                            player2.GetComponent <Character>().SetFoiceT(pFoice.pp, pFoice.rr);
                        }
                        break;

                    case "Egiro":
                        if (meuID == 2)
                        {
                            //player1 entrou girospot
                            player1.GetComponent <Character>().GirospotEnter(girospots[int.Parse(sepEnvio[2])], bool.Parse(sepEnvio[3]));
                        }
                        else
                        {
                            //player2 entrou girospot
                            player2.GetComponent <Character>().GirospotEnter(girospots[int.Parse(sepEnvio[2])], bool.Parse(sepEnvio[3]));
                        }
                        break;

                    case "Sgiro":
                        if (meuID == 2)
                        {
                            //player1 saiu girospot
                            player1.GetComponent <Character>().GirospotExit();
                        }
                        else
                        {
                            //player2 saiu girospot
                            player2.GetComponent <Character>().GirospotExit();
                        }
                        break;

                    case "InvDir":
                        Debug.Log("Inverter");
                        if (meuID == 1)
                        {
                            player1.GetComponent <Character>().InverterDirecao();
                            player1.GetComponent <Character>().PlayPonP();
                        }
                        break;

                    case "TomouDano":
                        player1.GetComponent <Character>().GiroGhostOn();
                        player1.GetComponent <Character>().instSangue(float.Parse(sepEnvio[1]), float.Parse(sepEnvio[2]), float.Parse(sepEnvio[3]));
                        player1.GetComponent <Character>().PlaySom();
                        break;

                    case "FoiceCol":
                        if (meuID == 1)
                        {
                            //Debug.Log("Recebi FoiceCol");
                            player1.GetComponent <Character>().InverterDirecao();
                            player1.GetComponent <Character>().InverterDirFaisca(float.Parse(sepEnvio[1]), float.Parse(sepEnvio[2]), float.Parse(sepEnvio[3]));
                        }
                        break;

                    case "CausouDano":
                        player2.GetComponent <Character>().GiroGhostOn();
                        player1.GetComponent <Character>().instSangue(float.Parse(sepEnvio[1]), float.Parse(sepEnvio[2]), float.Parse(sepEnvio[3]));
                        player1.GetComponent <Character>().PlaySom();
                        break;

                    case "Terminou":
                        terminarPartida(int.Parse(sepEnvio[1]));
                        player1.GetComponent <Character>().GiroFast();
                        break;

                    case "Revanche":
                        if (meuID == 1)
                        {
                            rematchB.interactable = true;
                            winner.text           = "Revanche?";
                        }
                        else if (meuID == 2)
                        {
                            //recomecar partida;
                            resetGame();
                        }
                        break;
                    }
                    goto case NetworkEventType.Nothing;

                case NetworkEventType.DisconnectEvent:
                    //Debug.Log("DisconnectEvent");

                    goto case NetworkEventType.Nothing;

                case NetworkEventType.BroadcastEvent:
                    //Debug.Log("BroadcastEvent");
                    goto case NetworkEventType.Nothing;

                case NetworkEventType.Nothing:
                    //Debug.Log("Nothing");
                    continue;
                }
            } while (recData != NetworkEventType.Nothing);
        }
        if (jogando)
        {
            if (slow)
            {
                if (meuID == 2)
                {
                    cam.GetComponent <CameraController>().setPosition(player2.transform.position);
                }
                else
                {
                    cam.GetComponent <CameraController>().setPosition(player1.transform.position);
                }
            }
            dist = Vector3.Distance(player1.transform.position, player2.transform.position);
            if (dist < 4 && !slow)
            {
                //Debug.Log("Distancia: " + dist);
                if (!player2.GetComponent <Character>().ghost&& !player1.GetComponent <Character>().ghost)
                {
                    if (!player2.GetComponent <Character>().onGirospot&& !player1.GetComponent <Character>().onGirospot)
                    {
                        if (meuID == 2)//host
                        {
                            player2.GetComponent <Character>().GiroSlow();
                            cam.GetComponent <CameraController>().setPosition(player2.transform.position);
                        }
                        else if (meuID == 1)//cliente
                        {
                            player1.GetComponent <Character>().GiroSlow();
                            cam.GetComponent <CameraController>().setPosition(player1.transform.position);
                        }
                        slow = true;
                    }
                }
            }
            else if (dist > 4 && slow)
            {
                if (meuID == 2)//host
                {
                    player2.GetComponent <Character>().GiroFast();
                    cam.GetComponent <CameraController>().resetPosition();
                }
                else//cliente
                {
                    player1.GetComponent <Character>().GiroFast();
                    cam.GetComponent <CameraController>().resetPosition();
                }
                slow = false;
            }
        }


        if (player1Ready && player2Ready && rede && !jogando)
        {
            ComecarPartida();
        }
        if (player1Ready && player2Ready && !jogando && !rede)
        {
            comecou = true;
            startPanel.SetActive(false);
            Debug.Log("todos prontos");
        }
        if (comecou && !rede)
        {
            jogando = true;
            player1.GetComponent <Character>().EntrarGirospot(girospots[0]);
            player2.GetComponent <Character>().EntrarGirospot(girospots[girospots.Length - 1]);
            comecou = false;
            Debug.Log("comecou");
        }
        if (jogando)
        {
            time      += Time.deltaTime;
            minutos    = ((int)time / 60).ToString();
            segundos   = (time % 60).ToString("f0");
            tempo.text = minutos + ":" + segundos;
        }
    }