private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.GetPosition(this).X < 10 && e.GetPosition(this).Y < 10)
            {
                (offset, WinPosition, WinSize, grab) = (GetMousePosition(), new Point(Left, Top), new Point(Width, Height), MouseGrab.LeftTop);
            }
            else
            if (e.GetPosition(this).X > Width - 10 && e.GetPosition(this).Y < 10)
            {
                (offset, WinPosition, WinSize, grab) = (GetMousePosition(), new Point(Left, Top), new Point(Width, Height), MouseGrab.RightTop);
            }
            else
            if (e.GetPosition(this).X < 10 && e.GetPosition(this).Y > Height - 10)
            {
                (offset, WinPosition, WinSize, grab) = (GetMousePosition(), new Point(Left, Top), new Point(Width, Height), MouseGrab.LeftBottom);
            }
            else
            if (e.GetPosition(this).X > Width - 10 && e.GetPosition(this).Y > Height - 10)
            {
                (offset, WinPosition, WinSize, grab) = (GetMousePosition(), new Point(Left, Top), new Point(Width, Height), MouseGrab.RightBottom);
            }
            else
            {
                (offset, WinPosition, WinSize, grab) = (GetMousePosition(), new Point(Left, Top), new Point(Width, Height), MouseGrab.Move);
            }
            new Thread(() =>
            {
                while (IsKeyDown(WinForms.Keys.LButton))
                {
                    Dispatcher.Invoke(() =>
                    {
                        switch (grab)
                        {
                        case MouseGrab.Move:
                            MoveWindow(GetForegroundWindow(), (int)(WinPosition.X + GetMousePosition().X - offset.X), (int)(WinPosition.Y + GetMousePosition().Y - offset.Y), (int)Width, (int)Height, true);
                            break;

                        case MouseGrab.LeftTop:
                        case MouseGrab.RightBottom:
                            MoveWindow(GetForegroundWindow(), (int)WinPosition.X, (int)(WinPosition.Y), (int)(WinSize.X + GetMousePosition().X - offset.X), IsKeyDown(WinForms.Keys.ShiftKey) ? (int)(WinSize.Y + GetMousePosition().Y - offset.Y) : (int)((WinSize.X + GetMousePosition().X - offset.X) / image.Width * image.Height), true);
                            break;

                        case MouseGrab.RightTop:
                            MoveWindow(GetForegroundWindow(), (int)WinPosition.X, IsKeyDown(WinForms.Keys.ShiftKey) ? (int)(WinPosition.Y + GetMousePosition().Y - offset.Y) : (int)(WinPosition.Y), (int)(WinSize.X + GetMousePosition().X - offset.X), IsKeyDown(WinForms.Keys.ShiftKey) ? (int)(WinSize.Y - GetMousePosition().Y + offset.Y) : (int)((WinSize.X + GetMousePosition().X - offset.X) / image.Width * image.Height), true);
                            break;

                        case MouseGrab.LeftBottom:
                            MoveWindow(GetForegroundWindow(), (int)(WinPosition.X + GetMousePosition().X - offset.X), (int)(WinPosition.Y), (int)(WinSize.X - GetMousePosition().X + offset.X), IsKeyDown(WinForms.Keys.ShiftKey) ? (int)(WinSize.Y + GetMousePosition().Y - offset.Y) : (int)((WinSize.X - GetMousePosition().X + offset.X) / image.Width * image.Height), true);
                            break;
                        }
                    });
                    Thread.Sleep(3);
                }
                grab = MouseGrab.None;
            }).Start();
        }
Beispiel #2
0
    private void Lose()
    {
        timerText.color = Color.red;
        timerText.text  = "YOU LOSE!!!";
        RectTransform rectXform = timerText.GetComponent <RectTransform>();

        rectXform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 450, 160);
        rectXform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 250, 30);
        MouseGrab.OnGameWon();  // just to disable mouse function

        timerText.gameObject.SetActive(false);
        LoseWindow.SetActive(true);

        // should be stopped..
        MainMusic.Stop();
        MainMusic.enabled = false;
        MainMusic.gameObject.SetActive(false);
        LoseMusic.Play();
    }
Beispiel #3
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // in this case we want to annhiliate all object, apply explosion at point contact, and then split them so that they're no longer parented anymore.
        if (collision.gameObject.GetComponent <MouseGrab>() != null)
        {
            Transform hit   = collision.gameObject.transform;
            Transform p     = hit.parent;
            Vector2   point = collision.GetContact(0).point;

            if (p?.GetComponent <MashObject>() != null)
            {
                p.GetComponent <MashObject>().HasBeenSelected = false;
                for (int i = p.childCount - 1; i >= 0; i--)
                {
                    Vector2     pos     = p.transform.position;
                    Vector2     dir     = Vector2.zero + (pos - point).normalized;
                    float       dist    = Vector2.Distance(pos, point);
                    float       intense = (impact - dist) / impact;
                    Transform   go      = p.GetChild(i).transform;
                    Rigidbody2D rb      = go.GetComponent <Rigidbody2D>();
                    MouseGrab   mg      = go.GetComponent <MouseGrab>();
                    mg.BreakObject();
                    rb.constraints = RigidbodyConstraints2D.None;
                    rb.AddForce(dir * intense * impact, ForceMode2D.Impulse);  // see why does the impulse so got damn high??
                }

                Destroy(p.gameObject);
            }
            else
            {
                Vector2 pos     = hit.transform.position;
                Vector2 dir     = Vector2.zero + (pos - point).normalized;
                float   dist    = Vector2.Distance(pos, point);
                float   intense = (impact - dist) / impact;
                hit.GetComponent <Rigidbody2D>().AddForce(point * intense * impact, ForceMode2D.Impulse);
            }
        }
    }
Beispiel #4
0
    private void Win()
    {
        timerText.color = Color.green;
        timerText.text  = "YOU WIN!!!";
        RectTransform rectXform = timerText.GetComponent <RectTransform>();

        rectXform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 450, 160);
        rectXform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 250, 30);
        gameOver = true;
        Vector3 playerPos = Player.transform.position;

        playerPos.z = distFromPlayer;
        Camera.main.transform.position = playerPos;
        Gerbil.HasWon();    // hehe
        MouseGrab.OnGameWon();
        timerText.gameObject.SetActive(false);
        MainMusic.Stop();
        MainMusic.gameObject.SetActive(false);
        MainMusic.enabled = false;

        WinWindow.SetActive(true);
        WinMusic.Play();
    }
Beispiel #5
0
 // Start is called before the first frame update
 void Start()
 {
     cam      = Camera.main;
     instance = this;
 }
Beispiel #6
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // in this case here we'll check and see if we're hitting another MouseGrabbing object.
        // if so then we'll check and see if we have MashObjectManager, if not spawn it and set parent to that object instead
        // option; if the object itself has individual rotationscript, then we need to disable that.
        //if (!isSelected && !(parent != null && parent.GetComponent<MashObject>().HasBeenSelected)) return;
        if (collision.gameObject.GetComponent <MouseGrab>() != null)
        {
            // see if the parent exist, if not create one and attach this child to the newly created parent object
            parent = transform.parent?.GetComponent <MashObject>()?.transform ?? CreateNewMesh(this.gameObject);

            //if (!isSelected && !parent.GetComponent<MashObject>().HasBeenSelected ) return;
            //parent.GetComponent<MashObject>().HasBeenSelected = true;

            if (dragObject != null)
            {
                dragObject = parent;
            }

            // get the other object's parent we hit with.
            Transform otherParent = collision.transform.parent?.GetComponent <MashObject>()?.transform;
            // freeze constraint since we already hit a object that is a mousegrab.
            this.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
            // also free the other object as well just in case.
            collision.gameObject.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;

            // if the other object have parents then we need to merge all of their existing child to this parent.
            if (otherParent != null)
            {
                //if (isSelected || parent.GetComponent<MashObject>().HasBeenSelected)
                //{
                // foreach gameobject down to 0, set it to null and then reassign to the parent.
                for (int i = otherParent.childCount - 1; i >= 0; i--)
                {
                    Transform go = otherParent.GetChild(i).transform;
                    go.parent = null;
                    go.parent = parent;
                    go.GetComponent <MouseGrab>().parent     = parent;
                    go.GetComponent <MouseGrab>().dragObject = null;
                }

                // realign the offset
                Vector3 basePos = parent != null ? parent.position : transform.position;
                offset   = GetWorldPositionOnPlane() - basePos;
                offset.z = 0;
                //}
            }
            // otherwise, it's just a floatnig object, we'll merge it anyway.
            else
            {
                // in this case if the other parent doesn't have a parent, we'll acquire the child.
                collision.transform.parent = parent;
            }

            // why is this script assigning nulls????
            //if( parent.GetComponent<MashObject>().HasBeenSelected)
            //{
            for (int i = parent.childCount - 1; i >= 0; i--)
            {
                Transform go = parent.GetChild(i).transform;
                MouseGrab mg = go.GetComponent <MouseGrab>();
                // why does this one even reset everything???
                //if (mg.isSelected )
                //{
                mg.parent     = parent;
                mg.dragObject = parent;
                //}
            }
        }
    }