From 2b7d7f2c5e87f27a97b3750a270d5f97cb654aad Mon Sep 17 00:00:00 2001 From: teatov Date: Mon, 21 Oct 2024 19:16:06 +1000 Subject: [PATCH] add camera focusing --- assets/textures/gui/pictograms.png | 4 ++-- project.godot | 7 ++++++- scenes/ui/pause_menu.tscn | 12 ++++++------ scenes/ui/unit_info.tscn | 1 + scripts/main_camera.gd | 27 +++++++++++++++++++++++++++ scripts/ui/closeable_ui.gd | 3 +++ scripts/ui/pause_menu.gd | 10 ++++++++-- 7 files changed, 53 insertions(+), 11 deletions(-) diff --git a/assets/textures/gui/pictograms.png b/assets/textures/gui/pictograms.png index d649647..22a91ea 100644 --- a/assets/textures/gui/pictograms.png +++ b/assets/textures/gui/pictograms.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:90fa4115128a314c03fb81dd4625bf9e803c364590f2d38ffd63134e83cfaf86 -size 76286 +oid sha256:54444918a6f522aeb4b8a3bbaf42fdeeeefe9d9d991632d5d316c39a83eefb0c +size 80801 diff --git a/project.godot b/project.godot index 2b5642b..d39656b 100644 --- a/project.godot +++ b/project.godot @@ -78,11 +78,16 @@ reset_camera={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194332,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } -toggle_confinement={ +focus_camera={ "deadzone": 0.5, "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194333,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +toggle_confinement={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194336,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} [layer_names] diff --git a/scenes/ui/pause_menu.tscn b/scenes/ui/pause_menu.tscn index 3a4215d..2997b87 100644 --- a/scenes/ui/pause_menu.tscn +++ b/scenes/ui/pause_menu.tscn @@ -25,7 +25,7 @@ region = Rect2(532.01, 2.29694, 48.3741, 55.326) [sub_resource type="AtlasTexture" id="AtlasTexture_wimn7"] atlas = ExtResource("4_dmywd") -region = Rect2(9.61571, 881, 845.387, 129) +region = Rect2(9.61571, 881, 1006.99, 129) [node name="PauseMenu" type="Panel"] process_mode = 3 @@ -122,7 +122,7 @@ anchor_top = 1.0 anchor_bottom = 1.0 offset_left = 15.0 offset_top = -169.0 -offset_right = 895.0 +offset_right = 1063.0 offset_bottom = -21.0 grow_vertical = 0 theme_override_styles/panel = ExtResource("1_06xpe") @@ -134,10 +134,10 @@ anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 -offset_left = -422.5 -offset_top = -64.5 -offset_right = 422.5 -offset_bottom = 64.5 +offset_left = -503.0 +offset_top = -64.0 +offset_right = 503.0 +offset_bottom = 65.0 grow_horizontal = 2 grow_vertical = 2 texture = SubResource("AtlasTexture_wimn7") diff --git a/scenes/ui/unit_info.tscn b/scenes/ui/unit_info.tscn index 2d82be1..67619d7 100644 --- a/scenes/ui/unit_info.tscn +++ b/scenes/ui/unit_info.tscn @@ -13,6 +13,7 @@ atlas = ExtResource("3_tdly6") region = Rect2(0, 0, 108, 94) [node name="UnitInfo" type="Panel"] +process_priority = 100 offset_right = 128.0 offset_bottom = 128.0 pivot_offset = Vector2(63, 130) diff --git a/scripts/main_camera.gd b/scripts/main_camera.gd index b7a180b..20b8387 100644 --- a/scripts/main_camera.gd +++ b/scripts/main_camera.gd @@ -4,6 +4,7 @@ class_name MainCamera enum State { FREE, HEADING_TO, + FOCUSED, } const ZOOM_STEP: float = 0.1 @@ -49,6 +50,8 @@ var _heading_from_position: Vector3 = Vector3.ZERO var _heading_from_zoom: float = 0 var _heading_progress: float = 0 +var _focus_target: Node3D + var _state: State = State.FREE var _window_out_of_focus: bool = false @@ -67,6 +70,7 @@ func _ready() -> void: func _process(delta: float) -> void: _handle_heading_to(delta) _handle_movement(delta) + _handle_focusing() zoom_value = lerpf(zoom_value, _zoom_unsmoothed, delta * ZOOM_DAMP) @@ -86,6 +90,8 @@ func _process(delta: float) -> void: listener.global_position = _target_position + (Vector3.UP * distance) listener.global_rotation = global_rotation + UiManager.unit_info.closed.connect(_on_unit_info_closed) + DebugManager.marker("mc_target", _target_position, 0.05) DebugManager.marker("mc_listener", listener.global_position, 0.05, Color.GREEN) @@ -106,6 +112,13 @@ func _input(event: InputEvent) -> void: if event.is_action_pressed("reset_camera"): head_to(StaticNodesManager.player_anthill.global_position) + if ( + event.is_action_pressed("focus_camera") + and UiManager.unit_info.unit != null + ): + _state = State.FOCUSED + _focus_target = UiManager.unit_info.unit + func _notification(what: int) -> void: if what == NOTIFICATION_WM_WINDOW_FOCUS_IN: @@ -230,3 +243,17 @@ func _handle_heading_to(delta: float) -> void: eased_progress, ) zoom_value = _zoom_unsmoothed + + +func _handle_focusing() -> void: + if _state != State.FOCUSED: + return + + _target_position = _focus_target.global_position + + +func _on_unit_info_closed() -> void: + if _state != State.FOCUSED: + return + + _state = State.FREE diff --git a/scripts/ui/closeable_ui.gd b/scripts/ui/closeable_ui.gd index 93f04a1..8879d6b 100644 --- a/scripts/ui/closeable_ui.gd +++ b/scripts/ui/closeable_ui.gd @@ -1,6 +1,8 @@ extends Control class_name CloseableUI +signal closed + const OPEN_TWEEN_DURATION: float = 0.5 const CLOSE_TWEEN_DURATION: float = 0.25 @@ -33,6 +35,7 @@ func _input(event: InputEvent) -> void: func close() -> void: + closed.emit() await _close_animation(self) visible = false diff --git a/scripts/ui/pause_menu.gd b/scripts/ui/pause_menu.gd index 0ef1569..c6cc35e 100644 --- a/scripts/ui/pause_menu.gd +++ b/scripts/ui/pause_menu.gd @@ -50,7 +50,10 @@ func _on_quit_button_pressed() -> void: func _open_animation() -> void: await _animate( Vector2.ZERO, - Vector2(_controls_info_pos.x, 1080), + Vector2( + _controls_info_pos.x, + get_viewport().get_visible_rect().size.y + ), Vector2.ONE, _controls_info_pos, OPEN_TWEEN_DURATION, @@ -63,7 +66,10 @@ func _close_animation() -> void: Vector2.ONE, _controls_info_pos, Vector2.ZERO, - Vector2(_controls_info_pos.x, 1080), + Vector2( + _controls_info_pos.x, + get_viewport().get_visible_rect().size.y + ), CLOSE_TWEEN_DURATION, Tween.EASE_IN, Tween.TRANS_BACK,