/// <summary> /// Creates a new block using the current closestColor /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> public static void AddCube(int x, int y, int z) { if (Singleton == null) { return; } int chunkX = Mathf.FloorToInt(x / 16f); int chunkY = Mathf.FloorToInt(y / 16f); int chunkZ = Mathf.FloorToInt(z / 16f); int chunkKey = Singleton.Vector3ToInt(chunkX, chunkY, chunkZ); ChunkController chunk = Singleton.GetChunkByBlock(x, y, z); if (chunk == null) { chunk = Singleton.CreateChunk(chunkX, chunkY, chunkZ); Singleton.chunks.Add(chunkKey, chunk); } var closestColor = ColorpickerController.GetClosestColor(); chunk.AddCube(Mathf.Abs(x - chunkX * 16), Mathf.Abs(y - chunkY * 16), Mathf.Abs(z - chunkZ * 16), closestColor.x, closestColor.y); }
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; } }