give each debug info its own label

This commit is contained in:
Antti Hakkarainen 2023-02-18 23:49:16 +02:00
parent 01d896c7dc
commit 63afd68cbf
13 changed files with 262 additions and 128 deletions

View file

@ -9,6 +9,8 @@ extends Node
@onready var node_uilayer:UILayer
var previous_zoom: float
# 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",
@ -51,17 +53,21 @@ 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
node_mainmenu.set_position(mainmenu_pos, false)
# show the menu
node_mainmenu.set_visible(true)
node_game.set_visible(false)
node_uilayer.set_visible(false)
node_main.pause_game()
if node_mainmenu.visible:
self.toggle_mainmenu()
node_main.unpause_game()
else:
node_mainmenu.set_scale(Vector2(1.0/Globals.CAMERA_ZOOM_LEVEL, 1.0/Globals.CAMERA_ZOOM_LEVEL))
# 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
node_mainmenu.set_position(mainmenu_pos, false)
self.toggle_mainmenu()
node_main.pause_game()
###################################
# GAME CAMERA #
@ -96,6 +102,14 @@ func _unhandled_input(event) -> void:
node_camera.clamp_camera_position()
func _on_camera_rotation_changed(new_rotation):
node_uilayer.camera_rotation_changed(new_rotation)
func _on_camera_zoom_changed(new_zoom_factor):
node_uilayer.camera_zoom_changed(new_zoom_factor)
func _on_control_infolayer_button_pressed(button_type):
var current_layer:int = node_infolayer.get_draw_mode()
@ -116,7 +130,7 @@ func _on_mainmenu_button_pressed(button:int):
pass
Globals.MAINMENU_RESUME_GAME:
resume_game()
self.toggle_mainmenu()
Globals.MAINMENU_OPTIONS:
pass
@ -129,18 +143,27 @@ func _on_mainmenu_button_pressed(button:int):
_:
push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button)
func resume_game() -> void:
# TODO save camera position before opening menu, restore camera position when closing menu
node_main.unpause_game()
node_mainmenu.set_visible(false)
node_game.set_visible(true)
node_uilayer.set_visible(true)
func set_camera_position(pos:Vector2):
func _on_set_camera_position(pos:Vector2):
node_camera.set_camera_position(pos)
func toggle_mainmenu() -> void:
# TODO save camera position before opening menu, restore camera position when closing menu
if node_mainmenu.visible:
node_mainmenu.set_visible(false)
else:
node_mainmenu.set_visible(true)
if node_game.visible:
node_game.set_visible(false)
else:
node_game.set_visible(true)
if node_uilayer.visible:
node_uilayer.set_visible(false)
else:
node_uilayer.set_visible(true)
func start_new_game():
@ -175,3 +198,6 @@ func start_new_game():

View file

@ -22,7 +22,7 @@ var chunks_loaded:int = 0
@export var map_parcel_data:Array[Array] = [[]]
# current camera zoom level
@export var CAMERA_ZOOM_LEVEL:float
@export var CAMERA_ZOOM_LEVEL:float = 1.0
@export var CAMERA_POSITION:Vector2i
# minimap texture, used also as save game's imagetexture

View file

@ -94,30 +94,6 @@ func generate_biomes() -> void:
# can add other tresholds here for other biomes
# forests are not generated yet so can just compare water and terrain
func is_filled_with_water(coords:Vector2i) -> bool:
var terrain_tile_count:int = 0
# 0*64, 0*64 +64-1 = 0-63
# 1*64, 1*64 +63 = 64-127
# 2*64, 2*64 +63 = 128-191
# 3*64, 3*64 +63 = 192-255
for y in range(
coords.y*Globals.PARCEL_HEIGHT,
coords.y*Globals.PARCEL_HEIGHT + Globals.PARCEL_HEIGHT-1
):
for x in range(
coords.x*Globals.PARCEL_WIDTH,
coords.x*Globals.PARCEL_WIDTH + Globals.PARCEL_WIDTH-1
):
if Globals.map_terrain_data[y][x] == Globals.TILE_TERRAIN:
terrain_tile_count += 1
# parcel is ok if it has at least one land
if terrain_tile_count > 0:
return false
return true
func generate_parcels() -> void:
# divide the land area Cadastres / Parcels
# TODO better solution, this is something my skills were able to handle at proto stage
@ -145,16 +121,6 @@ func generate_parcels() -> void:
var total_parcels = Globals.map_size/Globals.PARCEL_WIDTH * Globals.map_size / Globals.PARCEL_HEIGHT
give_starting_parcels_for_city(total_parcels)
func give_starting_parcels_for_city(_amount:int) -> void:
# gives a x*y parcel initial starting area for the player
var p_x = Globals.map_size/Globals.PARCEL_WIDTH/2
var p_y = Globals.map_size/Globals.PARCEL_HEIGHT/2
for y in range(0, Globals.STARTING_AREA_HEIGHT_IN_PARCELS):
for x in range(0, Globals.STARTING_AREA_WIDTH_IN_PARCELS):
if Globals.map_parcel_data[p_y-y][p_x-x] != null:
Globals.map_parcel_data[p_y-y][p_x-x].owner = Globals.PARCEL_CITY
func generate_world(filename) -> bool:
# Try to load the image which we used to place water & ground to world map
@ -200,7 +166,44 @@ func generate_world(filename) -> bool:
emit_signal("worldgenerator_function_called", (end-start)/1000.0, function[0])
return true
func give_starting_parcels_for_city(_amount:int) -> void:
# gives a x*y parcel initial starting area for the player
var p_x = Globals.map_size/Globals.PARCEL_WIDTH/2
var p_y = Globals.map_size/Globals.PARCEL_HEIGHT/2
for y in range(0, Globals.STARTING_AREA_HEIGHT_IN_PARCELS):
for x in range(0, Globals.STARTING_AREA_WIDTH_IN_PARCELS):
if Globals.map_parcel_data[p_y-y][p_x-x] != null:
Globals.map_parcel_data[p_y-y][p_x-x].owner = Globals.PARCEL_CITY
# forests are not generated yet so can just compare water and terrain
func is_filled_with_water(coords:Vector2i) -> bool:
#var terrain_tile_count:int = 0
# 0*64, 0*64 +64-1 = 0-63
# 1*64, 1*64 +63 = 64-127
# 2*64, 2*64 +63 = 128-191
# 3*64, 3*64 +63 = 192-255
for y in range(
coords.y*Globals.PARCEL_HEIGHT,
coords.y*Globals.PARCEL_HEIGHT + Globals.PARCEL_HEIGHT-1
):
for x in range(
coords.x*Globals.PARCEL_WIDTH,
coords.x*Globals.PARCEL_WIDTH + Globals.PARCEL_WIDTH-1
):
if Globals.map_terrain_data[y][x] == Globals.TILE_TERRAIN:
return false
#terrain_tile_count += 1
# parcel is ok if it has at least one land
#if terrain_tile_count > 0:
# return false
return true
func read_image_pixel_data() -> void:
# initialize the array to have enough rows

View file

@ -4,6 +4,7 @@ extends Camera2D
signal camera_rotation_changed(new_rotation)
signal camera_zoom_changed(new_zoom_factor)
signal camera_pos_changed(new_pos)
var is_panning_camera:bool = false
var tween:Tween
@ -31,13 +32,17 @@ func get_camera_rotation():
return self.rotation
func get_camera_zoom_level() -> float:
return Globals.CAMERA_ZOOM_LEVEL
func set_camera_panning(value:bool) -> void:
self.is_panning_camera = value
func set_camera_position(pos: Vector2) -> void:
self.position = pos
print("camera pos set:", self.position)
emit_signal("camera_pos_changed", pos)
func set_camera_limits() -> void:

View file

@ -10,7 +10,7 @@ extends Node2D
# which is extremely slow in godot 4.0, 4096x4096 takes minutes to fill with set_cell() commands
signal chunk_stats(chunks, removal_queue)
signal chunk_stats(chunks:int, removal_queue:int)
var chunks:Dictionary = {}
var chunks_to_remove:Array[Chunk] = []

View file

@ -3,13 +3,9 @@ extends Control
signal button_pressed(button_name)
# Connect main menu to Main game
#func _process(delta):
# print("aaa")
func set_ready():
# Connect main menu to Main game
self.connect("button_pressed", self.get_parent()._on_mainmenu_button_pressed, CONNECT_PERSIST)
self.find_child("Menu_ResumeGame").disabled = true

View file

@ -11,12 +11,12 @@ func _draw():
# Rotates the box if camera is rotated
func _on_camera_zoom_2d_camera_rotation_changed(new_rotation):
func set_camera_marker_rotation(new_rotation):
self.rotation = new_rotation
# Redraws the box to a different size if camera is zoomed
func _on_camera_zoom_2d_camera_zoom_changed(new_zoom_factor):
func set_camera_marker_zoom(new_zoom_factor):
w_s = DisplayServer.window_get_size(0) / size_multiplier
w_s.x /= new_zoom_factor
w_s.y /= new_zoom_factor

View file

@ -0,0 +1,85 @@
class_name DebugInfo
extends GridContainer
@onready var fps_label:Label
@onready var cam_pos_label:Label
@onready var cam_rotation_label:Label
@onready var cam_zoom_label:Label
@onready var chunk_label:Label
@onready var chunk_del_label:Label
func set_ready():
fps_label = find_child("FPSLabel")
cam_pos_label = find_child("CamPosLabel")
cam_rotation_label = find_child("CamRotationLabel")
cam_zoom_label = find_child("CamZoomLabel")
chunk_label = find_child("ChunkLabel")
chunk_del_label = find_child("ChunkDelLabel")
func _process(_delta):
self.set_fps_label(Engine.get_frames_per_second())
func _on_camera_pos_changed(new_pos):
self.set_cam_pos_label(new_pos)
func _on_camera_rotation_changed(new_rotation):
self.set_cam_rotation(new_rotation)
func _on_camera_zoom_changed(new_zoom_factor):
self.set_cam_zoom_label(new_zoom_factor)
func _on_chunk_handler_chunk_stats(chunks:int, removal_queue:int):
if !chunk_label or !chunk_del_label:
return
self.set_chunk_label(chunks)
self.set_chunk_del_label(removal_queue)
func set_fps_label(info:float) -> void:
self.fps_label.set_text("FPS: " + str(info))
func set_cam_pos_label(info:Vector2) -> void:
if info == null:
self.cam_pos_label.set_text("Cam pos: unknown")
return
self.cam_pos_label.set_text("Cam pos: " + str(info))
func set_cam_rotation(info) -> void:
if info == null:
self.cam_rotation_label.set_text("Cam rot: unknown")
return
self.cam_rotation_label.set_text("Cam rot: " + str(info))
func set_cam_zoom_label(info:float) -> void:
if info == null:
self.cam_zoom_label.set_text("Zoom : unknown")
return
self.cam_zoom_label.set_text("Zoom :" + str(info))
func set_chunk_label(info:int) -> void:
if info == null:
self.chunk_label.set_text("Chunks: unknown")
return
self.chunk_label.set_text("Chunks: " + str(info))
func set_chunk_del_label(info:int) -> void:
if info == null:
self.chunk_del_label.set_text("Chunk del: unknown")
return
self.chunk_del_label.set_text("Chunk del: " + str(info))

View file

@ -94,7 +94,6 @@ func set_minimap() -> void:
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

View file

@ -1,15 +1,15 @@
class_name UIControl
extends Control
# var view = get_node("../View")
signal construction_button_pressed(button_name, button_type)
signal infolayer_button_pressed(button_type)
@onready var debug_info = get_node("DebugContainer/" + Globals.DEBUGINFO_NODE)
@onready var minimap:Minimap
#@onready var node_minimap:Minimap
@onready var node_debuginfo:DebugInfo
var amount_of_chunks:int = 0
var size_of_chunk_removal_queue:int = 0
var update_debug_info:bool = false
# name, position
@ -30,16 +30,13 @@ func _on_chunk_handler_chunk_stats(chunks, removal_queue):
# Called when the node enters the scene tree for the first time.
func _ready():
func set_ready():
create_buttons()
minimap = Minimap.new()
##node_minimap = Minimap.new()
node_debuginfo = find_child("DebugInfo")
node_debuginfo.set_ready()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
update_debug_info_func()
# sends signals which View catches and places selected type of buildings
func _on_button_residental_pressed():
emit_signal("construction_button_pressed", Globals.TYPE_RESIDENTIAL, 0)
@ -75,7 +72,6 @@ func _on_button_infolayer_parcels_pressed():
func _on_main_worldgen_ready():
self.set_process(true)
update_debug_info = true
# defines construction toolbar buttons
@ -94,21 +90,5 @@ func create_buttons():
node_path.set_text(values[1])
node_path.show()
func update_debug_info_func():
debug_info.set_text(
"FPS " + str(Engine.get_frames_per_second()) + "\n" +
"Camera pos: " + str(Globals.CAMERA_POSITION) + "\n" +
"Chunks: " + str(self.amount_of_chunks) + "\n" +
"Chunk del: " + str(self.size_of_chunk_removal_queue)
)
# debug_info.set_text(
# #str(get_viewport().get_mouse_position()) +"\n" +
# "FPS " + str(Engine.get_frames_per_second()) + "\n" +
# "Zoom lvl: " + str(Globals.CAMERA_ZOOM_LEVEL) + "\n" +
# "Camera pos: " + str(Globals.CAMERA_POSITION) + "\n" +
# "Camera pos: " + str(Globals.camera_marker.position) + "\n" +
# "Chunks: " + str(self.amount_of_chunks) + "\n" +
# "Chunk del: " + str(self.size_of_chunk_removal_queue),
# )

View file

@ -2,13 +2,25 @@ class_name UILayer
extends CanvasLayer
@onready var node_minimap:Minimap
@onready var node_ui_control:UIControl
@onready var node_camera_marker:CameraMarker
func _ready() -> void:
node_minimap = find_child("Minimap")
node_camera_marker = find_child("CameraMarker")
node_ui_control = find_child("UIControl")
func set_ready() -> void:
node_minimap.set_ready()
node_ui_control.set_ready()
func camera_rotation_changed(new_rotation):
node_camera_marker.set_camera_marker_rotation(new_rotation)
func camera_zoom_changed(new_zoom_factor):
node_camera_marker.set_camera_marker_zoom(new_zoom_factor)