コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        if (connected)
        {
            byte[] bsize = new byte[4];
            client.Receive(bsize);
            int    size = BitConverter.ToInt32(bsize, 0);
            byte[] data = new byte[size];
            int    k    = client.Receive(data);
            if (k != size)
            {
                print("Alarm!");
            }

            //MemoryStream ms = new MemoryStream (data);
            md.aquireBuf(data);
            byte[] bint    = new byte[4];
            byte[] bdouble = new byte[8];
            if (/*ms.ReadByte()*/ md.readByte() == 1)
            {
                //First Plane parsing

                /*ms.Read (bdouble, 0, 8);
                 * float x = (float)BitConverter.ToDouble (bdouble, 0);
                 * ms.Read (bdouble, 0, 8);
                 * float y = (float)BitConverter.ToDouble (bdouble, 0);
                 * ms.Read (bdouble, 0, 8);
                 * float angle = (float)BitConverter.ToDouble (bdouble, 0);*/
                float x     = md.readFloat();
                float y     = md.readFloat();
                float angle = md.readFloat();
                p1.transform.position    = new Vector3(x, y, 0);
                p1.transform.eulerAngles = new Vector3(0, 0, angle);
                print(x + " " + y + " " + angle);
                //First Plane Bullets parsing
                //ms.Read (bint, 0, 4);
                int bulletSize1 = md.readInt();                 //BitConverter.ToInt32 (bint, 0);
                for (int i = 0; i < bulletSize1; ++i)
                {
                    /*ms.Read (bdouble, 0, 8);
                     * x = (float)BitConverter.ToDouble (bdouble, 0);
                     * ms.Read (bdouble, 0, 8);
                     * y = (float)BitConverter.ToDouble (bdouble, 0);*/
                    //ms.Read (bdouble, 0, 8);
                    //angle = (float)BitConverter.ToDouble (bdouble, 0);
                    x = md.readFloat();
                    y = md.readFloat();
                    if (i >= bullets1.Count)
                    {
                        bullets1.Add(Instantiate(bullet));
                    }

                    bullets1[i].transform.position = new Vector3(x, y, 0);
                    bullets1[i].SetActive(true);
                    //bullets1[i].transform.eulerAngles = new Vector3 (0, 0, angle);
                }

                for (int i = bulletSize1; i < bullets1.Count; ++i)
                {
                    bullets1[i].SetActive(false);
                }

                //Second Plane parsing

                /*ms.Read (bdouble, 0, 8);
                 * x = (float)BitConverter.ToDouble (bdouble, 0);
                 * ms.Read (bdouble, 0, 8);
                 * y = (float)BitConverter.ToDouble (bdouble, 0);
                 * ms.Read (bdouble, 0, 8);
                 * angle = (float)BitConverter.ToDouble (bdouble, 0);*/
                x     = md.readFloat();
                y     = md.readFloat();
                angle = md.readFloat();
                p2.transform.position    = new Vector3(x, y, 0);
                p2.transform.eulerAngles = new Vector3(0, 0, angle);

                //Second Plane Bullets Parsing
                //ms.Read (bint, 0, 4);
                int bulletSize2 = md.readInt();                 //BitConverter.ToInt32 (bint, 0);
                for (int i = 0; i < bulletSize2; ++i)
                {
                    /*ms.Read (bdouble, 0, 8);
                     * x = (float)BitConverter.ToDouble (bdouble, 0);
                     * ms.Read (bdouble, 0, 8);
                     * y = (float)BitConverter.ToDouble (bdouble, 0);*/
                    //ms.Read (bdouble, 0, 8);
                    //angle = (float)BitConverter.ToDouble (bdouble, 0);
                    x = md.readFloat();
                    y = md.readFloat();
                    if (i >= bullets2.Count)
                    {
                        bullets2.Add(Instantiate(bullet));
                    }

                    bullets2[i].transform.position = new Vector3(x, y, 0);
                    bullets2[i].SetActive(true);
                    //bullets2[i].transform.eulerAngles = new Vector3 (0, 0, angle);
                }

                for (int i = bulletSize2; i < bullets2.Count; ++i)
                {
                    bullets2[i].SetActive(false);
                }
            }
            else if (data[0] == 0)
            {
                player      = data[1];
                playerCount = data[2];
            }

            if (player == 1)
            {
                if (Input.GetKeyDown(KeyCode.UpArrow))
                {
                    byte[] b = { 2, 0, 0 };
                    client.Send(b);
                }
                else if (Input.GetKeyUp(KeyCode.UpArrow))
                {
                    byte[] b = { 2, 0, 1 };
                    client.Send(b);
                }
                else if (Input.GetKeyDown(KeyCode.DownArrow))
                {
                    byte[] b = { 2, 0, 2 };
                    client.Send(b);
                }
                else if (Input.GetKeyUp(KeyCode.DownArrow))
                {
                    byte[] b = { 2, 0, 3 };
                    client.Send(b);
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    byte[] b = { 2, 0, 4 };
                    client.Send(b);
                }
            }

            if (player == 2)
            {
                if (Input.GetKeyDown(KeyCode.UpArrow))
                {
                    byte[] b = { 2, 0, 2 };
                    client.Send(b);
                }
                else if (Input.GetKeyUp(KeyCode.UpArrow))
                {
                    byte[] b = { 2, 0, 3 };
                    client.Send(b);
                }
                else if (Input.GetKeyDown(KeyCode.DownArrow))
                {
                    byte[] b = { 2, 0, 0 };
                    client.Send(b);
                }
                else if (Input.GetKeyUp(KeyCode.DownArrow))
                {
                    byte[] b = { 2, 0, 1 };
                    client.Send(b);
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    byte[] b = { 2, 0, 4 };
                    client.Send(b);
                }
            }
        }
    }