Exemple #1
0
    /// <summary>
    /// Determines if this platform can slide.
    /// </summary>
    public void DetermineCanSlide()
    {
        SlidebarConnector sb = GetComponentInParent <SlidebarConnector> ();

        if (sb != null)
        {
            canSlide = sb.isConnected();
        }
    }
Exemple #2
0
    /// <summary>
    /// Raises the trigger exit 2d event. When two slidebars end connection with one another.
    /// Sets this slidebar's min and max, and if this slidebar contains a platform, sets
    /// the platforms min and max movement (which is none)
    /// </summary>
    /// <param name="collider">Collider.</param>
    void OnTriggerExit2D(Collider2D collider)
    {
        findOrientation();

        //if we are leaving contact with another slidebar
        if (collider.gameObject.CompareTag("SlideBar"))
        {
            //set to null because we're no longer connected
            connectedSlideBar = null;

            //mins and maxes are our own
            if (myOrientation == Orientation.Horizontal)
            {
                min = myCollider.bounds.min.x;
                max = myCollider.bounds.max.x;
            }
            else
            {
                min = myCollider.bounds.min.y;
                max = myCollider.bounds.max.y;
            }

            //if we contain a platform
            if (platform != null)
            {
                //get the platform's script
                PlatformDrag platformScript = platform.GetComponent <PlatformDrag> ();
                //platform can no longer slide
                platformScript.CanSlide(false);

                //set the platform's min and max movement to our own
                if (myOrientation == Orientation.Horizontal)
                {
                    platformScript.setSlideMin(myCollider.bounds.min.x - (collider.transform.localScale.x / 2.0f));
                    platformScript.setSlideMax(myCollider.bounds.min.x - (collider.transform.localScale.x / 2.0f));
                }
                else
                {
                    platformScript.setSlideMin(myCollider.bounds.min.y - (collider.transform.localScale.y / 2.0f));
                    platformScript.setSlideMax(myCollider.bounds.min.y - (collider.transform.localScale.y / 2.0f));
                }
            }
        }
    }
Exemple #3
0
    void Update()
    {
        Vector3 v3;

        //get the mouse down offset to the transform of the platform
        if (canSlide && Input.GetMouseButtonDown(0))
        {
            //cast a ray into the scene from the mouse position, the ray will only collide with a sliding platform
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (hit.collider != null)
            {
                toDrag      = hit.collider.transform;
                screenPoint = Camera.main.WorldToScreenPoint(toDrag.position);
                v3          = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
                offset      = toDrag.position - v3;
                dragging    = true;
            }
        }

        //while the mouse 0 button is held, the user can drag the platform
        if (Input.GetMouseButton(0))
        {
            if (canSlide && dragging)
            {
                v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                v3 = Camera.main.ScreenToWorldPoint(v3);
                setOrientation();

                //move in the x direction
                if (myOrientation == Orientation.Horizontal)
                {
                    if (v3.x > slideMax)
                    {
                        v3.x = slideMax - toDrag.lossyScale.x / 2.0f;
                    }
                    else if (v3.x < slideMin)
                    {
                        v3.x = slideMin - toDrag.lossyScale.x / 2.0f;
                    }
                    else
                    {
                        v3.x += offset.x;
                    }
                    toDrag.position = new Vector3(v3.x, toDrag.position.y, toDrag.position.z);
                }
                else
                {
                    //move in the y direction
                    if (v3.y > slideMax)
                    {
                        v3.y = slideMax + toDrag.lossyScale.y / 2.0f;
                    }
                    else if (v3.y < slideMin)
                    {
                        v3.y = slideMin + toDrag.lossyScale.y / 2.0f;
                    }
                    else
                    {
                        v3.y += offset.y;
                    }
                    toDrag.position = new Vector3(toDrag.position.x, v3.y, toDrag.position.z);
                }
            }
        }

        //when the user releases the mouse 0 button, the platform will snap to an end position
        if (Input.GetMouseButtonUp(0))
        {
            if (dragging)
            {
                setOrientation();
                dragging = false;
                float delta1, delta2;
                if (myOrientation == Orientation.Horizontal)
                {
                    //get the distances to the end points and snap to the closest position
                    delta1 = toDrag.position.x - slideMin;
                    delta2 = toDrag.position.x - slideMax + (toDrag.lossyScale.x / 2.0f);
                    if (delta1 * delta1 <= delta2 * delta2)
                    {
                        toDrag.position = new Vector3(slideMin - toDrag.lossyScale.x / 2.0f, toDrag.position.y, toDrag.position.z);
                    }
                    else
                    {
                        toDrag.position = new Vector3(slideMax - toDrag.lossyScale.x / 2.0f, toDrag.position.y, toDrag.position.z);
                    }

                    //transfer ownership of the platform between slidebars
                    SlidebarConnector sb = toDrag.GetComponentInParent <SlidebarConnector>();
                    if (sb.swapOwnerOfPlatform(toDrag.position.x, toDrag.position.y))
                    {
                        toDrag.SetParent(sb.connectedSlideBar.transform, true);
                        if (rotatingSquare != null)
                        {
                            this.setRotatingSquare(sb.rotatingSquare);
                        }
                    }
                }
                else
                {
                    //same process but for y, get distances to endpoints and snap to the closest position
                    delta1 = toDrag.position.y - slideMin;
                    delta2 = toDrag.position.y - slideMax + (toDrag.lossyScale.y / 2.0f);
                    if (delta1 * delta1 <= delta2 * delta2)
                    {
                        toDrag.position = new Vector3(toDrag.position.x, slideMin + toDrag.lossyScale.y / 2.0f, toDrag.position.z);
                    }
                    else
                    {
                        toDrag.position = new Vector3(toDrag.position.x, slideMax + toDrag.lossyScale.y / 2.0f, toDrag.position.z);
                    }
                    //transfer ownership of the platform between slidebars
                    SlidebarConnector sb = toDrag.GetComponentInParent <SlidebarConnector>();
                    if (sb.swapOwnerOfPlatform(toDrag.position.x, toDrag.position.y))
                    {
                        toDrag.SetParent(sb.connectedSlideBar.transform, true);
                        if (rotatingSquare != null)
                        {
                            this.setRotatingSquare(sb.rotatingSquare);
                        }
                    }
                }
            }
        }
    }
Exemple #4
0
    /// <summary>
    /// Raises the trigger enter2 d event. When two slidebars start connection with one another.
    /// Sets this slidebar's fields to reflect its connected slidebar and if this slidebar contains
    /// a platform, sets the min and max movement of the platform.
    /// </summary>
    /// <param name="collider">Collider.</param>
    void OnTriggerEnter2D(Collider2D collider)
    {
        //if this slidebar has a platform attached
        if (platform != null)
        {
            //and this collision is with another slidebar
            if (collider.transform.tag == "SlideBar")
            {
                findOrientation();

                //script of the attached platform
                PlatformDrag platformScript = platform.GetComponent <PlatformDrag> ();

                //set the connected slidebar
                connectedSlideBar = collider.GetComponent <SlidebarConnector> ();

                //if the connected slidebar has a platform already, we can't slide, so just return
                if (connectedSlideBar.platform != null)
                {
                    return;
                }

                //the end of the connect slidebar
                connectedPlatformSlot = connectedSlideBar.platformSlot;
                //tell the other slidebar that it's connected to this one
                connectedSlideBar.connectedSlideBar     = GetComponent <SlidebarConnector> ();
                connectedSlideBar.connectedPlatformSlot = platformSlot;
                platformScript.CanSlide(true);
                if (myOrientation == Orientation.Horizontal)
                {
                    //if our min is smaller, then our min is lowest the platform can slide, and his max is the max it can slide
                    if (platformSlot.transform.position.x < connectedPlatformSlot.transform.position.x)
                    {
                        min = platformSlot.transform.position.x;
                        max = connectedPlatformSlot.transform.position.x;
                    }
                    else
                    {
                        min = connectedPlatformSlot.transform.position.x;
                        max = platformSlot.transform.position.x;
                    }
                }
                else
                {
                    //same but for y direction if we're vertical
                    if (platformSlot.transform.position.y < connectedPlatformSlot.transform.position.y)
                    {
                        min = platformSlot.transform.position.y;
                        max = connectedPlatformSlot.transform.position.y;
                    }
                    else
                    {
                        min = connectedPlatformSlot.transform.position.y;
                        max = platformSlot.transform.position.y;
                    }
                }

                //set the platform's min and max slide distances
                platformScript.setSlideMax(max);
                platformScript.setSlideMin(min);
            }
        }
    }