diff --git a/scenes/Main.tscn b/scenes/Main.tscn index 9895eb8..bc10dd4 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -142,7 +142,7 @@ mouse_filter = 1 script = ExtResource("5_rg28x") [node name="CameraMarker" type="Sprite2D" parent="EventBus/Game/UILayer/Control/Minimap"] -z_index = 1 +z_index = 10 position = Vector2(-32, 0) centered = false script = ExtResource("7_6krn1") diff --git a/scripts/CameraMarker.gd b/scripts/CameraMarker.gd index e2296b9..b991b5a 100644 --- a/scripts/CameraMarker.gd +++ b/scripts/CameraMarker.gd @@ -24,10 +24,11 @@ func _on_camera_zoom_2d_camera_zoom_changed(new_zoom_factor): # Sets the initial size of the camera box, after game is loaded -func _on_main_worldgen_ready() -> void: +func set_camera_marker() -> void: + print("setting marker rdy") size_multiplier = Globals.map_size / 32 w_s = DisplayServer.window_get_size(0) / size_multiplier func set_camera_marker_position(pos:Vector2) -> void: - self.position = pos + print("marker pos: ", pos) diff --git a/scripts/CameraZoom2D.gd b/scripts/CameraZoom2D.gd index dc3d124..d32b864 100644 --- a/scripts/CameraZoom2D.gd +++ b/scripts/CameraZoom2D.gd @@ -15,20 +15,21 @@ var x_min_limit:int = self.game_res.x/2 - chunk_in_px.x #@onready var captured_image = $CapturedImage -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 + +func _process(_delta) -> void: + Globals.CAMERA_POSITION = self.get_camera_position() + + +func set_ready() -> void: + # set camera bounds to map size, with chunk_in_px room to go over + self.set_limit(SIDE_LEFT, -chunk_in_px.x) + 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) + func _set_camera_zoom_level(value: float) -> void: # keep zoom level in bounds, return if zoom level was at min or max zoom @@ -48,47 +49,45 @@ func _set_camera_zoom_level(value: float) -> void: ) emit_signal("camera_zoom_changed", new_zoom_level) - -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_action_pressed("take_screenshot"): - take_screenshot() - - if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: - if !is_panning_camera and event.pressed: - is_panning_camera = true - if is_panning_camera and !event.pressed: - is_panning_camera = false - if event is InputEventMouseMotion and is_panning_camera: - # rotate event.relative vector with camera rotation so camera moves to "correct" direction - self.position -= event.relative.rotated(self.rotation) * Globals.CAMERA_PAN_MULTI - - # prevent camera from going overboard - self.position.x = clamp( - self.position.x, - x_min_limit, - Globals.map_size*Globals.TILE_SIZE_X - chunk_in_px.x - ); - self.position.y = clamp( - self.position.y, - 0, - Globals.map_size*Globals.TILE_SIZE_Y +func get_camera_position(): + return self.position + + +func get_camera_panning() -> bool: + return self.is_panning_camera + + +func get_camera_rotation(): + return self.rotation + + +func set_camera_panning(value:bool) -> void: + self.is_panning_camera = value + + +func set_camera_position(pos: Vector2) -> void: + self.position = pos + + +func clamp_camera_position() -> void: + self.position.x = clamp( + self.position.x, + x_min_limit, + Globals.map_size*Globals.TILE_SIZE_X - chunk_in_px.x + ); + self.position.y = clamp( + self.position.y, + 0, + Globals.map_size*Globals.TILE_SIZE_Y ); +func camera_pan_position(value) -> void: + self.position -= value + + func camera_zoom_in() -> void: _set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL - Globals.CAMERA_ZOOM_FACTOR) @@ -96,21 +95,18 @@ func camera_zoom_in() -> void: func camera_zoom_out() -> void: _set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL + Globals.CAMERA_ZOOM_DURATION) - -func get_camera_position(): - return self.position - -func reset_camera_rotation() -> void: +func camera_reset_rotation() -> void: self.rotation_degrees = 0 emit_signal("camera_rotation_changed", self.rotation) -func rotate_camera(step:float) -> void: + +func camera_rotate(step:float) -> void: self.rotation_degrees += step emit_signal("camera_rotation_changed", self.rotation) -func take_screenshot() -> void: +func camera_take_screenshot() -> void: # Saves screenshot to user:// # Windows: %APPDATA%\Godot\app_userdata\[project_name] # macOS: ~/Library/Application Support/Godot/app_userdata/[project_name] @@ -132,15 +128,5 @@ func take_screenshot() -> void: captured_image.save_png(path) -func set_ready() -> void: - # set camera bounds to map size, with chunk_in_px room to go over - self.set_limit(SIDE_LEFT, -chunk_in_px.x) - 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) - - -func set_camera_position(pos: Vector2) -> void: - self.position = pos diff --git a/scripts/Control.gd b/scripts/Control.gd index ef6a2f1..cbdb602 100644 --- a/scripts/Control.gd +++ b/scripts/Control.gd @@ -27,6 +27,18 @@ func _on_chunk_handler_chunk_stats(chunks, removal_queue): self.size_of_chunk_removal_queue = removal_queue +# Called when the node enters the scene tree for the first time. +func _ready(): + create_buttons() + minimap = Minimap.new() + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(_delta): +# if update_debug_info: +# update_debug_info_func() + + # sends signals which View catches and places selected type of buildings func _on_button_residental_pressed(): emit_signal("button_pressed", Globals.TYPE_RESIDENTIAL) @@ -60,18 +72,6 @@ func _on_main_worldgen_ready(): self.set_process(true) update_debug_info = true - -# Called when the node enters the scene tree for the first time. -func _ready(): - create_buttons() - minimap = Minimap.new() - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - if update_debug_info: - update_debug_info_func() - # defines construction toolbar buttons func create_buttons(): diff --git a/scripts/EventBus.gd b/scripts/EventBus.gd index 0053d62..529e4ab 100644 --- a/scripts/EventBus.gd +++ b/scripts/EventBus.gd @@ -18,11 +18,24 @@ var map_filename:String = map_filenames[3] var _world_generator:WorldGenerator +func _process(_delta) -> void: + while Input.is_action_pressed("camera_rotate_left_stepless"): + node_game.camera_rotate(-0.1) + await get_tree().create_timer(0.2).timeout + while Input.is_action_pressed("camera_rotate_right_stepless"): + node_game.camera_rotate(0.1) + await get_tree().create_timer(0.2).timeout + + func _ready(): node_mainmenu.set_ready() func _unhandled_input(event) -> void: + ################################### + # MAIN MENU # + ################################### + if event.is_action_pressed("open_main_menu"): # move mainmenu to current game camera position var mainmenu_pos = Globals.CAMERA_POSITION @@ -34,6 +47,38 @@ func _unhandled_input(event) -> void: toggle_main_menu_visibility() #await get_tree().create_timer(0.2).timeout self.find_child("Game").process_mode = PROCESS_MODE_DISABLED + + ################################### + # GAME CAMERA # + ################################### + + if event.is_action_pressed("camera_zoom_in"): + node_game.camera_zoom_in() + if event.is_action_pressed("camera_zoom_out"): + node_game.camera_zoom_out() + + if event.is_action_pressed("camera_rotate_left_fixed_step"): + node_game.camera_rotate(-45) + if event.is_action_pressed("camera_rotate_right_fixed_step"): + node_game.camera_rotate(45) + if event.is_action_pressed("camera_reset_rotation"): + node_game.camera_reset_rotation() + + if event.is_action_pressed("take_screenshot"): + node_game.take_screenshot() + + if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: + if !node_game.camera_get_panning() and event.pressed: + node_game.camera_set_panning(true) + if node_game.camera_get_panning() and !event.pressed: + node_game.camera_set_panning(false) + + if event is InputEventMouseMotion and node_game.camera_get_panning(): + # rotate event.relative vector with camera rotation so camera moves to "correct" direction + node_game.camera_pan_position(event.relative.rotated(node_game.camera_get_rotation()) * Globals.CAMERA_PAN_MULTI) + + # prevent camera from going overboard + node_game.camera_clamp_position() func _on_mainmenu_button_pressed(button:int): @@ -89,7 +134,7 @@ func toggle_main_menu_visibility(): node_mainmenu.show() func set_camera_position(pos:Vector2): - node_game.set_camera_position(pos) + node_game.camera_set_position(pos) diff --git a/scripts/Game.gd b/scripts/Game.gd index f765673..8690504 100644 --- a/scripts/Game.gd +++ b/scripts/Game.gd @@ -31,11 +31,51 @@ func set_map_background_texture(sprite, scaling:Vector2) -> void: node_mapbackground.set_map_background_texture(sprite, scaling) -func set_camera_position(pos:Vector2): +func camera_clamp_position() -> void: + node_camera.clamp_camera_position() + + +func camera_pan_position(value): + node_camera.camera_pan_position(value) + + +func camera_zoom_out() -> void: + node_camera.camera_zoom_out() + + +func camera_zoom_in() -> void: + node_camera.camera_zoom_in() + + +func camera_rotate(value) -> void: + node_camera.camera_rotate(value) + + +func camera_reset_rotation() -> void: + node_camera.camera_reset_rotation() + + +func camera_get_panning() -> bool: + return node_camera.get_camera_panning() + + +func camera_get_rotation(): + return node_camera.get_camera_rotation() + + +func camera_set_panning(value:bool) -> void: + node_camera.set_camera_panning(value) + + +func camera_set_position(pos:Vector2) -> void: node_camera.set_camera_position(pos) -func toggle_visibility(): +func camera_take_screenshot() -> void: + node_camera.camera_take_screenshot() + + +func toggle_visibility() -> void: if self.visible: self.hide() else: diff --git a/scripts/Minimap.gd b/scripts/Minimap.gd index 7d7447d..e9fe817 100644 --- a/scripts/Minimap.gd +++ b/scripts/Minimap.gd @@ -23,12 +23,12 @@ func _draw(): pass -#func _process(_delta): -# if !is_mouse_inside_minimap and observe_mouse_inside_minimap: -# node_camera_marker.set_position(Vector2( -# Globals.CAMERA_POSITION.x / position_multiplier, -# Globals.CAMERA_POSITION.y / position_multiplier, -# )) +func _process(_delta): + if !is_mouse_inside_minimap and observe_mouse_inside_minimap: + node_camera_marker.set_position(Vector2( + Globals.CAMERA_POSITION.x / position_multiplier, + Globals.CAMERA_POSITION.y / position_multiplier, + )) func _on_mouse_entered(): @@ -74,6 +74,7 @@ func generate_minimap() -> void: func set_camera_marker() -> void: node_camera_marker = self.find_child("CameraMarker") + node_camera_marker.set_camera_marker() func set_minimap() -> void: