From 63ea78592afcf051f9f5d444798d6e7f5b3b91d3 Mon Sep 17 00:00:00 2001 From: Antti Hakkarainen Date: Tue, 14 Feb 2023 11:29:05 +0200 Subject: [PATCH] fix minimap colors --- project.godot | 2 +- scenes/Chunk.gd | 4 ++ scenes/Main.tscn | 88 ++++++++++++++++++++++++--------------- scripts/CameraZoom2D.gd | 4 +- scripts/ChunkHandler.gd | 27 +++++++----- scripts/Control.gd | 2 +- scripts/Globals.gd | 56 +++++++++++++++++++------ scripts/Main.gd | 26 ++++-------- scripts/Minimap.gd | 36 ++++++++++------ scripts/WorldGenerator.gd | 2 +- 10 files changed, 155 insertions(+), 92 deletions(-) diff --git a/project.godot b/project.godot index 8362343..6e7f078 100644 --- a/project.godot +++ b/project.godot @@ -60,4 +60,4 @@ camera_move={ [rendering] driver/threads/thread_model=2 -environment/defaults/default_clear_color=Color(0.658824, 0.329412, 0, 1) +environment/defaults/default_clear_color=Color(0, 0, 0, 1) diff --git a/scenes/Chunk.gd b/scenes/Chunk.gd index c334296..d5da155 100644 --- a/scenes/Chunk.gd +++ b/scenes/Chunk.gd @@ -5,6 +5,7 @@ var x:int = -1 var y:int = -1 var should_remove:bool = false + # Called when the node enters the scene tree for the first time. func _init(ypos:int, xpos:int, sr: bool): self.x = xpos @@ -18,9 +19,11 @@ func _init(ypos:int, xpos:int, sr: bool): y*Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y ) + func _ready(): generate_chunk() + func _draw(): self.draw_rect( Rect2( @@ -33,6 +36,7 @@ func _draw(): false ) + func generate_chunk() -> void: for row in Globals.CHUNK_SIZE.y: for col in Globals.CHUNK_SIZE.x: diff --git a/scenes/Main.tscn b/scenes/Main.tscn index 79b2b08..943d99d 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=6 format=3 uid="uid://nfayf78xiuap"] +[gd_scene load_steps=7 format=3 uid="uid://nfayf78xiuap"] [ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"] +[ext_resource type="Script" path="res://scripts/ChunkHandler.gd" id="2_6cequ"] [ext_resource type="Script" path="res://scripts/Control.gd" id="3_1t1c8"] [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"] @@ -9,6 +10,9 @@ [node name="Main" type="Node2D"] script = ExtResource("1_ysxum") +[node name="ChunkHandler" type="Node2D" parent="."] +script = ExtResource("2_6cequ") + [node name="EntityPlacer" type="Control" parent="."] layout_mode = 3 anchors_preset = 0 @@ -17,30 +21,37 @@ offset_bottom = 40.0 script = ExtResource("5_8jju5") [node name="CameraZoom2D" parent="." instance=ExtResource("4_rx82t")] -position = Vector2(960, 536) -limit_left = -40000 -limit_top = -40000 -limit_right = 40000 -limit_bottom = 40000 +position = Vector2(1272, 720) +limit_left = 0 +limit_top = 0 +limit_right = 65536 +limit_bottom = 65536 +limit_smoothed = true editor_draw_limits = true [node name="UILayer" type="CanvasLayer" parent="."] [node name="Control" type="Control" parent="UILayer"] layout_mode = 3 -anchors_preset = 5 -anchor_left = 0.5 -anchor_right = 0.5 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 grow_horizontal = 2 +grow_vertical = 2 +mouse_filter = 2 script = ExtResource("3_1t1c8") metadata/_edit_use_anchors_ = true [node name="ConstructionPanel" type="Panel" parent="UILayer/Control"] custom_minimum_size = Vector2(500, 100) -layout_mode = 0 -offset_left = -248.0 -offset_right = 252.0 -offset_bottom = 120.0 +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -250.0 +offset_right = 250.0 +offset_bottom = 100.0 +grow_horizontal = 2 [node name="button_residental" type="Button" parent="UILayer/Control/ConstructionPanel"] layout_mode = 0 @@ -77,33 +88,42 @@ layout_mode = 0 offset_right = 8.0 offset_bottom = 8.0 -[node name="DebugInfo" type="Label" parent="UILayer/Control"] -layout_mode = 0 -offset_left = 648.0 -offset_top = 16.0 -offset_right = 936.0 -offset_bottom = 256.0 -horizontal_alignment = 2 +[node name="DebugContainer" type="GridContainer" parent="UILayer/Control"] +layout_mode = 1 +anchors_preset = 1 +anchor_left = 1.0 +anchor_right = 1.0 +offset_left = -264.0 +offset_top = 24.0 +offset_bottom = 424.0 +grow_horizontal = 0 -[node name="Minimap" type="Container" parent="UILayer"] -anchors_preset = -1 -anchor_top = -0.044 -anchor_right = 0.172 -anchor_bottom = 0.161 -offset_left = 1696.0 -offset_top = 735.36 -offset_right = 1807.68 -offset_bottom = 1032.16 +[node name="DebugInfo" type="Label" parent="UILayer/Control/DebugContainer"] +layout_mode = 2 + +[node name="Minimap" type="Panel" parent="UILayer/Control"] +clip_contents = true +custom_minimum_size = Vector2(512, 512) +layout_mode = 1 +anchors_preset = 3 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 0 +grow_vertical = 0 script = ExtResource("5_rg28x") -[node name="CameraMarker" type="Sprite2D" parent="UILayer/Minimap"] -position = Vector2(296, 392) +[node name="CameraMarker" type="Sprite2D" parent="UILayer/Control/Minimap"] +position = Vector2(-32, 0) -[node name="MinimapSprite" type="Sprite2D" parent="UILayer/Minimap"] -position = Vector2(416, 472) +[node name="MinimapSprite" type="Sprite2D" parent="UILayer/Control/Minimap"] +texture_repeat = 1 +centered = false [connection signal="set_camera_position" from="." to="CameraZoom2D" method="_on_set_camera_position"] -[connection signal="worldgen_ready" from="." to="UILayer/Minimap" method="_on_main_worldgen_ready"] +[connection signal="worldgen_ready" from="." to="ChunkHandler" method="_on_main_worldgen_ready"] +[connection signal="worldgen_ready" from="." to="UILayer/Control/Minimap" method="_on_main_worldgen_ready"] [connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_residental" to="UILayer/Control" method="_on_button_residental_pressed"] [connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_commercial" to="UILayer/Control" method="_on_button_commercial_pressed"] [connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_industrial" to="UILayer/Control" method="_on_button_industrial_pressed"] diff --git a/scripts/CameraZoom2D.gd b/scripts/CameraZoom2D.gd index a79ed3d..e1fec10 100644 --- a/scripts/CameraZoom2D.gd +++ b/scripts/CameraZoom2D.gd @@ -44,8 +44,8 @@ func _unhandled_input(event): camera_zoom_in() if event.is_action_pressed("camera_zoom_out"): camera_zoom_out() - - if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: + + 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: diff --git a/scripts/ChunkHandler.gd b/scripts/ChunkHandler.gd index 70ccee6..8cf001c 100644 --- a/scripts/ChunkHandler.gd +++ b/scripts/ChunkHandler.gd @@ -22,7 +22,6 @@ var exit_thread = false var _timer:Timer - func _exit_tree(): mutex.lock() exit_thread = true @@ -35,6 +34,22 @@ func _exit_tree(): func _init() -> void: self.name = "ChunkHandler" + +func _on_main_worldgen_ready(): + thread.start(start_chunkgen, Thread.PRIORITY_NORMAL) + + # chunk cleanup timer + _timer = Timer.new() + add_child(_timer) + _timer.connect("timeout", _on_timer_timeout, 0) + _timer.set_wait_time(0.05) + _timer.set_one_shot(false) + _timer.start() + + +func _on_timer_timeout(): + clean_up_chunks() + func _process(_delta): update_chunks() @@ -46,17 +61,7 @@ func _ready(): exit_thread = false thread = Thread.new() - thread.start(start_chunkgen, Thread.PRIORITY_NORMAL) - _timer = Timer.new() - add_child(_timer) - _timer.connect("timeout", _on_timer_timeout, 0) - _timer.set_wait_time(0.05) - _timer.set_one_shot(false) - _timer.start() - -func _on_timer_timeout(): - clean_up_chunks() func start_chunkgen(): while true: diff --git a/scripts/Control.gd b/scripts/Control.gd index f91b29f..dda4d6f 100644 --- a/scripts/Control.gd +++ b/scripts/Control.gd @@ -3,7 +3,7 @@ extends Control # var view = get_node("../View") signal button_pressed(button_name) -@onready var debug_info = get_node(Globals.DEBUGINFO_NODE) +@onready var debug_info = get_node("DebugContainer/" + Globals.DEBUGINFO_NODE) @onready var minimap:Minimap # name, position diff --git a/scripts/Globals.gd b/scripts/Globals.gd index 14864cb..1d180c1 100644 --- a/scripts/Globals.gd +++ b/scripts/Globals.gd @@ -1,10 +1,31 @@ # File contains global variables or constants so they all are in one place instead -# of a million files. So you can adjust them easily from one place if needed. +# of a million files. So you can adjust them "easily" from one place if needed. extends Node var chunks_loaded:int = 0 -var worlgen_ready:bool = false + + +################################### +# FILE PATHS # +################################### + +const SCENE_PATH:String = "res://scenes/" +const ART_PATH:String = "res://art/" +const SCRIPT_PATH:String = "res://scripts" + + +################################### +# MINIMAP SETTINGS # +################################### + +var minimap_colors:Dictionary = { + Globals.TILE_WATER : Color8(42, 31, 255), + Globals.TILE_TERRAIN: Color8(148, 113, 71), + Globals.TILE_FOREST: Color8(0,123,19), + "default": Color8(255,0,255), +} + ################################### # CHUNK AND TERRAIN SETTINGS # @@ -29,10 +50,15 @@ var map_terrain_data:Array[Array] = [[]] # preprocess and store exact tile for every map cell to speed up setting tiles var map_tile_data:Array[Array] = [[]] + ################################### # CAMERA SETTINGS # ################################### +# GAME WINDOW DEFAULT SIZE +const DEFAULT_X_RES:int = 1920 +const DEFAULT_Y_RES:int = 1080 + # current camera zoom level var CAMERA_ZOOM_LEVEL: float = 1.0 var CAMERA_POSITION @@ -44,10 +70,10 @@ const CAMERA_ZOOM_FACTOR: float = 0.1 const CAMERA_ZOOM_DURATION: float = 0.1 const CAMERA_PAN_MULTI:float = 2.0 -# FILE PATHS -const SCENE_PATH:String = "res://scenes/" -const ART_PATH:String = "res://art/" -const SCRIPT_PATH:String = "res://scripts" + +################################### +# UI ELEMENT SETTINGS # +################################### # NODE NAMES const WORLD_NODE:String = "World" @@ -58,10 +84,6 @@ const GUI_BUILD_BUTTON_SIZE_X: int = 50 const GUI_BUILD_BUTTON_SIZE_Y: int = 50 const GUI_BUILD_BUTTON_SIZE: Vector2i = Vector2i(GUI_BUILD_BUTTON_SIZE_X,GUI_BUILD_BUTTON_SIZE_Y) -# GAME WINDOW DEFAULT SIZE -const DEFAULT_X_RES:int = 1920 -const DEFAULT_Y_RES:int = 1080 - # maybe should use int for these instead for faster matching? const TYPE_RESIDENTIAL:String = "residential" const TYPE_COMMERCIAL:String = "commercial" @@ -72,9 +94,14 @@ const TYPE_POWERPLANT:String = "powerplant" const TYPE_ROADS:String = "roads" const TYPE_DEMOLISH:String = "demolish" + +################################### +# WORLD GENERATION SETTINGS # +################################### + # city map generation file should have black ground (0,0,0) and white water (1,1,1) -const GROUND_TILE_COLOR_IN_MAP_FILE: Color = Color(0,0,0,1) -const WATER_TILE_COLOR_IN_MAP_FILE: Color = Color(1,1,1,1) +const GROUND_TILE_COLOR_IN_MAP_FILE: Color = Color(0,0,0) +const WATER_TILE_COLOR_IN_MAP_FILE: Color = Color(1,1,1) # min and max sizes for a map so the map won't be unreasonably small or large const MAP_MIN_HEIGHT:int = 128 @@ -135,6 +162,11 @@ var td = { "key": [Vector2i(0,0)] } } + + +################################### +# GAME ERORR MESSAGES # +################################### # error messages const ERROR_BUILDING_TYPE_NOT_SET:String = "Building type not set, while trying to place building." diff --git a/scripts/Main.gd b/scripts/Main.gd index 0d92128..112c270 100644 --- a/scripts/Main.gd +++ b/scripts/Main.gd @@ -20,11 +20,9 @@ var map_filenames:Array = [ "res://maps/tampere_256px.png", "res://maps/tampere_10x10km_4096px.png" ] -var map_filename:String = map_filenames[2] -var _world_generator:WorldGenerator -var _chunk_handler:ChunkHandler -#var _2d_camera:CameraZoom2D +var map_filename:String = map_filenames[4] +var _world_generator:WorldGenerator func _init(): # DisplayServer.window_set_size( @@ -36,21 +34,16 @@ func _init(): # Called when the node enters the scene tree for the first time. func _ready(): - # create a new world and worldgenerator + # create a new world with worldgenerator _world_generator = WorldGenerator.new() - #_2d_camera = CameraZoom2D.new() - - # add chunk handler if worldgen was successful - if _world_generator.generate_world(map_filename): - Globals.worlgen_ready = true - else: - push_error("World generation failed :-(") - - _chunk_handler = ChunkHandler.new() - add_child(_chunk_handler) + 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") - # center camera to world map emit_signal( "set_camera_position", @@ -60,6 +53,5 @@ func _ready(): func quit_game(): get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST) - #SceneTree.quit diff --git a/scripts/Minimap.gd b/scripts/Minimap.gd index b1b3509..aadbbbf 100644 --- a/scripts/Minimap.gd +++ b/scripts/Minimap.gd @@ -1,5 +1,5 @@ class_name Minimap -extends Node +extends Control @onready var minimap_texture:ImageTexture = null @onready var sprite:Sprite2D @@ -15,15 +15,14 @@ func _process(_delta): func _on_main_worldgen_ready(): - print("test") self.generate_minimap() self.set_minimap() -func generate_minimap() -> void: +func generate_minimap() -> void: var image = Image.new() - print(Globals.map_size) - image = Image.create(Globals.map_size, Globals.map_size, false, Image.FORMAT_RGB8) + + image = Image.create(Globals.map_size, Globals.map_size, false, Image.FORMAT_RGBAF) image.resize(Globals.map_size, Globals.map_size) for y in Globals.map_size: for x in Globals.map_size: @@ -31,24 +30,35 @@ func generate_minimap() -> void: match Globals.map_terrain_data[y][x]: Globals.TILE_WATER: - color = Color(0,0,255) - Globals.TILE_TERRAIN: - color = Color(148,113,71) + color = Globals.minimap_colors.get(Globals.TILE_WATER) + Globals.TILE_TERRAIN: + color = Globals.minimap_colors.get(Globals.TILE_TERRAIN) Globals.TILE_FOREST: - color = Color(0,255,0) + color = Globals.minimap_colors.get(Globals.TILE_FOREST) _: #default - color = Color(255,0,255) + color = Globals.minimap_colors.get("default") image.set_pixel(x, y, color) - minimap_texture = ImageTexture.create_from_image(image) + minimap_texture = ImageTexture.create_from_image(image) func set_minimap() -> void: sprite = self.get_child(1) sprite.texture = minimap_texture - sprite.set_scale(Vector2i(2,2)) - sprite.set_position(Vector2(0, 0)) + + # Assuming the area has a child CollisionShape2D with a RectangleShape resource + var area_size = self.get_rect() + area_size = area_size.size + + # The size of a sprite is determined from its texture + var texture_size = sprite.texture.get_size() + + # Calculate which scale the sprite should have to match the size of the area + var sx = area_size.x / texture_size.x + var sy = area_size.y / texture_size.y + + sprite.scale = Vector2(sx, sy) diff --git a/scripts/WorldGenerator.gd b/scripts/WorldGenerator.gd index 0911996..ad6b4ad 100644 --- a/scripts/WorldGenerator.gd +++ b/scripts/WorldGenerator.gd @@ -101,7 +101,7 @@ func generate_world(filename) -> bool: return false if (image.get_size().x / image.get_size().y) != 1: - push_error("Error: image size was invalidin world generator") + push_error("Error: image size was invalid in world generator") return false image_size = image.get_size()