private void UpdatePaintTerrain() { Map map = GameManager.Instance.Map; int targetHeight = int.Parse(targetHeightInput.text); if (Input.GetMouseButtonDown(0)) { state = HeightUpdaterState.Manipulating; } if (Input.GetMouseButton(0) && state == HeightUpdaterState.Manipulating) { foreach (HeightmapHandle handle in currentFrameHoveredHandles) { map[handle.TileCoords].SurfaceHeight = targetHeight; } } if (Input.GetMouseButtonUp(0)) { map.CommandManager.FinishAction(); state = HeightUpdaterState.Idle; } if (Input.GetMouseButtonDown(1)) { map.CommandManager.UndoAction(); state = HeightUpdaterState.Idle; } }
private void UpdateLevelArea() { Map map = GameManager.Instance.Map; int targetHeight = int.Parse(targetHeightInput.text); if (Input.GetMouseButtonDown(0)) { state = HeightUpdaterState.Dragging; } if (Input.GetMouseButtonUp(0) && state == HeightUpdaterState.Dragging) { foreach (HeightmapHandle handle in currentFrameHoveredHandles) { map[handle.TileCoords].SurfaceHeight = targetHeight; } map.CommandManager.FinishAction(); state = HeightUpdaterState.Idle; LayoutManager.Instance.CurrentCamera.RenderSelectionBox = false; } if (Input.GetMouseButtonDown(1)) { map.CommandManager.UndoAction(); state = HeightUpdaterState.Idle; LayoutManager.Instance.CurrentCamera.RenderSelectionBox = false; } }
private void ResetState() { deselectedHandles.AddRange(currentFrameHoveredHandles); currentFrameHoveredHandles.Clear(); lastFrameHoveredHandles.Clear(); deselectedHandles.AddRange(selectedHandles); selectedHandles.Clear(); activeHandle = null; anchorHandle = null; if (anchorPlaneLine) { anchorPlaneLine.gameObject.SetActive(false); } state = HeightUpdaterState.Idle; GameManager.Instance.Map.CommandManager.UndoAction(); UpdateHandlesColors(); LayoutManager.Instance.CurrentCamera.RenderSelectionBox = false; }
private void UpdateCreateRamps() { Map map = GameManager.Instance.Map; float dragSensitivity = 0; float.TryParse(dragSensitivityInput.text, NumberStyles.Any, CultureInfo.InvariantCulture, out dragSensitivity); bool respectSlopes = respectOriginalSlopesToggle.isOn; if (state == HeightUpdaterState.Recovering) { state = HeightUpdaterState.Idle; } if (Input.GetMouseButtonDown(0)) { if (currentFrameHoveredHandles.Count == 1 && selectedHandles.Contains(currentFrameHoveredHandles[0])) { if (anchorHandle != null && anchorHandle != currentFrameHoveredHandles[0]) { activeHandle = currentFrameHoveredHandles[0]; } else { anchorHandle = currentFrameHoveredHandles[0]; } state = HeightUpdaterState.Manipulating; } else if (Input.GetKey(KeyCode.LeftShift)) { state = HeightUpdaterState.Dragging; } else { deselectedHandles = selectedHandles; selectedHandles = new List <HeightmapHandle>(); anchorHandle = null; anchorPlaneLine.gameObject.SetActive(false); state = HeightUpdaterState.Dragging; } } if (Input.GetMouseButton(0)) { if (state == HeightUpdaterState.Manipulating) { if (activeHandle != null && anchorHandle != null) { map.CommandManager.UndoAction(); bool locked = anchorPlaneLine.gameObject.activeSelf; PlaneAlignment lockedAxis = anchorPlaneLine.Alignment; int originalHeight = map[anchorHandle.TileCoords].SurfaceHeight; int heightDelta = (int)((dragEndPos.y - dragStartPos.y) * dragSensitivity); // instantly make smooth ramp from anchor handle to active handle if original slopes are not respected // turned off if original slopes are respected, because instantly making ramp is impractical in such case if (!respectSlopes) { heightDelta += map[activeHandle.TileCoords].SurfaceHeight - originalHeight; } Vector2Int manipulatedTileCoords = activeHandle.TileCoords; Vector2Int manipulatedAnchorCoords = GetAxisCorrectedAnchor(manipulatedTileCoords, anchorHandle.TileCoords, locked, lockedAxis); Vector2Int manipulatedDifference = manipulatedTileCoords - manipulatedAnchorCoords; foreach (HeightmapHandle heightmapHandle in selectedHandles) { Vector2Int tileCoords = heightmapHandle.TileCoords; Vector2Int anchorCoords = GetAxisCorrectedAnchor(tileCoords, anchorHandle.TileCoords, locked, lockedAxis); Vector2Int difference = tileCoords - anchorCoords; float deltaX = (float)difference.x / manipulatedDifference.x; if (float.IsNaN(deltaX) || float.IsInfinity(deltaX)) { deltaX = float.NegativeInfinity; } float deltaY = (float)difference.y / manipulatedDifference.y; if (float.IsNaN(deltaY) || float.IsInfinity(deltaY)) { deltaY = float.NegativeInfinity; } float delta = Mathf.Max(deltaX, deltaY); if (float.IsNegativeInfinity(delta)) { delta = 0; } if (respectSlopes) { map[tileCoords].SurfaceHeight += (int)(heightDelta * delta); } else { map[tileCoords].SurfaceHeight = originalHeight + (int)(heightDelta * delta); } } } else if (anchorHandle != null) { float anchorPositionX = anchorHandle.TileCoords.x * 4; float anchorPositionY = anchorHandle.TileCoords.y * 4; Vector2 anchorPosition = new Vector2(anchorPositionX, anchorPositionY); Vector3 raycastPoint = LayoutManager.Instance.CurrentCamera.CurrentRaycast.point; Vector2 raycastPosition = new Vector2(raycastPoint.x, raycastPoint.z); Vector2 positionDelta = raycastPosition - anchorPosition; if (positionDelta.magnitude > 4) { anchorPlaneLine.gameObject.SetActive(true); anchorPlaneLine.TileCoords = anchorHandle.TileCoords; bool horizontal = Mathf.Abs(positionDelta.x) > Mathf.Abs(positionDelta.y); anchorPlaneLine.Alignment = horizontal ? PlaneAlignment.Vertical : PlaneAlignment.Horizontal; } else { anchorPlaneLine.gameObject.SetActive(false); } } } } if (Input.GetMouseButtonUp(0)) { if (state == HeightUpdaterState.Dragging && Input.GetKey(KeyCode.LeftShift)) { selectedHandles.AddRange(lastFrameHoveredHandles); } else if (state == HeightUpdaterState.Dragging) { deselectedHandles = selectedHandles; selectedHandles = lastFrameHoveredHandles; } else if (state == HeightUpdaterState.Manipulating) { map.CommandManager.FinishAction(); activeHandle = null; } state = HeightUpdaterState.Idle; } if (Input.GetMouseButtonDown(1)) { if (state == HeightUpdaterState.Manipulating && activeHandle != null) { map.CommandManager.UndoAction(); activeHandle = null; state = HeightUpdaterState.Recovering; } else if (anchorHandle != null) { anchorHandle = null; anchorPlaneLine.gameObject.SetActive(false); state = HeightUpdaterState.Recovering; } else if (state == HeightUpdaterState.Idle) { deselectedHandles = selectedHandles; selectedHandles = new List <HeightmapHandle>(); } else { state = HeightUpdaterState.Recovering; } LayoutManager.Instance.CurrentCamera.RenderSelectionBox = false; } }
private void UpdateSelectAndDrag() { Map map = GameManager.Instance.Map; float dragSensitivity = 0; float.TryParse(dragSensitivityInput.text, NumberStyles.Any, CultureInfo.InvariantCulture, out dragSensitivity); bool respectSlopes = respectOriginalSlopesToggle.isOn; bool propertiesNeedSaving = false; if (Math.Abs(dragSensitivity - Properties.Instance.HeightDragSensitivity) > float.Epsilon) { Properties.Instance.HeightDragSensitivity = dragSensitivity; propertiesNeedSaving = true; } if (respectSlopes != Properties.Instance.HeightRespectOriginalSlopes) { Properties.Instance.HeightRespectOriginalSlopes = respectSlopes; propertiesNeedSaving = true; } if (propertiesNeedSaving) { Properties.Instance.SaveProperties(); } if (Input.GetMouseButtonDown(0)) { if (currentFrameHoveredHandles.Count == 1 && selectedHandles.Contains(currentFrameHoveredHandles[0])) { activeHandle = currentFrameHoveredHandles[0]; state = HeightUpdaterState.Manipulating; } else if (Input.GetKey(KeyCode.LeftShift)) { state = HeightUpdaterState.Dragging; } else { deselectedHandles = selectedHandles; selectedHandles = new List <HeightmapHandle>(); state = HeightUpdaterState.Dragging; } } if (Input.GetMouseButton(0)) { if (state == HeightUpdaterState.Manipulating) { map.CommandManager.UndoAction(); int originalHeight = map[activeHandle.TileCoords].SurfaceHeight; int heightDelta = (int)((dragEndPos.y - dragStartPos.y) * dragSensitivity); foreach (HeightmapHandle heightmapHandle in selectedHandles) { Vector2Int tileCoords = heightmapHandle.TileCoords; if (respectSlopes) { map[tileCoords].SurfaceHeight += heightDelta; } else { map[tileCoords].SurfaceHeight = originalHeight + heightDelta; } } } } if (Input.GetMouseButtonUp(0)) { if (state == HeightUpdaterState.Dragging && Input.GetKey(KeyCode.LeftShift)) { selectedHandles.AddRange(lastFrameHoveredHandles); } else if (state != HeightUpdaterState.Manipulating && state != HeightUpdaterState.Recovering) { deselectedHandles = selectedHandles; selectedHandles = lastFrameHoveredHandles; } else if (state == HeightUpdaterState.Manipulating) { map.CommandManager.FinishAction(); activeHandle = null; } state = HeightUpdaterState.Idle; } if (Input.GetMouseButtonDown(1)) { if (state == HeightUpdaterState.Idle) { deselectedHandles = selectedHandles; selectedHandles = new List <HeightmapHandle>(); } else if (state == HeightUpdaterState.Manipulating) { map.CommandManager.UndoAction(); activeHandle = null; state = HeightUpdaterState.Recovering; } else { state = HeightUpdaterState.Recovering; } LayoutManager.Instance.CurrentCamera.RenderSelectionBox = false; } }