コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        // TODO: 尝试每5帧计算一次,提高效率

        /*
         * if (!isPending && !isEnemyTurn && Input.GetMouseButtonDown (0)) {
         *      mouse_clicked = true;
         * } else if (mouse_clicked && Input.GetMouseButtonUp (0)) {
         *      mouse_clicked = false;
         * }
         * if (frameCounter < 4) {
         *      frameCounter += 1;
         *      return;
         * }
         * frameCounter = 0;
         */

        // 等待对手消息中 ...
        if (!isOperating)
        {
            return;
        }

        arrowEnd = Input.mousePosition;
        Vector2 dir = arrowStart - arrowEnd;
        float   len = Vector2.Distance(arrowStart, arrowEnd);

        if (len > arrowMaxLen)
        {
            dir.x    = dir.x * arrowMaxLen / len;
            dir.y    = dir.y * arrowMaxLen / len;
            arrowEnd = arrowStart + dir;
        }
        arrowEnd = arrowStart + dir;

        //if (!isPending && Input.GetMouseButtonDown (0)) {
        if (Input.GetMouseButtonDown(0))
        {
            hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit)
            {
                //ts_mouse_start = System.DateTime.Now;
                ball = GameObject.Find(hit.transform.name);
                HealthScript hs = ball.GetComponent <HealthScript> ();
                if (hs.isEnemy != isEnemyTurn)
                {
                    return;
                }

                mouse_clicked = true;
                // 只在当前小球靠近屏幕边缘的时候调整camera
                savedPos = Camera.main.transform.position;
                Vector2 ballPos = ball.transform.position;
                //float newX, newY;
                if (ballPos.x < bgWidth * (-0.6f) / 200.0f || ballPos.x > bgWidth * 0.6f / 200.0f ||
                    ballPos.y < bgHeight * (-0.6f) / 200.0f || ballPos.y > bgWidth * 0.6f / 200.0f)
                {
                    //Camera.main.orthographicSize = cameraSize / 1.5f;
                    Camera.main.transform.position = new Vector3(ball.transform.position.x, ball.transform.position.y, -10);
                }

                arrowStart  = Camera.main.WorldToScreenPoint(ball.transform.position);
                startVertex = new Vector3(arrowStart.x / Screen.width, arrowStart.y / Screen.height, 0);
                arrowEnd    = arrowStart;
            }
        }

        if (mouse_clicked && Input.GetMouseButtonUp(0))
        {
            mouse_clicked = false;

            Camera.main.transform.position = savedPos;
            //Camera.main.orthographicSize = cameraSize;

            // 将用户操作发送到
            int actor  = int.Parse(ball.transform.name.ToCharArray()[4].ToString());
            int forceX = (int)(dir.x * forceFactor);
            int forceY = (int)(dir.y * forceFactor);
            //sendMove(actor, forceX, forceY);
            ball.GetComponent <HealthScript> ().Attack(new Vector2(forceX, forceY));
            isPending = true;                   // 等待actor运动结束

            // 发送操作给slave
            client.sendMove(actor, forceX, forceY);
            isWaiting   = true;
            isOperating = false;
        }
    }