Esempio n. 1
0
    private void ClearWords()
    {
        if (!mouseDown && blockedBottom && !heldUp && !heldUpJustNow)
        {
            Collider2D   c          = GetComponent <Collider2D>();
            Collider2D[] otherArray = new Collider2D[2];

            ContactFilter2D cf = new ContactFilter2D();
            cf.SetLayerMask(LayerMask.GetMask("Row", "Col"));

            int length = c.OverlapCollider(cf, otherArray);

            WordClear[] rowCol = new WordClear[2];
            rowCol[0] = null;
            rowCol[1] = null;

            for (int i = 0; i < length; i++)
            {
                if (otherArray[i] != null)
                {
                    // there should only be 2
                    if (otherArray[i].gameObject.layer == 8)
                    {
                        rowCol[0] = otherArray[i].gameObject.GetComponent <WordClear>();
                    }
                    else if (otherArray[i].gameObject.layer == 9)
                    {
                        rowCol[1] = otherArray[i].gameObject.GetComponent <WordClear>();
                    }
                }
            }

            for (int i = 0; i < rowCol.Length; i++)
            {
                if (rowCol[i] != null)
                {
                    rowCol[i].ClearWords();
                }
            }
        }
    }
Esempio n. 2
0
    public void Update()
    {
        // input and game stuff in here
        //Vector2 deltaPosition;
        if (mouseDown)
        {
            //body.isKinematic = true;
            //body.useFullKinematicContacts = true;

            /* original 1
             * deltaPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             * //transform.Translate(deltaPosition);
             * body.velocity = deltaPosition * 10;
             */
            pointerPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else
        {
            //body.isKinematic = false;
        }

        // snap x
        Vector2 newPosition = transform.position;
        float   roundedX    = Mathf.Round(newPosition.x);

        if (newPosition.x > roundedX + 0.01f || newPosition.x < roundedX - 0.01f)
        //if (roundedX != newPosition.x)
        {
            Debug.Log(newPosition.x + " != " + roundedX);
            newPosition.x = roundedX;

            //body.velocity = Vector2.zero;
            transform.position = newPosition;
            Debug.Log("New postion: " + transform.position.x + ", " + transform.position.y);
        }

        /* original 1
         * //else if (body.velocity.y <= 0 && Mathf.Abs(body.velocity.y) >= Mathf.Abs(body.velocity.x))
         * if (Mathf.Abs(body.velocity.x) < 2.5)// && body.velocity.y <= 0)
         * {
         *  Vector3 v = body.velocity;
         *  // consider rounding in direction of throw or getting rid of throws
         *  //v.x = ((float) Mathf.Round(transform.position.x) - transform.position.x) * 10;
         *  //if (v.y > 0) { v.y = 0; }
         *
         *  //if (Mathf.Abs(v.x) >= 1)
         *  //{
         *  //    body.velocity = v;
         *  //}
         *  //else
         *  //{
         *      // snap x
         *      v.x = 0;
         *      body.velocity = v;
         *      deltaPosition = new Vector3((float) Mathf.Round(transform.position.x), transform.position.y, transform.position.z);
         *      transform.position = deltaPosition;
         *  //}
         * }
         */

        // clear words when tile stops moving
        if (wasMoving && body.velocity == Vector2.zero)
        {
            // snap y
            newPosition = transform.position;
            float roundedY = Mathf.Round(newPosition.y);
            if (newPosition.y > roundedY + 0.01f || newPosition.y < roundedY - 0.01f)
            //if (roundedY != newPosition.y)
            {
                Debug.Log(newPosition.y + " != " + roundedY);
                newPosition.y = roundedY;

                //body.velocity = Vector2.zero;
                transform.position = newPosition;
                Debug.Log("New postion: " + transform.position.x + ", " + transform.position.y);
            }

            // tile was moving and has now stopped
            wasMoving = false;
            //Debug.Log("Stopped moving");
            //clear words
            Collider2D   c          = GetComponent <Collider2D>();
            Collider2D[] otherArray = new Collider2D[2];

            ContactFilter2D cf = new ContactFilter2D();
            cf.SetLayerMask(LayerMask.GetMask("Row", "Col"));

            int length = c.OverlapCollider(cf, otherArray);

            //Debug.Log("otherArray length: " + length);

            WordClear[] rowCol = new WordClear[2];
            rowCol[0] = null;
            rowCol[1] = null;

            for (int i = 0; i < length; i++)
            {
                if (otherArray[i] != null)
                {
                    // there should only be 2
                    if (otherArray[i].gameObject.layer == 8)
                    {
                        rowCol[0] = otherArray[i].gameObject.GetComponent <WordClear>();
                        //Debug.Log("IS ROW!");
                    }
                    else if (otherArray[i].gameObject.layer == 9)
                    {
                        rowCol[1] = otherArray[i].gameObject.GetComponent <WordClear>();
                        //Debug.Log("IS COL!");
                    }
                }
            }

            for (int i = 0; i < rowCol.Length; i++)
            {
                if (rowCol[i] != null)
                {
                    rowCol[i].ClearWords();
                }
            }
        }
        else if (body.velocity != Vector2.zero)
        {
            // tile is moving again
            wasMoving = true;
        }
    }