public static void handleGameTransportPacket(GameTransportPacket data)
    {
        CubeiaClient cubeia = GameApplication.cubeia;

        cubeia.tableId = data.tableid;

        string jsonGameTransportPacket = System.Text.Encoding.UTF8.GetString(data.gamedata);

        Debug.Log("GameTransportPacket: " + jsonGameTransportPacket);

        var    gameData = JSONNode.Parse(jsonGameTransportPacket);
        string evt      = gameData ["evt"];

        if (evt.Equals("ctable"))
        {
            var jsonData = new JSONClass();
            jsonData ["evt"]  = "play";
            jsonData ["data"] = "nguyenhaian";

            cubeia.sendDataGame(jsonData);
        }
        else if (evt.Equals("play"))
        {
            //			String str = "";
            //			foreach (byte b in data.gamedata)
            //				str += (byte)b + ", ";
            //
            //			Debug.LogWarning(str);
            //			Debug.LogWarning("length: "+data.gamedata.Length);
            //			sendPacket(data);
        }
        else if (evt.Equals("ltable"))
        {
            // khi gui ltable len, neu tra ve LeaveResponsePacket thi back ra LobbyScene
            // truong hop khac thi tra ve ten nguoi thoat ban choi
            // => day nguoi do ra khoi ban choi
        }
    }
Beispiel #2
0
    // Update is called once per frame
    void Update()
    {
        if (GUIUtility.hotControl != 0)
        {
            return;
        }
#if !UNITY_EDITOR
        if (Input.touchCount > 0)
        {
            if (gameState == GAMESTATE.SHOOT)
            {
                return;
            }
            Vector2 touchPosition = Input.GetTouch(0).position;
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector2 delta   = Input.GetTouch(0).deltaPosition;
                Vector2 cuePos  = Camera.main.WorldToScreenPoint(transform.position);
                Vector2 prevPos = touchPosition - delta;
                float   angle   = Vector2.Angle(prevPos - cuePos, touchPosition - cuePos);
                Vector3 cross   = Vector3.Cross(touchPosition - cuePos, prevPos - cuePos);

                if (cross.z > 0)
                {
                    angle = 360 - angle;
                }
                cue.transform.Rotate(0, 0, angle);
                Vector3 v            = cueHead.transform.position - cue.transform.position;
                Vector3 t            = transform.position + v;
                Vector2 newScreenPos = Camera.main.WorldToScreenPoint(t);
                PlaceCue(newScreenPos);
            }
        }
#else
        if (Input.GetMouseButtonDown(0))
        {
            if (gameState == GAMESTATE.SHOOT)
            {
                return;
            }
            Vector2 touchPosition = Input.mousePosition;
            PlaceCue(touchPosition);
        }

        if (Input.GetMouseButtonDown(1))
        {
            Shot();
        }
#endif
        if (gameState == GAMESTATE.REPLAY)
        {
            if (updateBallsTimer <= 0)
            {
                updateBallsTimer  = DELAY;
                timeToUpdateBalls = 0;
                foreach (GameObject GO in GOS)
                {
                    Ball ball = GOSDict[GO.name];
                    if (ball.destx.Count > 0)
                    {
                        ball.destx.RemoveAt(0);
                        ball.desty.RemoveAt(0);
                        ball.orgx.RemoveAt(0);
                        ball.orgy.RemoveAt(0);
                        ball.st.RemoveAt(0);
                    }
                }
            }
            else
            {
                updateBallsTimer  -= Time.deltaTime;
                timeToUpdateBalls += Time.deltaTime;
                if (timeToUpdateBalls >= DELAY)
                {
                    timeToUpdateBalls = DELAY;
                }

                foreach (GameObject GO in GOS)
                {
                    GO.rigidbody2D.collider2D.enabled = false;
                }
                foreach (GameObject GO in GOS)
                {
                    Ball ball = GOSDict[GO.name];
                    if (ball.destx.Count > 0)
                    {
                        float[] dx     = ball.destx.ToArray();
                        float[] dy     = ball.desty.ToArray();
                        Vector2 newPos = new Vector2(dx[0], dy[0]);

                        float[] ox     = ball.orgx.ToArray();
                        float[] oy     = ball.orgy.ToArray();
                        Vector2 oldPos = new Vector2(ox[0], oy[0]);
                        Vector2 curPos = newPos - oldPos;
                        curPos *= timeToUpdateBalls / DELAY;
                        curPos += oldPos;
                        GO.rigidbody2D.MovePosition(curPos);

                        bool[] st = ball.st.ToArray();
                        GO.SetActive(st[0]);
                    }
                }
            }
            return;
        }
        else if (gameState != GAMESTATE.SHOOT)
        {
            return;
        }
        if (updateBallsTimer <= 0)
        {
            updateBallsTimer = DELAY;
            var jsonData = new JSONClass();
            jsonData ["evt"] = "balls";
            foreach (GameObject GO in GOS)
            {
                string ballname = GO.name;
                jsonData [ballname] = GO.name;
                float x  = GO.transform.position.x;
                float y  = GO.transform.position.y;
                bool  st = GO.activeInHierarchy;
                jsonData [ballname + "x"].AsFloat = x;
                jsonData [ballname + "y"].AsFloat = y;
                jsonData [ballname + "st"].AsBool = st;
            }
            cubeia.sendDataGame(jsonData);
        }
        else
        {
            updateBallsTimer -= Time.deltaTime;
//			Debug.Log(Time.deltaTime);
        }

        foreach (GameObject GO in GOS)
        {
            if (GO.activeInHierarchy && !GO.rigidbody2D.IsSleeping())
            {
                return;
            }
        }
        var jsonDataEnd = new JSONClass();
        jsonDataEnd ["evt"] = "endshoot";
        cubeia.sendDataGame(jsonDataEnd);

        gameState = GAMESTATE.AIM;
        foreach (GameObject GO in GOS)
        {
            if (GO.name == "CueBall")
            {
                continue;
            }
            if (!GO.activeInHierarchy)
            {
                continue;
            }
            Vector3 newPosition  = GO.transform.position;
            Vector2 newScreenPos = Camera.main.WorldToScreenPoint(newPosition);
            PlaceCue(newScreenPos);
                        #if AUTO_PLAY
            Invoke("Shot", 1);
                        #endif
            break;
        }
    }