Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (sandboxSurface != GlobalHelper.GetMostImmediateParentVoxeme(sandboxSurface))
        {
            sandboxSurface = GlobalHelper.GetMostImmediateParentVoxeme(sandboxSurface);
        }

        if (placementState == PlacementState.Delete)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (GlobalHelper.PointOutsideMaskedAreas(
                        new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y),
                        new Rect[] {
                    new Rect(Screen.width - (15 + (int)(110 * fontSizeModifier / 3)) + 38 * fontSizeModifier - 60,
                             Screen.height - (35 + (int)(20 * fontSizeModifier)),
                             60, 20 * fontSizeModifier)
                }))
                {
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    // Casts the ray and get the first game object hit
                    Physics.Raycast(ray, out selectRayhit);
                    if (selectRayhit.collider != null)
                    {
                        if (selectRayhit.collider.gameObject.transform.root.gameObject == selectedObject)
                        {
                            DeleteVoxeme(selectedObject);
                            actionButtonText            = "Add Object";
                            placementState              = PlacementState.Add;
                            selected                    = -1;
                            cameraControl.allowRotation = true;
                        }
                    }
                }
            }
        }
        else if (placementState == PlacementState.Place)
        {
            if (Input.GetMouseButton(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                // Casts the ray and get the first game object hit
                Physics.Raycast(ray, out selectRayhit);
                if (selectRayhit.collider != null)
                {
                    if (selectRayhit.collider.gameObject.transform.root.gameObject == sandboxSurface)
                    {
                        Debug.Log(selectRayhit.point.y);
                        if (Mathf.Abs(selectRayhit.point.y - ((Vector3)preds.ComposeRelation(voxmlLibrary.VoxMLObjectDict["on"], new object[] { sandboxSurface })).y) <=
                            Constants.EPSILON)
                        {
                            if ((Mathf.Abs(selectRayhit.point.x - GlobalHelper.GetObjectWorldSize(sandboxSurface).min.x) >=
                                 GlobalHelper.GetObjectWorldSize(selectedObject).extents.x) &&
                                (Mathf.Abs(selectRayhit.point.x - GlobalHelper.GetObjectWorldSize(sandboxSurface).max.x) >=
                                 GlobalHelper.GetObjectWorldSize(selectedObject).extents.x) &&
                                (Mathf.Abs(selectRayhit.point.z - GlobalHelper.GetObjectWorldSize(sandboxSurface).min.z) >=
                                 GlobalHelper.GetObjectWorldSize(selectedObject).extents.z) &&
                                (Mathf.Abs(selectRayhit.point.z - GlobalHelper.GetObjectWorldSize(sandboxSurface).max.z) >=
                                 GlobalHelper.GetObjectWorldSize(selectedObject).extents.z))
                            {
                                selectedObject.transform.position = new Vector3(selectRayhit.point.x,
                                                                                ((Vector3)preds.ComposeRelation(voxmlLibrary.VoxMLObjectDict["on"], new object[] { sandboxSurface })).y + surfacePlacementOffset,
                                                                                selectRayhit.point.z);
                                Voxeme voxComponent = selectedObject.GetComponent <Voxeme>();
                                voxComponent.targetPosition = selectedObject.transform.position;

                                foreach (Voxeme child in voxComponent.children)
                                {
                                    if (child.isActiveAndEnabled)
                                    {
                                        if (child.gameObject != voxComponent.gameObject)
                                        {
                                            child.transform.localPosition =
                                                voxComponent.parentToChildPositionOffset[child.gameObject];
                                            child.targetPosition = child.transform.position;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.Return))
            {
                actionButtonText            = "Add Object";
                placementState              = PlacementState.Add;
                selected                    = -1;
                cameraControl.allowRotation = true;
                selectedObject.GetComponent <Rigging>().ActivatePhysics(true);
                SetShader(selectedObject, ShaderType.Default);
            }
        }
        else if (placementState == PlacementState.Add)
        {
            if (Input.GetMouseButton(0))
            {
                if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                {
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    // Casts the ray and get the first game object hit
                    Physics.Raycast(ray, out selectRayhit);
                    if (selectRayhit.collider != null)
                    {
                        if (GlobalHelper.IsSupportedBy(selectRayhit.collider.gameObject.transform.root.gameObject,
                                                       sandboxSurface))
                        {
                            if (selectRayhit.collider.gameObject.transform.root.gameObject.GetComponent <Voxeme>() !=
                                null)
                            {
                                selectedObject         = selectRayhit.collider.gameObject.transform.root.gameObject;
                                surfacePlacementOffset =
                                    (GlobalHelper.GetObjectWorldSize(selectedObject.gameObject).center.y -
                                     GlobalHelper.GetObjectWorldSize(selectedObject.gameObject).min.y) +
                                    (selectedObject.gameObject.transform.position.y -
                                     GlobalHelper.GetObjectWorldSize(selectedObject.gameObject).center.y);
                                SetShader(selectedObject, ShaderType.Highlight);
                                actionButtonText            = "Place";
                                placementState              = PlacementState.Place;
                                cameraControl.allowRotation = false;

                                if (selectedObject != null)
                                {
                                    selectedObject.GetComponent <Rigging>().ActivatePhysics(false);
                                }
                            }
                        }
                    }
                }
            }
        }
    }