diff --git a/scenes/Main.tscn b/scenes/Main.tscn index e547542..9895eb8 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=10 format=3 uid="uid://b1bahdquscsym"] +[gd_scene load_steps=12 format=3 uid="uid://b1bahdquscsym"] [ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"] +[ext_resource type="Script" path="res://scripts/EventBus.gd" id="2_0j1ud"] [ext_resource type="Script" path="res://scripts/ChunkHandler.gd" id="2_6cequ"] -[ext_resource type="PackedScene" uid="uid://bflqpejouge8r" path="res://scenes/MainMenu.tscn" id="2_wfpe2"] +[ext_resource type="PackedScene" uid="uid://dq7jmiqon170p" path="res://scenes/MainMenu.tscn" id="2_wfpe2"] [ext_resource type="Script" path="res://scripts/Control.gd" id="3_1t1c8"] +[ext_resource type="Script" path="res://scripts/Game.gd" id="4_4tr6y"] [ext_resource type="PackedScene" uid="uid://2we3txfr812u" path="res://scenes/Camera_zoom_2d.tscn" id="4_rx82t"] [ext_resource type="Script" path="res://scripts/EntityPlacer.gd" id="5_8jju5"] [ext_resource type="Script" path="res://scripts/Minimap.gd" id="5_rg28x"] @@ -13,16 +15,21 @@ [node name="Main" type="Node2D"] script = ExtResource("1_ysxum") -[node name="MainMenu" parent="." instance=ExtResource("2_wfpe2")] +[node name="EventBus" type="Node" parent="."] +script = ExtResource("2_0j1ud") -[node name="Game" type="Node2D" parent="."] +[node name="MainMenu" parent="EventBus" instance=ExtResource("2_wfpe2")] +process_mode = 2 + +[node name="Game" type="Node2D" parent="EventBus"] process_mode = 1 visible = false +script = ExtResource("4_4tr6y") -[node name="ChunkHandler" type="Node2D" parent="Game"] +[node name="ChunkHandler" type="Node2D" parent="EventBus/Game"] script = ExtResource("2_6cequ") -[node name="CameraZoom2D" parent="Game" instance=ExtResource("4_rx82t")] +[node name="CameraZoom2D" parent="EventBus/Game" instance=ExtResource("4_rx82t")] position = Vector2(1272, 720) ignore_rotation = false limit_left = 0 @@ -33,23 +40,22 @@ limit_smoothed = true rotation_smoothing_enabled = true editor_draw_limits = true -[node name="MapBackground" type="Sprite2D" parent="Game"] +[node name="MapBackground" type="Sprite2D" parent="EventBus/Game"] z_index = -1 centered = false script = ExtResource("8_ron2j") -[node name="UILayer" type="CanvasLayer" parent="Game"] -process_mode = 1 +[node name="UILayer" type="CanvasLayer" parent="EventBus/Game"] visible = false -[node name="EntityPlacer" type="Control" parent="Game/UILayer"] +[node name="EntityPlacer" type="Control" parent="EventBus/Game/UILayer"] layout_mode = 3 anchors_preset = 0 offset_right = 40.0 offset_bottom = 40.0 script = ExtResource("5_8jju5") -[node name="Control" type="Control" parent="Game/UILayer"] +[node name="Control" type="Control" parent="EventBus/Game/UILayer"] layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 @@ -60,7 +66,7 @@ mouse_filter = 1 script = ExtResource("3_1t1c8") metadata/_edit_use_anchors_ = true -[node name="ConstructionPanel" type="Panel" parent="Game/UILayer/Control"] +[node name="ConstructionPanel" type="Panel" parent="EventBus/Game/UILayer/Control"] custom_minimum_size = Vector2(500, 100) layout_mode = 1 anchors_preset = 5 @@ -71,42 +77,42 @@ offset_right = 250.0 offset_bottom = 100.0 grow_horizontal = 2 -[node name="button_residental" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_residental" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_commercial" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_commercial" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_industrial" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_industrial" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_roads" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_roads" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_demolish" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_demolish" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_services" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_services" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="button_social" type="Button" parent="Game/UILayer/Control/ConstructionPanel"] +[node name="button_social" type="Button" parent="EventBus/Game/UILayer/Control/ConstructionPanel"] layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="DebugContainer" type="GridContainer" parent="Game/UILayer/Control"] +[node name="DebugContainer" type="GridContainer" parent="EventBus/Game/UILayer/Control"] layout_mode = 1 anchors_preset = 1 anchor_left = 1.0 @@ -116,10 +122,10 @@ offset_top = 24.0 offset_bottom = 424.0 grow_horizontal = 0 -[node name="DebugInfo" type="Label" parent="Game/UILayer/Control/DebugContainer"] +[node name="DebugInfo" type="Label" parent="EventBus/Game/UILayer/Control/DebugContainer"] layout_mode = 2 -[node name="Minimap" type="Panel" parent="Game/UILayer/Control"] +[node name="Minimap" type="Panel" parent="EventBus/Game/UILayer/Control"] clip_contents = true custom_minimum_size = Vector2(512, 512) layout_mode = 1 @@ -135,33 +141,27 @@ grow_vertical = 0 mouse_filter = 1 script = ExtResource("5_rg28x") -[node name="CameraMarker" type="Sprite2D" parent="Game/UILayer/Control/Minimap"] +[node name="CameraMarker" type="Sprite2D" parent="EventBus/Game/UILayer/Control/Minimap"] z_index = 1 position = Vector2(-32, 0) centered = false script = ExtResource("7_6krn1") -[node name="MinimapSprite" type="Sprite2D" parent="Game/UILayer/Control/Minimap"] +[node name="MinimapSprite" type="Sprite2D" parent="EventBus/Game/UILayer/Control/Minimap"] texture_repeat = 1 centered = false -[connection signal="set_camera_position" from="." to="Game/CameraZoom2D" method="_on_set_camera_position"] -[connection signal="worldgen_ready" from="." to="Game/ChunkHandler" method="_on_main_worldgen_ready"] -[connection signal="worldgen_ready" from="." to="Game/CameraZoom2D" method="_on_main_worldgen_ready"] -[connection signal="worldgen_ready" from="." to="Game/UILayer/Control" method="_on_main_worldgen_ready"] -[connection signal="worldgen_ready" from="." to="Game/UILayer/Control/Minimap" method="_on_main_worldgen_ready"] -[connection signal="worldgen_ready" from="." to="Game/UILayer/Control/Minimap/CameraMarker" method="_on_main_worldgen_ready"] -[connection signal="chunk_stats" from="Game/ChunkHandler" to="Game/UILayer/Control" method="_on_chunk_handler_chunk_stats"] -[connection signal="camera_rotation_changed" from="Game/CameraZoom2D" to="Game/UILayer/Control/Minimap/CameraMarker" method="_on_camera_zoom_2d_camera_rotation_changed"] -[connection signal="camera_zoom_changed" from="Game/CameraZoom2D" to="Game/UILayer/Control/Minimap/CameraMarker" method="_on_camera_zoom_2d_camera_zoom_changed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_residental" to="Game/UILayer/Control" method="_on_button_residental_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_commercial" to="Game/UILayer/Control" method="_on_button_commercial_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_industrial" to="Game/UILayer/Control" method="_on_button_industrial_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_roads" to="Game/UILayer/Control" method="_on_button_roads_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_demolish" to="Game/UILayer/Control" method="_on_button_demolish_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_services" to="Game/UILayer/Control" method="_on_button_services_pressed"] -[connection signal="pressed" from="Game/UILayer/Control/ConstructionPanel/button_social" to="Game/UILayer/Control" method="_on_button_social_pressed"] -[connection signal="mouse_entered" from="Game/UILayer/Control/Minimap" to="Game/UILayer/Control/Minimap" method="_on_mouse_entered"] -[connection signal="mouse_exited" from="Game/UILayer/Control/Minimap" to="Game/UILayer/Control/Minimap" method="_on_mouse_exited"] -[connection signal="set_camera_position" from="Game/UILayer/Control/Minimap" to="Game/CameraZoom2D" method="_on_set_camera_position"] -[connection signal="set_map_background_texture" from="Game/UILayer/Control/Minimap" to="Game/MapBackground" method="_on_minimap_set_map_background_texture"] +[connection signal="chunk_stats" from="EventBus/Game/ChunkHandler" to="EventBus/Game/UILayer/Control" method="_on_chunk_handler_chunk_stats"] +[connection signal="camera_rotation_changed" from="EventBus/Game/CameraZoom2D" to="EventBus/Game/UILayer/Control/Minimap/CameraMarker" method="_on_camera_zoom_2d_camera_rotation_changed"] +[connection signal="camera_zoom_changed" from="EventBus/Game/CameraZoom2D" to="EventBus/Game/UILayer/Control/Minimap/CameraMarker" method="_on_camera_zoom_2d_camera_zoom_changed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_residental" to="EventBus/Game/UILayer/Control" method="_on_button_residental_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_commercial" to="EventBus/Game/UILayer/Control" method="_on_button_commercial_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_industrial" to="EventBus/Game/UILayer/Control" method="_on_button_industrial_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_roads" to="EventBus/Game/UILayer/Control" method="_on_button_roads_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_demolish" to="EventBus/Game/UILayer/Control" method="_on_button_demolish_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_services" to="EventBus/Game/UILayer/Control" method="_on_button_services_pressed"] +[connection signal="pressed" from="EventBus/Game/UILayer/Control/ConstructionPanel/button_social" to="EventBus/Game/UILayer/Control" method="_on_button_social_pressed"] +[connection signal="mouse_entered" from="EventBus/Game/UILayer/Control/Minimap" to="EventBus/Game/UILayer/Control/Minimap" method="_on_mouse_entered"] +[connection signal="mouse_exited" from="EventBus/Game/UILayer/Control/Minimap" to="EventBus/Game/UILayer/Control/Minimap" method="_on_mouse_exited"] +[connection signal="set_camera_position" from="EventBus/Game/UILayer/Control/Minimap" to="EventBus" method="set_camera_position"] +[connection signal="set_map_background_texture" from="EventBus/Game/UILayer/Control/Minimap" to="EventBus/Game" method="_on_minimap_set_map_background_texture"] diff --git a/scenes/MainMenu.tscn b/scenes/MainMenu.tscn index 5c33815..9811e80 100644 --- a/scenes/MainMenu.tscn +++ b/scenes/MainMenu.tscn @@ -1,7 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://bflqpejouge8r"] +[gd_scene load_steps=5 format=3 uid="uid://dq7jmiqon170p"] -[ext_resource type="Script" path="res://scripts/MainMenuBar.gd" id="1_50qd7"] [ext_resource type="Theme" uid="uid://clswkwdkqsx87" path="res://themes/mainmenu_button_theme.tres" id="1_jy0t0"] +[ext_resource type="Script" path="res://scripts/MainMenu.gd" id="1_rpwyu"] [ext_resource type="Texture2D" uid="uid://ccbum282wlrrj" path="res://art/backgrounds/mainmenu_background.png" id="2_7rmv5"] [ext_resource type="Texture2D" uid="uid://jsnvlmenolg8" path="res://art/logos/mainmenu_logo.png" id="3_etjlt"] @@ -12,6 +12,7 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +script = ExtResource("1_rpwyu") [node name="MainMenuBar" type="GridContainer" parent="."] layout_mode = 0 @@ -22,7 +23,6 @@ offset_bottom = 1155.0 size_flags_horizontal = 4 size_flags_vertical = 4 theme_override_constants/v_separation = 32 -script = ExtResource("1_50qd7") metadata/_edit_use_anchors_ = true [node name="Menu_NewGame" type="Button" parent="MainMenuBar"] @@ -74,9 +74,9 @@ rotation = -1.5708 scale = Vector2(0.547831, 0.547831) texture = ExtResource("3_etjlt") -[connection signal="pressed" from="MainMenuBar/Menu_NewGame" to="MainMenuBar" method="_on_menu_new_game_pressed"] -[connection signal="pressed" from="MainMenuBar/Menu_LoadGame" to="MainMenuBar" method="_on_menu_load_game_pressed"] -[connection signal="pressed" from="MainMenuBar/Menu_ResumeGame" to="MainMenuBar" method="_on_menu_resume_game_pressed"] -[connection signal="pressed" from="MainMenuBar/Menu_Options" to="MainMenuBar" method="_on_menu_options_pressed"] -[connection signal="pressed" from="MainMenuBar/Menu_Credits" to="MainMenuBar" method="_on_menu_credits_pressed"] -[connection signal="pressed" from="MainMenuBar/Menu_ExitGame" to="MainMenuBar" method="_on_menu_exit_game_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_NewGame" to="." method="_on_menu_new_game_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_LoadGame" to="." method="_on_menu_load_game_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_ResumeGame" to="." method="_on_menu_resume_game_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_Options" to="." method="_on_menu_options_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_Credits" to="." method="_on_menu_credits_pressed"] +[connection signal="pressed" from="MainMenuBar/Menu_ExitGame" to="." method="_on_menu_exit_game_pressed"] diff --git a/scripts/CameraMarker.gd b/scripts/CameraMarker.gd index dc0cf37..e2296b9 100644 --- a/scripts/CameraMarker.gd +++ b/scripts/CameraMarker.gd @@ -1,3 +1,4 @@ +class_name CameraMarker extends Sprite2D var size_multiplier:float @@ -26,3 +27,7 @@ func _on_camera_zoom_2d_camera_zoom_changed(new_zoom_factor): func _on_main_worldgen_ready() -> void: 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 diff --git a/scripts/CameraZoom2D.gd b/scripts/CameraZoom2D.gd index 498471d..dc3d124 100644 --- a/scripts/CameraZoom2D.gd +++ b/scripts/CameraZoom2D.gd @@ -15,18 +15,6 @@ var x_min_limit:int = self.game_res.x/2 - chunk_in_px.x #@onready var captured_image = $CapturedImage -func _on_main_worldgen_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 _on_set_camera_position(pos: Vector2) -> void: - self.position = pos - - func _process(_delta) -> void: Globals.CAMERA_POSITION = self.position @@ -142,5 +130,17 @@ func take_screenshot() -> void: var captured_image:Image = get_viewport().get_texture().get_image() 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/ChunkHandler.gd b/scripts/ChunkHandler.gd index 841da41..8e4a2c9 100644 --- a/scripts/ChunkHandler.gd +++ b/scripts/ChunkHandler.gd @@ -37,56 +37,6 @@ func _exit_tree(): func _init() -> void: self.name = "ChunkHandler" - -func _on_main_worldgen_ready(): - if !thread.is_started(): - thread.start(start_chunkgen, Thread.PRIORITY_NORMAL) - clean_up_chunks() - - -func _ready(): - mutex = Mutex.new() - semaphore = Semaphore.new() - exit_thread = false - - if !thread: - thread = Thread.new() - - process_delay_chunks() - process_delay_stats() - - -func process_delay_chunks() -> void: - while true: - update_chunks() - await get_tree().create_timer(0.05).timeout - - -func process_delay_stats() -> void: - # emit stats about chunk amounts every 0,5s - while true: - emit_signal("chunk_stats", self.chunks.size(), self.chunks_to_remove.size()) - await get_tree().create_timer(0.5).timeout - - -func start_chunkgen(): - while true: - semaphore.wait() - - mutex.lock() - var should_exit = exit_thread # Protect with Mutex. - mutex.unlock() - - if should_exit: - break - - # work on emptying the generation queue - if not chunk_queue.is_empty(): - mutex.lock() - var vars = chunk_queue.pop_front() - mutex.unlock() - - load_chunk(vars[0].y, vars[0].x, vars[1]) func clean_up_chunks(): while true: @@ -98,6 +48,7 @@ func clean_up_chunks(): await get_tree().create_timer(0.02).timeout + func correction_factor(d) -> float: if Globals.CAMERA_ZOOM_LEVEL < 0.6: return d * 2.0 @@ -122,6 +73,55 @@ func load_chunk(y:int, x:int, key): chunks[key] = chunk mutex.unlock() + +func process_delay_chunks() -> void: + while true: + update_chunks() + await get_tree().create_timer(0.05).timeout + + +func process_delay_stats() -> void: + # emit stats about chunk amounts every 0,5s + while true: + emit_signal("chunk_stats", self.chunks.size(), self.chunks_to_remove.size()) + await get_tree().create_timer(0.5).timeout + + +func set_ready(): + mutex = Mutex.new() + semaphore = Semaphore.new() + exit_thread = false + + if !thread: + thread = Thread.new() + + if !thread.is_started(): + thread.start(start_chunkgen, Thread.PRIORITY_NORMAL) + clean_up_chunks() + + process_delay_chunks() + process_delay_stats() + + +func start_chunkgen(): + while true: + semaphore.wait() + + mutex.lock() + var should_exit = exit_thread # Protect with Mutex. + mutex.unlock() + + if should_exit: + break + + # work on emptying the generation queue + if not chunk_queue.is_empty(): + mutex.lock() + var vars = chunk_queue.pop_front() + mutex.unlock() + + load_chunk(vars[0].y, vars[0].x, vars[1]) + func update_chunks(): var p_x:float = int(Globals.CAMERA_POSITION.x- Globals.CHUNK_SIZE.x) / Globals.TILE_SIZE_X / Globals.CHUNK_SIZE.x diff --git a/scripts/EventBus.gd b/scripts/EventBus.gd new file mode 100644 index 0000000..0053d62 --- /dev/null +++ b/scripts/EventBus.gd @@ -0,0 +1,95 @@ +class_name EventBus +extends Node + +@onready var node_main = get_parent() +@onready var node_mainmenu = find_child("MainMenu") +@onready var node_game = find_child("Game") + + +# The idea is for the user to be able to choose the map from GUI later +var map_filenames:Array = [ + "res://maps/tampere_10x10km_1000px.png", + "res://maps/tampere_10x10km_1024px.png", + "res://maps/varkaus_256x256px_test.png", + "res://maps/tampere_256px.png", + "res://maps/tampere_10x10km_4096px.png" + ] +var map_filename:String = map_filenames[3] +var _world_generator:WorldGenerator + + +func _ready(): + node_mainmenu.set_ready() + + +func _unhandled_input(event) -> void: + if event.is_action_pressed("open_main_menu"): + # move mainmenu to current game camera position + var mainmenu_pos = Globals.CAMERA_POSITION + mainmenu_pos.x -= DisplayServer.window_get_size(0).x/2 + mainmenu_pos.y -= DisplayServer.window_get_size(0).y/2 + self.find_child("MainMenu").position = mainmenu_pos + + # show the menu + toggle_main_menu_visibility() + #await get_tree().create_timer(0.2).timeout + self.find_child("Game").process_mode = PROCESS_MODE_DISABLED + + +func _on_mainmenu_button_pressed(button:int): + match button: + Globals.MAINMENU_NEW_GAME: + start_new_game() + node_mainmenu.find_child("Menu_NewGame").disabled = true + node_mainmenu.find_child("Menu_ResumeGame").disabled = false + toggle_main_menu_visibility() + + Globals.MAINMENU_LOAD_GAME: + pass + + Globals.MAINMENU_RESUME_GAME: + # TODO save camera position before opening menu, restore camera position when closing menu + node_game.process_mode = PROCESS_MODE_INHERIT + toggle_main_menu_visibility() + + Globals.MAINMENU_OPTIONS: + pass + + Globals.MAINMENU_CREDITS: + pass + + Globals.MAINMENU_QUIT_GAME: + node_mainmenu.quit_game() + + _: + push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button) + + +func start_new_game(): + # create a new world with worldgenerator + _world_generator = WorldGenerator.new() + if !_world_generator.generate_world(map_filename): + push_error("World generation failed :-(") + node_main.quit_game() + + node_main.unpause_game() + node_game.set_ready() + self.set_camera_position( + Vector2(Globals.map_size / 2.0 * Globals.TILE_SIZE_X, + Globals.map_size / 2.0 * Globals.TILE_SIZE_Y) + ) + + +func toggle_main_menu_visibility(): + node_game.toggle_visibility() + + if node_mainmenu.visible: + node_mainmenu.hide() + else: + node_mainmenu.show() + +func set_camera_position(pos:Vector2): + node_game.set_camera_position(pos) + + + diff --git a/scripts/Game.gd b/scripts/Game.gd new file mode 100644 index 0000000..f765673 --- /dev/null +++ b/scripts/Game.gd @@ -0,0 +1,47 @@ +class_name Game +extends Node2D + +@onready var node_camera:CameraZoom2D +@onready var node_chunkhandler:ChunkHandler +@onready var node_minimap:Minimap +@onready var node_mapbackground:MapBackground +@onready var node_uilayer + + +func _ready() -> void: + node_camera = find_child("CameraZoom2D") + node_chunkhandler = find_child("ChunkHandler") + node_minimap = find_child("Minimap") + node_mapbackground = find_child("MapBackground") + node_uilayer = find_child("UILayer") + + +# sets the minimap texture as map background to avoid jarring transitions +func _on_minimap_set_map_background_texture(sprite, scaling:Vector2) -> void: + self.set_map_background_texture(sprite, scaling) + + +func set_ready() -> void: + node_camera.set_ready() + node_chunkhandler.set_ready() + node_minimap.set_ready() + + +func set_map_background_texture(sprite, scaling:Vector2) -> void: + node_mapbackground.set_map_background_texture(sprite, scaling) + + +func set_camera_position(pos:Vector2): + node_camera.set_camera_position(pos) + + +func toggle_visibility(): + if self.visible: + self.hide() + else: + self.show() + + if node_uilayer.visible: + node_uilayer.hide() + else: + node_uilayer.show() diff --git a/scripts/Main.gd b/scripts/Main.gd index 225c083..b425770 100644 --- a/scripts/Main.gd +++ b/scripts/Main.gd @@ -1,107 +1,33 @@ -# OK - "Cube Clicker 2000" where you click a spot and a cube appears there. -# OK - Then add different shapes or colors of cubes. -# OK - Then clamp their positions to be on a grid. -# - Then make it so that when you add yellow cubes, yellow desire meter goes down and green meter goes up. -# - Then click a bunch of grey cubes in a row and have them automatically flatten out into flat grey cubes (roads). -# - Then click and drag to draw the lines of grey cubes. -# - etc. - - # https://github.com/dfloer/SC2k-docs - class_name Main extends Node2D -signal worldgen_ready -signal set_camera_position(pos:Vector2) -var start_new_game_pressed:bool = false +var bus:EventBus -# The idea is for the user to be able to choose the map from GUI later -var map_filenames:Array = [ - "res://maps/tampere_10x10km_1000px.png", - "res://maps/tampere_10x10km_1024px.png", - "res://maps/varkaus_256x256px_test.png", - "res://maps/tampere_256px.png", - "res://maps/tampere_10x10km_4096px.png" - ] -var map_filename:String = map_filenames[3] -var _world_generator:WorldGenerator - -func _init(): +func _init() -> void: # DisplayServer.window_set_size( # #Vector2i(Globals.DEFAULT_X_RES, Globals.DEFAULT_Y_RES) # Vector2i(3800,2000) # ) - Globals.CAMERA_POSITION = Vector2(16*256/2, 16*256/2) + pass - -func start_new_game(): - # create a new world with worldgenerator - _world_generator = WorldGenerator.new() - if !_world_generator.generate_world(map_filename): - push_error("World generation failed :-(") - quit_game() - - # connections are made from GUI - - # tell other classes they can start working after loading is done - emit_signal("worldgen_ready") - - self.find_child("MainMenu").hide() - self.find_child("Game").show() - self.find_child("UILayer").show() - - # center camera to world map - emit_signal( - "set_camera_position", - Vector2(Globals.map_size / 2.0 * Globals.TILE_SIZE_X, - Globals.map_size / 2.0 * Globals.TILE_SIZE_Y) - ) - -func _on_mainmenu_button_pressed(button:int): - match button: - Globals.MAINMENU_NEW_GAME: - start_new_game() - self.find_child("Menu_NewGame").disabled = true - self.find_child("Menu_ResumeGame").disabled = false - Globals.MAINMENU_LOAD_GAME: - pass - Globals.MAINMENU_RESUME_GAME: - # TODO save camera position before opening menu, restore camera position when closing menu - self.find_child("Game").process_mode = PROCESS_MODE_INHERIT - toggle_main_menu_visibility() - Globals.MAINMENU_QUIT_GAME: - quit_game() - _: - push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button) - -func _unhandled_input(event) -> void: - if event.is_action_pressed("open_main_menu"): - # move mainmenu to current game camera position - var mainmenu_pos = Globals.CAMERA_POSITION - mainmenu_pos.x -= DisplayServer.window_get_size(0).x/2 - mainmenu_pos.y -= DisplayServer.window_get_size(0).y/2 - self.find_child("MainMenu").position = mainmenu_pos - # show the menu - toggle_main_menu_visibility() - #await get_tree().create_timer(0.2).timeout - self.find_child("Game").process_mode = PROCESS_MODE_DISABLED +func _ready() -> void: + pause_game() + bus = find_child("EventBus") + bus.set_camera_position(Vector2(16*256/2, 16*256/2)) -func toggle_main_menu_visibility(): - var items = [find_child("Game"), find_child("UILayer"), find_child("MainMenu")] +func pause_game() -> void: + get_tree().paused = true + + +func unpause_game() -> void: + get_tree().paused = false - for item in items: - if !item: - push_error("Error: While toggling main menu visibility. Missing UI element " + item) - if item.visible: - item.hide() - continue - item.show() func quit_game(): get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST) diff --git a/scripts/MainMenuBar.gd b/scripts/MainMenu.gd similarity index 82% rename from scripts/MainMenuBar.gd rename to scripts/MainMenu.gd index 14606fb..ffd991e 100644 --- a/scripts/MainMenuBar.gd +++ b/scripts/MainMenu.gd @@ -1,12 +1,13 @@ -extends GridContainer +class_name MainMenu +extends Control signal button_pressed(button_name) # Connect main menu to Main game -func _ready(): - self.connect("button_pressed", self.find_parent("Main")._on_mainmenu_button_pressed, CONNECT_PERSIST) - self.find_child("Menu_ResumeGame").disabled = true +func set_ready(): + self.connect("button_pressed", self.get_parent()._on_mainmenu_button_pressed, CONNECT_PERSIST) + self.find_child("Menu_ResumeGame").disabled = true func _on_menu_new_game_pressed(): emit_signal("button_pressed", Globals.MAINMENU_NEW_GAME) diff --git a/scripts/MapBackground.gd b/scripts/MapBackground.gd index 9e6c20c..a872b16 100644 --- a/scripts/MapBackground.gd +++ b/scripts/MapBackground.gd @@ -1,7 +1,8 @@ +class_name MapBackground extends Sprite2D # sets the minimap texture as map background to avoid jarring transitions -func _on_minimap_set_map_background_texture(sprite) -> void: +func set_map_background_texture(sprite, scaling:Vector2) -> void: self.texture = sprite - self.scale = Vector2(16, 16) + self.scale = scaling diff --git a/scripts/Minimap.gd b/scripts/Minimap.gd index 6a66bfe..7d7447d 100644 --- a/scripts/Minimap.gd +++ b/scripts/Minimap.gd @@ -2,13 +2,14 @@ class_name Minimap extends Control signal set_camera_position(pos:Vector2) -signal set_map_background_texture(texture) +signal set_map_background_texture(texture, scaling) @onready var minimap_texture:ImageTexture = null @onready var sprite:Sprite2D @onready var is_mouse_inside_minimap:bool = false @onready var position_multiplier:float @onready var area_size:Vector2 +@onready var node_camera_marker:CameraMarker var observe_mouse_inside_minimap:bool = false @@ -22,27 +23,14 @@ func _draw(): pass -func _process(_delta): - if !is_mouse_inside_minimap and observe_mouse_inside_minimap: - Globals.camera_marker.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_main_worldgen_ready(): - # Assuming the area has a child CollisionShape2D with a RectangleShape resource - self.set_process(true) - observe_mouse_inside_minimap = true - area_size = self.get_rect().size - - position_multiplier = Globals.map_size / 32 - - self.generate_minimap() - self.set_minimap() - self.setup_camera_marker() - - func _on_mouse_entered(): is_mouse_inside_minimap = true @@ -53,10 +41,10 @@ func _on_mouse_exited(): func _unhandled_input(event) -> void: if is_mouse_inside_minimap: if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: - Globals.camera_marker.position = get_local_mouse_position() + node_camera_marker.set_camera_marker_position(get_local_mouse_position()) emit_signal( "set_camera_position", - get_local_mouse_position() * position_multiplier # 8 on 256 size map. need a forumla to calculate this + get_local_mouse_position() * position_multiplier ) @@ -83,6 +71,10 @@ func generate_minimap() -> void: minimap_texture = ImageTexture.create_from_image(image) + +func set_camera_marker() -> void: + node_camera_marker = self.find_child("CameraMarker") + func set_minimap() -> void: self.sprite = self.find_child("MinimapSprite") @@ -97,12 +89,23 @@ func set_minimap() -> void: sprite.scale = Vector2(sx, sy) - emit_signal("set_map_background_texture", sprite.texture) + emit_signal("set_map_background_texture", sprite.texture, Vector2(16, 16)) + +func set_ready() -> void: + # Assuming the area has a child CollisionShape2D with a RectangleShape resource + self.set_process(true) + observe_mouse_inside_minimap = true + area_size = self.get_rect().size + + position_multiplier = Globals.map_size / 32 + + self.generate_minimap() + self.set_minimap() + self.set_camera_marker() -func setup_camera_marker() -> void: - Globals.camera_marker = self.find_child("CameraMarker") +