move source files to separate folders

This commit is contained in:
Antti Hakkarainen 2023-02-18 10:45:19 +02:00
parent e619b9a6df
commit 99b28ecfa3
23 changed files with 34 additions and 34 deletions

View file

@ -0,0 +1,34 @@
class_name CameraMarker
extends Sprite2D
var size_multiplier:float
var w_s:Vector2
# Draws a box represnting the camera view in minimap
func _draw():
draw_rect(Rect2(-w_s.x/2, -w_s.y/2, w_s.x, w_s.y), Color.GREEN, false, -3.0)
# Rotates the box if camera is rotated
func _on_camera_zoom_2d_camera_rotation_changed(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):
w_s = DisplayServer.window_get_size(0) / size_multiplier
w_s.x /= new_zoom_factor
w_s.y /= new_zoom_factor
queue_redraw()
# Sets the initial size of the camera box, after game is loaded
func set_camera_marker() -> void:
size_multiplier = Globals.map_size / 32
w_s = DisplayServer.window_get_size(0) / size_multiplier
func set_camera_marker_position(pos:Vector2) -> void:
#print("marker pos: ", pos)
self.position = pos

107
source/uilayer/Control.gd Normal file
View file

@ -0,0 +1,107 @@
extends Control
# var view = get_node("../View")
signal button_pressed(button_name)
@onready var debug_info = get_node("DebugContainer/" + Globals.DEBUGINFO_NODE)
@onready var minimap:Minimap
var amount_of_chunks:int = 0
var size_of_chunk_removal_queue:int = 0
var update_debug_info:bool = false
# name, position
var buttons = {
"button_residental": [Vector2(0,0), "R"],
"button_commercial": [Vector2(50,0), "C"],
"button_industrial": [Vector2(100,00), "I"],
"button_roads": [Vector2(0,50), "Rd"],
"button_demolish": [Vector2(50,50), "Dm"],
"button_services": [Vector2(100,50), "Sv"],
"button_social": [Vector2(150,50), "So"],
}
func _on_chunk_handler_chunk_stats(chunks, removal_queue):
self.amount_of_chunks = chunks
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):
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)
func _on_button_commercial_pressed():
emit_signal("button_pressed", Globals.TYPE_COMMERCIAL)
func _on_button_industrial_pressed():
emit_signal("button_pressed", Globals.TYPE_INDUSTRIAL)
func _on_button_roads_pressed():
emit_signal("button_pressed", Globals.TYPE_ROADS)
func _on_button_demolish_pressed():
emit_signal("button_pressed", Globals.TYPE_DEMOLISH)
func _on_button_services_pressed():
emit_signal("button_pressed", Globals.TYPE_SERVICES)
func _on_button_social_pressed():
emit_signal("button_pressed", Globals.TYPE_SOCIAL)
func _on_main_worldgen_ready():
self.set_process(true)
update_debug_info = true
# defines construction toolbar buttons
func create_buttons():
for button in buttons:
var values = buttons[button]
var node_path = get_node(Globals.CONSTRUCTION_PANEL_NODE + "/" + str(button))
if(!node_path):
var errmsg = Globals.ERROR_BUTTON_NOT_FOUND % button
push_error(errmsg)
node_path.set_size(Globals.GUI_BUILD_BUTTON_SIZE)
node_path.set_position(values[0])
node_path.set_anchor(SIDE_TOP, anchor_top)
node_path.set_text(values[1])
node_path.show()
func update_debug_info_func():
debug_info.set_text(
"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

@ -0,0 +1,90 @@
class_name EntityPlacer
extends Control
var has_placeable_building: bool = false
var building
var building_type: String
var scene
func get_building_properties() -> Array:
var tileset_id = 0 # default value
var tilemap_tile_coords: Vector2i
if building_type == null:
push_error(Globals.ERROR_BUILDING_TYPE_NOT_SET)
return []
# layer | position coords | tileset id | coords of the tile at tilemap | alternative tile
match building_type:
Globals.TYPE_RESIDENTIAL:
tilemap_tile_coords = Vector2i(0,0)
tileset_id = 0
Globals.TYPE_COMMERCIAL:
tilemap_tile_coords = Vector2i(4,12)
tileset_id = 1
Globals.TYPE_INDUSTRIAL:
tilemap_tile_coords = Vector2i(4,20)
tileset_id = 1
Globals.TYPE_ROADS:
tilemap_tile_coords = Vector2i(14,2)
tileset_id = 1
Globals.TYPE_DEMOLISH:
tilemap_tile_coords = Vector2i(4,4)
tileset_id = 1
Globals.TYPE_SERVICES:
tilemap_tile_coords = Vector2i(4,8)
tileset_id = 1
Globals.TYPE_SOCIAL:
tilemap_tile_coords = Vector2i(4,0)
tileset_id = 1
_: #default
tilemap_tile_coords = Vector2i(16,16)
tileset_id = 1
return [tileset_id, tilemap_tile_coords]
func _on_control_button_pressed(type):
self.building_type = type
# create new building, in Building node it is attached to mouse cursor
#var building_properties = get_building_properties()
scene = load(Globals.SCENE_PATH + "Building.tscn")
building = scene.instantiate()
#building.set_cell(0, Vector2i(0,0), building_properties[0], building_properties[1], 0)
#add_child(building)
has_placeable_building = true
#func _input(event):
# # place the building
# if event.is_action_pressed("place_building") and has_placeable_building:
# has_placeable_building = false
# place_building_to_map()
#
# # cancel placement
# if event.is_action_pressed("cancel"):
# if has_placeable_building:
# pass
func calculate_grid_coordinates(map_position: Vector2) -> Vector2:
return (map_position).floor()
#func place_building_to_map():
# var building_properties = get_building_properties()
# #var tile_on_mouse = local_to_map(get_global_mouse_position())
#
# if !Globals.are_coords_valid(
# tile_on_mouse.y,
# Vector2i(0, Globals.map_image_size.y),
# Globals.ERROR_TILE_Y_COORDS_OUT_OF_BOUNDS
# ):
# return false
# elif !Globals.are_coords_valid(
# tile_on_mouse.x,
# Vector2i(0, Globals.map_image_size.x),
# Globals.ERROR_TILE_X_COORDS_OUT_OF_BOUNDS
# ):
# return false
# set_cell(Globals.LAYER_BUILDINGS, tile_on_mouse, building_properties[0], building_properties[1], 0)

111
source/uilayer/Minimap.gd Normal file
View file

@ -0,0 +1,111 @@
class_name Minimap
extends Control
signal set_camera_position(pos:Vector2)
signal set_map_background_texture(texture, scaling)
@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
# Called when the node enters the scene tree for the first time.
func _ready():
Globals.minimap_texture = ImageTexture.new()
func _draw():
#self.draw_rect(Rect2i(Vector2i(1,1), Vector2i(514,514)), Color(0,0,0), false, 2.0)
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 _on_mouse_entered():
is_mouse_inside_minimap = true
func _on_mouse_exited():
is_mouse_inside_minimap = false
func _unhandled_input(event) -> void:
if is_mouse_inside_minimap:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
node_camera_marker.set_camera_marker_position(get_local_mouse_position())
emit_signal(
"set_camera_position",
get_local_mouse_position() * position_multiplier
)
func generate_minimap() -> void:
var image = Image.new()
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:
var color:Color
match Globals.map_terrain_data[y][x]:
Globals.TILE_WATER:
color = Globals.minimap_colors.get(Globals.TILE_WATER)
Globals.TILE_TERRAIN:
color = Globals.minimap_colors.get(Globals.TILE_TERRAIN)
Globals.TILE_FOREST:
color = Globals.minimap_colors.get(Globals.TILE_FOREST)
_: #default
color = Globals.minimap_colors.get("default")
image.set_pixel(x, y, color)
Globals.minimap_texture = ImageTexture.create_from_image(image)
func set_camera_marker() -> void:
node_camera_marker = self.find_child("CameraMarker")
node_camera_marker.set_camera_marker()
func set_minimap() -> void:
self.sprite = self.find_child("MinimapSprite")
self.sprite.texture = Globals.minimap_texture
# 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)
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()

14
source/uilayer/UILayer.gd Normal file
View file

@ -0,0 +1,14 @@
class_name UILayer
extends CanvasLayer
@onready var node_minimap:Minimap
func _ready() -> void:
node_minimap = find_child("Minimap")
func set_ready() -> void:
node_minimap.set_ready()