Ejemplo n.º 1
0
    private void OnSelectState()
    {
        //On left click
        if (Input.GetMouseButtonDown(0))
        {
            //Higlighting of hitobjects
            Fruit hitObject = DetectHitObject();
            if (hitObject != null)
            {
                //Select slider
                if (!ClickManager.DoubleClick() && hitObject.transform.parent.GetComponent <Slider>())
                {
                    Slider slider = hitObject.transform.parent.GetComponent <Slider>();
                    selectedHitObject = slider;

                    //When the dragging starts
                    if (draggingHitObject == null)
                    {
                        distanceFromSliderFruit = slider.transform.position - Input.mousePosition;
                    }
                    draggingHitObject = slider;
                    slider.OnHightlight();
                }
                else //Select fruit
                {
                    //Unhighlight previously selected
                    selectedHitObject?.UnHighlight();

                    selectedHitObject = hitObject;
                    draggingHitObject = hitObject;
                    hitObject.OnHightlight();
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.Delete))
        {
            Destroy(selectedHitObject.gameObject);
            selectedHitObject = null;
            draggingHitObject = null;
        }

        //Reset dragging if mouse button is released
        if (Input.GetMouseButtonUp(0))
        {
            selectedHitObject?.UnHighlight();
            draggingHitObject = null;
            selectedHitObject = null;
        }



        if (draggingHitObject == null)
        {
            return;
        }
        DraggingBehaviour();
    }
Ejemplo n.º 2
0
    public void OnPointerDown(PointerEventData eventData)
    {
        if (Input.GetMouseButtonDown(0))
        {
            var grid = ScreenToGrid(eventData.position);
            _item = inventory.GetAtPoint(grid);

            if (clickManager.DoubleClick() && _item != null & _item.Useable)
            {
                // use the item
                gameEventManager.UseItem(_item);

                // remove item
                inventory.Remove(_item);
            }
        }
    }
    void Update()
    {
        // Selected an object and deselect when not clicking on an object
        if (Input.GetMouseButtonDown(0))
        {
            // Select the smallest object
            RaycastHit[] hits;
            hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), Mathf.Infinity);

            bool       hit     = false;
            RaycastHit hitInfo = new RaycastHit();
            if (hits.Length > 1)
            {
                foreach (RaycastHit info in hits)
                {
                    Debug.Log(info.transform.name);
                    if (hitInfo.collider == null || info.collider.bounds.size.x < hitInfo.collider.bounds.size.x || info.collider.bounds.size.z < hitInfo.collider.bounds.size.z)
                    {
                        hitInfo = info;
                        hit     = true;
                    }
                }
            }
            else if (hits.Length == 1)
            {
                hit     = true;
                hitInfo = hits[0];
            }


            // Check if we hit the UI, delete button or scale slider
            PointerEventData cursor = new PointerEventData(EventSystem.current);
            cursor.position = Input.mousePosition;
            List <RaycastResult> objectsHit = new List <RaycastResult>();
            EventSystem.current.RaycastAll(cursor, objectsHit);
            hitUI     = false;
            hitDelete = false;
            hitSlider = false;

            foreach (RaycastResult result in objectsHit)
            {
                //Debug.Log("UI raycaster hit " + result.gameObject.name);
                if (result.gameObject.layer == LayerMask.NameToLayer("UI"))
                {
                    hitUI = true;
                }
                if (result.gameObject.tag == "DeleteButton")
                {
                    hitDelete = true;
                }
                if (result.gameObject.tag == "ScaleSlider")
                {
                    hitSlider = true;
                    moveMode  = false;
                }
            }

            // We only hit a model
            if (hit && !hitUI && !hitDelete && !hitSlider)
            {
                // Hit rotation object
                if (hitInfo.transform.gameObject.tag == "RotateSphere")
                {
                    Debug.Log("Hit rotation");
                    rotateObject   = true;
                    rotationSphere = hitInfo.transform.gameObject;
                }
                // Hit an object
                if ((hitInfo.transform.gameObject.tag == "Models" || hitInfo.transform.gameObject.tag == "PhotoObject") && !rotateObject)
                {
                    Debug.Log(currentObject);
                    // Select the object we hit
                    if ((currentObject == null || !currentObject.Equals(hitInfo.transform)) && clickManager.DoubleClick())
                    {
                        Debug.Log("Selected " + hitInfo.transform.name);
                        currentObject = hitInfo.transform;
                        isObjSelected = true;
                        SelectObject(currentObject.gameObject);
                        ObjectWorldPanel.Instance.SetTarget(currentObject);
                    }
                    // Needed so double clicking doesn't rotate the camera?
                    else if (!currentObject && !clickManager.DoubleClick())
                    {
                        Debug.Log("Object deselected");
                        SetSelectableToNull();
                    }
                    // Move selected object
                    else if (isObjSelected && currentObject.Equals(hitInfo.transform))
                    {
                        Debug.Log("Move Mode");
                        isObjSelected = true;
                        moveMode      = true;
                        Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
                        RaycastHit groundHit = new RaycastHit();
                        if (Physics.Raycast(ray, out groundHit, Mathf.Infinity, 1 << LayerMask.NameToLayer("GridCollider")))
                        {
                            Debug.Log("Hit " + groundHit.transform.name);
                        }
                    }
                    else
                    {
                        Debug.Log("Object deselected");
                        SetSelectableToNull();
                    }
                }
                else if (hitInfo.transform.gameObject.tag == "Ground" && !rotateObject)
                {
                    Debug.Log("Hit ground");
                    SetSelectableToNull();
                }
                else
                {
                    Debug.Log("Hit unknown object" + hitInfo.transform.gameObject.name);
                }
            }
            else if (!hitDelete && !hitSlider)
            {
                Debug.Log("Hit nothing");
                SetSelectableToNull();
            }
        }

        // With object selected
        if (currentObject)
        {
            if (hitDelete)
            {
                DeleteCurrentObject();
                SetSelectableToNull();
            }
            // Rotate Object
            else if (Input.GetMouseButton(0) && rotateObject)
            {
                //Debug.Log("In rotate");
                RaycastHit hit;
                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("GridCollider")))
                {
                    SelectorIndicator.Instance.RotateSelectedObject(hit.point, rotationSphere);
                }
            }
            // Move Object
            else if (Input.GetMouseButton(0) && moveMode)
            {
                //Debug.Log("Move Mode");
                isObjSelected = true;
                Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit groundHit = new RaycastHit();
                if (Physics.Raycast(ray, out groundHit, Mathf.Infinity, 1 << LayerMask.NameToLayer("GridCollider")))
                {
                    currentObject.position = groundHit.point;
                }
            }
        }
        // Rotate Camera
        else if (Input.GetMouseButton(0) && !hitUI && !CameraPositions.Instance.isFP)
        {
            Rotating();
        }

        // Stop rotating object on mouse up
        if (Input.GetMouseButtonUp(0))
        {
            rotateObject = false;
        }
    }