From d0f0d8c0098260c52b520543bee378dde8f0693d Mon Sep 17 00:00:00 2001 From: Antti Hakkarainen Date: Wed, 15 Feb 2023 14:01:22 +0200 Subject: [PATCH] implement 2d camera rotation --- project.godot | 25 ++++++++++++++++++ scenes/Main.tscn | 2 ++ scripts/CameraZoom2D.gd | 56 ++++++++++++++++++++++++++++++++++++++--- scripts/Main.gd | 4 +++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/project.godot b/project.godot index 4cc1d95..0e389d0 100644 --- a/project.godot +++ b/project.godot @@ -56,6 +56,31 @@ camera_move={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +camera_rotate_left_stepless={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +camera_rotate_right_stepless={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +camera_rotate_left_fixed_step={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +camera_rotate_right_fixed_step={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +camera_reset_rotation={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194309,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} [rendering] diff --git a/scenes/Main.tscn b/scenes/Main.tscn index 9606587..c0e4cba 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -23,11 +23,13 @@ script = ExtResource("5_8jju5") [node name="CameraZoom2D" parent="." instance=ExtResource("4_rx82t")] position = Vector2(1272, 720) +ignore_rotation = false limit_left = 0 limit_top = 0 limit_right = 65536 limit_bottom = 65536 limit_smoothed = true +rotation_smoothing_enabled = true editor_draw_limits = true [node name="UILayer" type="CanvasLayer" parent="."] diff --git a/scripts/CameraZoom2D.gd b/scripts/CameraZoom2D.gd index 63a2008..36c9875 100644 --- a/scripts/CameraZoom2D.gd +++ b/scripts/CameraZoom2D.gd @@ -4,6 +4,7 @@ extends Camera2D var is_panning_camera = false var tween +var _timer:Timer var camera_bounds:Array = [Vector2(0,0), Vector2(256*16, 256*16)] var chunk_in_px:Vector2i = Vector2i(Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X, Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y) @@ -17,8 +18,21 @@ func _on_main_worldgen_ready() -> void: self.set_limit(SIDE_RIGHT, Globals.map_size*Globals.TILE_SIZE_X + chunk_in_px.x) self.set_limit(SIDE_TOP, -chunk_in_px.y) self.set_limit(SIDE_BOTTOM, Globals.map_size*Globals.TILE_SIZE_Y + chunk_in_px.y) + + # create timer for repeating camera rotation + if !_timer: + _timer = Timer.new() + _timer.set_wait_time(0.1) + _timer.set_one_shot(false) + add_child(_timer) + +var is_camera_rotating: bool = false +func rotate_camera(step:float) -> void: + self.rotation_degrees += step + + func _on_set_camera_position(pos: Vector2) -> void: self.position = pos @@ -26,6 +40,13 @@ func _on_set_camera_position(pos: Vector2) -> void: func _process(_delta) -> void: Globals.CAMERA_POSITION = self.position + while Input.is_action_pressed("camera_rotate_left_stepless"): + rotate_camera(-0.1) + await get_tree().create_timer(0.2).timeout + while Input.is_action_pressed("camera_rotate_right_stepless"): + rotate_camera(0.1) + await get_tree().create_timer(0.2).timeout + func _ready() -> void: pass @@ -42,14 +63,21 @@ func _set_camera_zoom_level(value: float) -> void: Vector2(Globals.CAMERA_ZOOM_LEVEL, Globals.CAMERA_ZOOM_LEVEL), Globals.CAMERA_ZOOM_DURATION ) - + func _unhandled_input(event): if event.is_action_pressed("camera_zoom_in"): camera_zoom_in() if event.is_action_pressed("camera_zoom_out"): camera_zoom_out() - + + if event.is_action_pressed("camera_rotate_left_fixed_step"): + rotate_camera(-45) + if event.is_action_pressed("camera_rotate_right_fixed_step"): + rotate_camera(45) + if event.is_action_pressed("camera_reset_rotation"): + reset_camera_rotation() + if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: if !is_panning_camera and event.pressed: is_panning_camera = true @@ -57,7 +85,23 @@ func _unhandled_input(event): is_panning_camera = false if event is InputEventMouseMotion and is_panning_camera: - self.position -= event.relative * Globals.CAMERA_PAN_MULTI + #var new_x = x + d * cos(theta) + #var new_y = y + d * sin(theta) + #self.position -= event.relative * Globals.CAMERA_PAN_MULTI + # √[(x₂ - x₁)² + (y₂ - y₁)²] + + var d = sqrt(pow(event.position.x - 2560/2, 2) + pow(event.position.y - 1440/2, 2)) + + var new_x = self.position.x + d * cos(self.rotation) + var new_y = self.position.y + d * sin(self.rotation) + + #var new_vect = Vector2(new_x, new_y) + + #print ("distance: " , d, " ", new_x, " ", new_y) + + self.position = Vector2(new_x, new_y) + #self.position -= event.relative * Globals.CAMERA_PAN_MULTI + # prevent camera from going overboard self.position.x = clamp( self.position.x, @@ -69,7 +113,7 @@ func _unhandled_input(event): 0, Globals.map_size*Globals.TILE_SIZE_Y ); - + func camera_zoom_in() -> void: _set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL - Globals.CAMERA_ZOOM_FACTOR) @@ -83,5 +127,9 @@ func get_camera_position(): return self.position +func reset_camera_rotation() -> void: + self.rotation_degrees = 0 + + diff --git a/scripts/Main.gd b/scripts/Main.gd index 2b09ddd..a05bd42 100644 --- a/scripts/Main.gd +++ b/scripts/Main.gd @@ -6,6 +6,10 @@ # - Then click and drag to draw the lines of grey cubes. # - etc. + +# https://github.com/dfloer/SC2k-docs + + class_name Main extends Node2D