Esempio n. 1
0
        private void OnNewToolSelected(Tool newTool)
        {
            if (newTool != Tool.Select)
            {
                //Remove selected cubes when not in selection tool
                foreach (var cube in CubeSelectionController.GetAllCubes())
                {
                    ModelManager.AddCube(cube.x, cube.y, cube.z, cube.color);
                }

                CubeSelectionController.Clear();
            }
        }
Esempio n. 2
0
        private void Update()
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            var currentTool = GetCurrentTool();

            if (currentTool == Tool.Select)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        if (ModelManager.GetCube(x, y, z) && !CubeSelectionController.DoesCubeExist(x, y, z) && TransformController.GetSelectedTranformComponents() == 0)
                        {
                            int color = ModelManager.GetCubeColor(x, y, z);

                            //CubeSelectionController.ResetCubePositions();
                            CubeSelectionController.AddCube(x, y, z, color);

                            ModelManager.RemoveCube(x, y, z);

                            TransformController.UpdateVisibleTools();
                        }
                    }
                    else
                    {
                        var controllerPosition = CubeSelectionController.GetPosition();
                        foreach (var cube in CubeSelectionController.GetAllCubes())
                        {
                            ModelManager.AddCube(cube.x + (int)controllerPosition.x, cube.y + (int)controllerPosition.y, cube.z + (int)controllerPosition.z, cube.color);
                        }

                        CubeSelectionController.Clear();

                        CubeSelectionController.ResetPosition();

                        TransformController.UpdateVisibleTools();
                    }
                }
            }
            else if (currentTool == Tool.Create)
            {
                if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f)
                {
                    lastRapidTool = Time.time;

                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                }
                else if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                    else
                    {
                        var point = ray.GetPoint(5);
                        int x     = Mathf.FloorToInt(point.x);
                        int y     = Mathf.FloorToInt(point.y);
                        int z     = Mathf.FloorToInt(point.z);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                }
            }
            else if (currentTool == Tool.Destroy)
            {
                if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f)
                {
                    lastRapidTool = Time.time;

                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color = ModelManager.GetCubeColor(x, y, z);
                        ModelManager.RemoveCube(x, y, z);

                        UndoManager.AddAction(new RemoveBlockAction(x, y, z, color));
                    }
                }
                else if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color = ModelManager.GetCubeColor(x, y, z);
                        ModelManager.RemoveCube(x, y, z);

                        UndoManager.AddAction(new RemoveBlockAction(x, y, z, color));
                    }
                }
            }
            else if (currentTool == Tool.Paint)
            {
                if (Input.GetKey(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color            = ModelManager.GetCubeColor(x, y, z);
                        int colorpickerValue = ColorpickerController.GetClosestColor().GetAsIndex();

                        if (color != colorpickerValue)
                        {
                            UndoManager.AddAction(new PaintBlockAction(x, y, z, color, ColorpickerController.GetClosestColor().GetAsIndex()));

                            ModelManager.AddCube(x, y, z);
                        }
                    }
                }
            }
            else if (currentTool == Tool.Colorpicker)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color        = ModelManager.GetCubeColor(x, y, z);
                        int paletteWidth = ColorPaletteManager.GetPaletteWidth();
                        ColorpickerController.SetColor(color % paletteWidth, Mathf.FloorToInt(color / paletteWidth));
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.Mouse1))
            {
                CursorController.AddUser("mainLook");
                isLooking = true;
            }
            if (Input.GetKeyUp(KeyCode.Mouse1))
            {
                CursorController.RemoveUser("mainLook");
                isLooking = false;
            }

            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                movementSpeed = 0.35f;
            }
            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                movementSpeed = 0.175f;
            }
        }