fix minimap colors

This commit is contained in:
Antti Hakkarainen 2023-02-14 11:29:05 +02:00
parent 57d47c5eed
commit 63ea78592a
10 changed files with 155 additions and 92 deletions

View file

@ -60,4 +60,4 @@ camera_move={
[rendering] [rendering]
driver/threads/thread_model=2 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)

View file

@ -5,6 +5,7 @@ var x:int = -1
var y:int = -1 var y:int = -1
var should_remove:bool = false var should_remove:bool = false
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _init(ypos:int, xpos:int, sr: bool): func _init(ypos:int, xpos:int, sr: bool):
self.x = xpos self.x = xpos
@ -18,9 +19,11 @@ func _init(ypos:int, xpos:int, sr: bool):
y*Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y y*Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y
) )
func _ready(): func _ready():
generate_chunk() generate_chunk()
func _draw(): func _draw():
self.draw_rect( self.draw_rect(
Rect2( Rect2(
@ -33,6 +36,7 @@ func _draw():
false false
) )
func generate_chunk() -> void: func generate_chunk() -> void:
for row in Globals.CHUNK_SIZE.y: for row in Globals.CHUNK_SIZE.y:
for col in Globals.CHUNK_SIZE.x: for col in Globals.CHUNK_SIZE.x:

View file

@ -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/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="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="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/EntityPlacer.gd" id="5_8jju5"]
@ -9,6 +10,9 @@
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_ysxum") script = ExtResource("1_ysxum")
[node name="ChunkHandler" type="Node2D" parent="."]
script = ExtResource("2_6cequ")
[node name="EntityPlacer" type="Control" parent="."] [node name="EntityPlacer" type="Control" parent="."]
layout_mode = 3 layout_mode = 3
anchors_preset = 0 anchors_preset = 0
@ -17,30 +21,37 @@ offset_bottom = 40.0
script = ExtResource("5_8jju5") script = ExtResource("5_8jju5")
[node name="CameraZoom2D" parent="." instance=ExtResource("4_rx82t")] [node name="CameraZoom2D" parent="." instance=ExtResource("4_rx82t")]
position = Vector2(960, 536) position = Vector2(1272, 720)
limit_left = -40000 limit_left = 0
limit_top = -40000 limit_top = 0
limit_right = 40000 limit_right = 65536
limit_bottom = 40000 limit_bottom = 65536
limit_smoothed = true
editor_draw_limits = true editor_draw_limits = true
[node name="UILayer" type="CanvasLayer" parent="."] [node name="UILayer" type="CanvasLayer" parent="."]
[node name="Control" type="Control" parent="UILayer"] [node name="Control" type="Control" parent="UILayer"]
layout_mode = 3 layout_mode = 3
anchors_preset = 5 anchors_preset = 15
anchor_left = 0.5 anchor_right = 1.0
anchor_right = 0.5 anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
script = ExtResource("3_1t1c8") script = ExtResource("3_1t1c8")
metadata/_edit_use_anchors_ = true metadata/_edit_use_anchors_ = true
[node name="ConstructionPanel" type="Panel" parent="UILayer/Control"] [node name="ConstructionPanel" type="Panel" parent="UILayer/Control"]
custom_minimum_size = Vector2(500, 100) custom_minimum_size = Vector2(500, 100)
layout_mode = 0 layout_mode = 1
offset_left = -248.0 anchors_preset = 5
offset_right = 252.0 anchor_left = 0.5
offset_bottom = 120.0 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"] [node name="button_residental" type="Button" parent="UILayer/Control/ConstructionPanel"]
layout_mode = 0 layout_mode = 0
@ -77,33 +88,42 @@ layout_mode = 0
offset_right = 8.0 offset_right = 8.0
offset_bottom = 8.0 offset_bottom = 8.0
[node name="DebugInfo" type="Label" parent="UILayer/Control"] [node name="DebugContainer" type="GridContainer" parent="UILayer/Control"]
layout_mode = 0 layout_mode = 1
offset_left = 648.0 anchors_preset = 1
offset_top = 16.0 anchor_left = 1.0
offset_right = 936.0 anchor_right = 1.0
offset_bottom = 256.0 offset_left = -264.0
horizontal_alignment = 2 offset_top = 24.0
offset_bottom = 424.0
grow_horizontal = 0
[node name="Minimap" type="Container" parent="UILayer"] [node name="DebugInfo" type="Label" parent="UILayer/Control/DebugContainer"]
anchors_preset = -1 layout_mode = 2
anchor_top = -0.044
anchor_right = 0.172 [node name="Minimap" type="Panel" parent="UILayer/Control"]
anchor_bottom = 0.161 clip_contents = true
offset_left = 1696.0 custom_minimum_size = Vector2(512, 512)
offset_top = 735.36 layout_mode = 1
offset_right = 1807.68 anchors_preset = 3
offset_bottom = 1032.16 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") script = ExtResource("5_rg28x")
[node name="CameraMarker" type="Sprite2D" parent="UILayer/Minimap"] [node name="CameraMarker" type="Sprite2D" parent="UILayer/Control/Minimap"]
position = Vector2(296, 392) position = Vector2(-32, 0)
[node name="MinimapSprite" type="Sprite2D" parent="UILayer/Minimap"] [node name="MinimapSprite" type="Sprite2D" parent="UILayer/Control/Minimap"]
position = Vector2(416, 472) texture_repeat = 1
centered = false
[connection signal="set_camera_position" from="." to="CameraZoom2D" method="_on_set_camera_position"] [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_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_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"] [connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_industrial" to="UILayer/Control" method="_on_button_industrial_pressed"]

View file

@ -44,8 +44,8 @@ func _unhandled_input(event):
camera_zoom_in() camera_zoom_in()
if event.is_action_pressed("camera_zoom_out"): if event.is_action_pressed("camera_zoom_out"):
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: if !is_panning_camera and event.pressed:
is_panning_camera = true is_panning_camera = true
if is_panning_camera and !event.pressed: if is_panning_camera and !event.pressed:

View file

@ -22,7 +22,6 @@ var exit_thread = false
var _timer:Timer var _timer:Timer
func _exit_tree(): func _exit_tree():
mutex.lock() mutex.lock()
exit_thread = true exit_thread = true
@ -35,6 +34,22 @@ func _exit_tree():
func _init() -> void: func _init() -> void:
self.name = "ChunkHandler" 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): func _process(_delta):
update_chunks() update_chunks()
@ -46,17 +61,7 @@ func _ready():
exit_thread = false exit_thread = false
thread = Thread.new() 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(): func start_chunkgen():
while true: while true:

View file

@ -3,7 +3,7 @@ extends Control
# var view = get_node("../View") # var view = get_node("../View")
signal button_pressed(button_name) 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 @onready var minimap:Minimap
# name, position # name, position

View file

@ -1,10 +1,31 @@
# File contains global variables or constants so they all are in one place instead # 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 extends Node
var chunks_loaded:int = 0 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 # # 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 # preprocess and store exact tile for every map cell to speed up setting tiles
var map_tile_data:Array[Array] = [[]] var map_tile_data:Array[Array] = [[]]
################################### ###################################
# CAMERA SETTINGS # # CAMERA SETTINGS #
################################### ###################################
# GAME WINDOW DEFAULT SIZE
const DEFAULT_X_RES:int = 1920
const DEFAULT_Y_RES:int = 1080
# current camera zoom level # current camera zoom level
var CAMERA_ZOOM_LEVEL: float = 1.0 var CAMERA_ZOOM_LEVEL: float = 1.0
var CAMERA_POSITION var CAMERA_POSITION
@ -44,10 +70,10 @@ const CAMERA_ZOOM_FACTOR: float = 0.1
const CAMERA_ZOOM_DURATION: float = 0.1 const CAMERA_ZOOM_DURATION: float = 0.1
const CAMERA_PAN_MULTI:float = 2.0 const CAMERA_PAN_MULTI:float = 2.0
# FILE PATHS
const SCENE_PATH:String = "res://scenes/" ###################################
const ART_PATH:String = "res://art/" # UI ELEMENT SETTINGS #
const SCRIPT_PATH:String = "res://scripts" ###################################
# NODE NAMES # NODE NAMES
const WORLD_NODE:String = "World" 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_Y: int = 50
const GUI_BUILD_BUTTON_SIZE: Vector2i = Vector2i(GUI_BUILD_BUTTON_SIZE_X,GUI_BUILD_BUTTON_SIZE_Y) 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? # maybe should use int for these instead for faster matching?
const TYPE_RESIDENTIAL:String = "residential" const TYPE_RESIDENTIAL:String = "residential"
const TYPE_COMMERCIAL:String = "commercial" const TYPE_COMMERCIAL:String = "commercial"
@ -72,9 +94,14 @@ const TYPE_POWERPLANT:String = "powerplant"
const TYPE_ROADS:String = "roads" const TYPE_ROADS:String = "roads"
const TYPE_DEMOLISH:String = "demolish" 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) # 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 GROUND_TILE_COLOR_IN_MAP_FILE: Color = Color(0,0,0)
const WATER_TILE_COLOR_IN_MAP_FILE: Color = Color(1,1,1,1) 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 # min and max sizes for a map so the map won't be unreasonably small or large
const MAP_MIN_HEIGHT:int = 128 const MAP_MIN_HEIGHT:int = 128
@ -135,6 +162,11 @@ var td = {
"key": [Vector2i(0,0)] "key": [Vector2i(0,0)]
} }
} }
###################################
# GAME ERORR MESSAGES #
###################################
# error messages # error messages
const ERROR_BUILDING_TYPE_NOT_SET:String = "Building type not set, while trying to place building." const ERROR_BUILDING_TYPE_NOT_SET:String = "Building type not set, while trying to place building."

View file

@ -20,11 +20,9 @@ var map_filenames:Array = [
"res://maps/tampere_256px.png", "res://maps/tampere_256px.png",
"res://maps/tampere_10x10km_4096px.png" "res://maps/tampere_10x10km_4096px.png"
] ]
var map_filename:String = map_filenames[2] var map_filename:String = map_filenames[4]
var _world_generator:WorldGenerator
var _chunk_handler:ChunkHandler
#var _2d_camera:CameraZoom2D
var _world_generator:WorldGenerator
func _init(): func _init():
# DisplayServer.window_set_size( # DisplayServer.window_set_size(
@ -36,21 +34,16 @@ func _init():
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
# create a new world and worldgenerator # create a new world with worldgenerator
_world_generator = WorldGenerator.new() _world_generator = WorldGenerator.new()
#_2d_camera = CameraZoom2D.new() if !_world_generator.generate_world(map_filename):
push_error("World generation failed :-(")
# add chunk handler if worldgen was successful quit_game()
if _world_generator.generate_world(map_filename):
Globals.worlgen_ready = true # connections are made from GUI
else:
push_error("World generation failed :-(")
_chunk_handler = ChunkHandler.new()
add_child(_chunk_handler)
# tell other classes they can start working after loading is done
emit_signal("worldgen_ready") emit_signal("worldgen_ready")
# center camera to world map # center camera to world map
emit_signal( emit_signal(
"set_camera_position", "set_camera_position",
@ -60,6 +53,5 @@ func _ready():
func quit_game(): func quit_game():
get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST) get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
#SceneTree.quit

View file

@ -1,5 +1,5 @@
class_name Minimap class_name Minimap
extends Node extends Control
@onready var minimap_texture:ImageTexture = null @onready var minimap_texture:ImageTexture = null
@onready var sprite:Sprite2D @onready var sprite:Sprite2D
@ -15,15 +15,14 @@ func _process(_delta):
func _on_main_worldgen_ready(): func _on_main_worldgen_ready():
print("test")
self.generate_minimap() self.generate_minimap()
self.set_minimap() self.set_minimap()
func generate_minimap() -> void: func generate_minimap() -> void:
var image = Image.new() 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) image.resize(Globals.map_size, Globals.map_size)
for y in Globals.map_size: for y in Globals.map_size:
for x 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]: match Globals.map_terrain_data[y][x]:
Globals.TILE_WATER: Globals.TILE_WATER:
color = Color(0,0,255) color = Globals.minimap_colors.get(Globals.TILE_WATER)
Globals.TILE_TERRAIN: Globals.TILE_TERRAIN:
color = Color(148,113,71) color = Globals.minimap_colors.get(Globals.TILE_TERRAIN)
Globals.TILE_FOREST: Globals.TILE_FOREST:
color = Color(0,255,0) color = Globals.minimap_colors.get(Globals.TILE_FOREST)
_: #default _: #default
color = Color(255,0,255) color = Globals.minimap_colors.get("default")
image.set_pixel(x, y, color) image.set_pixel(x, y, color)
minimap_texture = ImageTexture.create_from_image(image) minimap_texture = ImageTexture.create_from_image(image)
func set_minimap() -> void: func set_minimap() -> void:
sprite = self.get_child(1) sprite = self.get_child(1)
sprite.texture = minimap_texture 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)

View file

@ -101,7 +101,7 @@ func generate_world(filename) -> bool:
return false return false
if (image.get_size().x / image.get_size().y) != 1: 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 return false
image_size = image.get_size() image_size = image.get_size()