separate func for tile placement error checking
This commit is contained in:
parent
c440e5f482
commit
9f2dbb2a1d
8 changed files with 87 additions and 70 deletions
|
@ -1,4 +1,4 @@
|
||||||
extends Node2D
|
extends TileMap
|
||||||
|
|
||||||
func _process(delta):
|
func _process(_delta):
|
||||||
position = get_viewport().get_mouse_position()
|
position = get_viewport().get_mouse_position()
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
[gd_scene load_steps=3 format=3 uid="uid://sqvd1ev6ubp"]
|
[gd_scene load_steps=2 format=3 uid="uid://sqvd1ev6ubp"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://scenes/Building.gd" id="1_uqlnt"]
|
[ext_resource type="Script" path="res://scenes/Building.gd" id="1_uqlnt"]
|
||||||
[ext_resource type="Texture2D" uid="uid://dabmo11wkiwm3" path="res://art/buildings/building_grid.png" id="1_yd1tr"]
|
|
||||||
|
|
||||||
[node name="Building" type="Sprite2D"]
|
[node name="Building" type="Sprite2D"]
|
||||||
texture = ExtResource("1_yd1tr")
|
|
||||||
script = ExtResource("1_uqlnt")
|
script = ExtResource("1_uqlnt")
|
||||||
|
|
|
@ -549,10 +549,11 @@ offset_bottom = 8.0
|
||||||
|
|
||||||
[node name="DebugInfo" type="Label" parent="UILayer/Control"]
|
[node name="DebugInfo" type="Label" parent="UILayer/Control"]
|
||||||
layout_mode = 0
|
layout_mode = 0
|
||||||
offset_left = 568.0
|
offset_left = 648.0
|
||||||
offset_top = 24.0
|
offset_top = 16.0
|
||||||
offset_right = 928.0
|
offset_right = 936.0
|
||||||
offset_bottom = 280.0
|
offset_bottom = 256.0
|
||||||
|
horizontal_alignment = 2
|
||||||
|
|
||||||
[connection signal="set_camera_position" from="World" to="World/CameraZoom2D" method="_on_world_set_camera_position"]
|
[connection signal="set_camera_position" from="World" to="World/CameraZoom2D" method="_on_world_set_camera_position"]
|
||||||
[connection signal="button_pressed" from="UILayer/Control" to="World" method="_on_control_button_pressed"]
|
[connection signal="button_pressed" from="UILayer/Control" to="World" method="_on_control_button_pressed"]
|
||||||
|
|
|
@ -25,7 +25,6 @@ func _set_camera_zoom_level(value: float) -> void:
|
||||||
)
|
)
|
||||||
|
|
||||||
func _on_world_set_camera_position(pos: Vector2) -> void:
|
func _on_world_set_camera_position(pos: Vector2) -> void:
|
||||||
print("Setting camera pos:", pos)
|
|
||||||
self.position = pos
|
self.position = pos
|
||||||
|
|
||||||
func _unhandled_input(event):
|
func _unhandled_input(event):
|
||||||
|
|
|
@ -22,7 +22,7 @@ func _ready():
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(_delta):
|
func _process(_delta):
|
||||||
debug_info.set_text(str(get_viewport().get_mouse_position()))
|
debug_info.set_text(str(get_viewport().get_mouse_position()) +"\n" + "FPS " + str(Engine.get_frames_per_second()))
|
||||||
|
|
||||||
# defines construction toolbar buttons
|
# defines construction toolbar buttons
|
||||||
func create_buttons():
|
func create_buttons():
|
||||||
|
@ -31,7 +31,8 @@ func create_buttons():
|
||||||
var node_path = get_node(Globals.CONSTRUCTION_PANEL_NODE + "/" + str(button))
|
var node_path = get_node(Globals.CONSTRUCTION_PANEL_NODE + "/" + str(button))
|
||||||
|
|
||||||
if(!node_path):
|
if(!node_path):
|
||||||
push_error("Error: Button '%s' not found when trying to set it's properties in Control.gd!", button)
|
var errmsg = Globals.ERROR_BUTTON_NOT_FOUND % button
|
||||||
|
push_error(errmsg)
|
||||||
|
|
||||||
node_path.set_size(Globals.GUI_BUILD_BUTTON_SIZE)
|
node_path.set_size(Globals.GUI_BUILD_BUTTON_SIZE)
|
||||||
node_path.set_position(values[0])
|
node_path.set_position(values[0])
|
||||||
|
|
|
@ -61,7 +61,15 @@ const TILE_SIZE_X:int = 16
|
||||||
const TILE_SIZE_Y:int = 16
|
const TILE_SIZE_Y:int = 16
|
||||||
|
|
||||||
# error messages
|
# error messages
|
||||||
|
const ERROR_BUILDING_TYPE_NOT_SET:String = "Building type not set, while trying to place building."
|
||||||
|
const ERROR_BUTTON_NOT_FOUND:String = "Button '%s' not found when trying to set it's properties in Control.gd!"
|
||||||
|
const ERROR_FAILED_TO_LOAD_FILE:String = "Failed to load image with filename: '%s'"
|
||||||
|
const ERROR_TILE_X_COORDS_OUT_OF_BOUNDS:String = "Trying to build outside the game area: '%s'. Cell should be between '%s-%s'"
|
||||||
|
const ERROR_TILE_Y_COORDS_OUT_OF_BOUNDS:String = "Trying to build outside the game area: y is: '%s'. Cell should be between '%s-%s'"
|
||||||
const ERROR_IMAGE_WIDTH_INCORRECT:String = "Provided map image width '%s' too small or too large. Width should be between: '%s-%s'"
|
const ERROR_IMAGE_WIDTH_INCORRECT:String = "Provided map image width '%s' too small or too large. Width should be between: '%s-%s'"
|
||||||
const ERROR_IMAGE_HEIGHT_INCORRECT:String = "Provided map image height '%s' too small or too large. Height should be between: '%s-%s'"
|
const ERROR_IMAGE_HEIGHT_INCORRECT:String = "Provided map image height '%s' too small or too large. Height should be between: '%s-%s'"
|
||||||
const ERROR_FAILED_TO_LOAD_FILE:String = "Failed to load image with filename: '%s'"
|
const ERROR_MAKING_WORLD_INSTANCE:String = "Error while making an instance of World node."
|
||||||
|
const ERROR_WHILE_GENERATING_MAP:String = "Error in generating the map. Game won't start."
|
||||||
|
const ERROR_WORLD_TILEMAP_NODE_MISSING:String = "World TileMap node missing or name is wrong. Tried to load: '%s'"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,14 +20,13 @@ func _init():
|
||||||
func _ready():
|
func _ready():
|
||||||
Globals.world_map = get_node("World")
|
Globals.world_map = get_node("World")
|
||||||
if !Globals.world_map:
|
if !Globals.world_map:
|
||||||
push_error("Error while making an instance of World node.")
|
push_error(Globals.ERROR_MAKING_WORLD_INSTANCE)
|
||||||
quit_game()
|
quit_game()
|
||||||
|
|
||||||
# generate terrain. quit game if generation fails.
|
# generate terrain. quit game if generation fails.
|
||||||
if !Globals.world_map.generate_terrain(map_file_name):
|
if !Globals.world_map.generate_terrain(map_file_name):
|
||||||
push_error("Error in generating the map. Game won't start.")
|
push_error(Globals.ERROR_WHILE_GENERATING_MAP)
|
||||||
quit_game()
|
quit_game()
|
||||||
#SceneTree.quit
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(_delta):
|
func _process(_delta):
|
||||||
|
@ -35,5 +34,6 @@ func _process(_delta):
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
|
116
scripts/World.gd
116
scripts/World.gd
|
@ -2,9 +2,10 @@ extends TileMap
|
||||||
|
|
||||||
signal set_camera_position(pos:Vector2)
|
signal set_camera_position(pos:Vector2)
|
||||||
|
|
||||||
var building : bool = false
|
var has_placeable_building: bool = false
|
||||||
|
var building
|
||||||
var building_type: String
|
var building_type: String
|
||||||
#var scene
|
var scene
|
||||||
|
|
||||||
# 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():
|
||||||
|
@ -14,38 +15,14 @@ func _ready():
|
||||||
func _process(_delta):
|
func _process(_delta):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
func get_building_properties() -> Array:
|
||||||
func _on_control_button_pressed(type):
|
|
||||||
# create new building, in Building node it is attached to mouse cursor
|
|
||||||
#scene = load(Globals.SCENE_PATH + "Building.tscn")
|
|
||||||
#building = scene.instantiate()
|
|
||||||
#add_child(building)
|
|
||||||
self.building = true
|
|
||||||
self.building_type = type
|
|
||||||
|
|
||||||
#print(button_name + " button pressed in Control! :-0")
|
|
||||||
pass
|
|
||||||
|
|
||||||
func _input(event):
|
|
||||||
# place the building
|
|
||||||
if event.is_action_pressed("place_building") and building != false:
|
|
||||||
# stop processing the sprite (fix it in place). no idea if it is a good idea yet..
|
|
||||||
#building.set_process(false)
|
|
||||||
building = false
|
|
||||||
place_building_to_map(event)
|
|
||||||
|
|
||||||
# cancel placement
|
|
||||||
if event.is_action_pressed("cancel"):
|
|
||||||
if building != false:
|
|
||||||
pass
|
|
||||||
|
|
||||||
func calculate_grid_coordinates(map_position: Vector2) -> Vector2:
|
|
||||||
return (map_position).floor()
|
|
||||||
|
|
||||||
func place_building_to_map(event):
|
|
||||||
var tileset_id = 0 # default value
|
var tileset_id = 0 # default value
|
||||||
var tilemap_tile_coords: Vector2i
|
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
|
# layer | position coords | tileset id | coords of the tile at tilemap | alternative tile
|
||||||
match building_type:
|
match building_type:
|
||||||
Globals.TYPE_RESIDENTIAL:
|
Globals.TYPE_RESIDENTIAL:
|
||||||
|
@ -73,11 +50,52 @@ func place_building_to_map(event):
|
||||||
tilemap_tile_coords = Vector2i(16,16)
|
tilemap_tile_coords = Vector2i(16,16)
|
||||||
tileset_id = 1
|
tileset_id = 1
|
||||||
|
|
||||||
var mouse_pos = local_to_map(get_global_mouse_position())
|
return [tileset_id, tilemap_tile_coords]
|
||||||
set_cell(Globals.LAYER_BUILDINGS, mouse_pos, tileset_id, tilemap_tile_coords, 0)
|
|
||||||
#print("cell set at layer ", Globals.LAYER_BUILDINGS)
|
func _on_control_button_pressed(type):
|
||||||
#print("cellpos:", local_to_map(get_global_mouse_position()))
|
self.building_type = type
|
||||||
#print("event position:", event.position)
|
|
||||||
|
# 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 are_coords_valid(value:int, errmsg:String) -> bool:
|
||||||
|
if Globals.MAP_MIN_HEIGHT > value or value > Globals.MAP_MAX_HEIGHT:
|
||||||
|
errmsg = errmsg % [value, Globals.MAP_MIN_HEIGHT, Globals.MAP_MAX_HEIGHT]
|
||||||
|
push_error(errmsg)
|
||||||
|
return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
func place_building_to_map():
|
||||||
|
var building_properties = get_building_properties()
|
||||||
|
var tile_on_mouse = local_to_map(get_global_mouse_position())
|
||||||
|
|
||||||
|
if !are_coords_valid(tile_on_mouse.y,Globals.ERROR_TILE_Y_COORDS_OUT_OF_BOUNDS):
|
||||||
|
return false
|
||||||
|
elif !are_coords_valid(tile_on_mouse.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)
|
||||||
|
|
||||||
func generate_terrain(filename) -> bool:
|
func generate_terrain(filename) -> bool:
|
||||||
# Try to load the image which we used to place water & ground to world map
|
# Try to load the image which we used to place water & ground to world map
|
||||||
|
@ -87,31 +105,23 @@ func generate_terrain(filename) -> bool:
|
||||||
var errmsg = Globals.ERROR_FAILED_TO_LOAD_FILE
|
var errmsg = Globals.ERROR_FAILED_TO_LOAD_FILE
|
||||||
push_error(errmsg % filename)
|
push_error(errmsg % filename)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
var image_height:int = image.get_height()
|
|
||||||
var image_width:int = image.get_width()
|
|
||||||
|
|
||||||
# Check if image is too small or too large
|
# Check if image is too small or too large
|
||||||
if Globals.MAP_MIN_HEIGHT > image_height or image_height > Globals.MAP_MAX_HEIGHT:
|
var image_size:Vector2i = image.get_size()
|
||||||
var errmsg = Globals.ERROR_IMAGE_HEIGHT_INCORRECT
|
if !are_coords_valid(image_size.y,Globals.ERROR_IMAGE_HEIGHT_INCORRECT):
|
||||||
errmsg = errmsg % [image_height, Globals.MAP_MIN_HEIGHT, Globals.MAP_MAX_HEIGHT]
|
|
||||||
push_error(errmsg)
|
|
||||||
return false
|
return false
|
||||||
elif Globals.MAP_MIN_WIDTH > image_width or image_width > Globals.MAP_MAX_WIDTH:
|
elif !are_coords_valid(image_size.x,Globals.ERROR_IMAGE_WIDTH_INCORRECT):
|
||||||
var errmsg = Globals.ERROR_IMAGE_WIDTH_INCORRECT
|
|
||||||
errmsg = errmsg % [image_width, Globals.MAP_MIN_WIDTH, Globals.MAP_MAX_WIDTH]
|
|
||||||
push_error(errmsg)
|
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# Try to load the world tilemap where we place the tiles
|
# Try to load the world tilemap where we place the tiles
|
||||||
if (Globals.world_map == null):
|
if (Globals.world_map == null):
|
||||||
print("World TileMap node missing or name is wrong.\n
|
var errmsg = Globals.ERROR_WORLD_TILEMAP_NODE_MISSING % Globals.WORLD_NODE
|
||||||
Tried to load: '%s'", Globals.WORLD_NODE)
|
push_error(errmsg)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# Finally populate the world map with hopefully valid tiles!
|
# Finally populate the world map with hopefully valid tiles!
|
||||||
for x in image_width:
|
for x in image_size.x:
|
||||||
for y in image_height:
|
for y in image_size.y:
|
||||||
# layer | position coords | tilemap id | coords of the tile at tilemap | alternative tile
|
# layer | position coords | tilemap id | coords of the tile at tilemap | alternative tile
|
||||||
if image.get_pixel(x, y) == Globals.WATER_TILE_COLOR_IN_MAP_FILE:
|
if image.get_pixel(x, y) == Globals.WATER_TILE_COLOR_IN_MAP_FILE:
|
||||||
Globals.world_map.set_cell(Globals.LAYER_TERRAIN, Vector2i(x, y), 2, Vector2i(0,0), 0)
|
Globals.world_map.set_cell(Globals.LAYER_TERRAIN, Vector2i(x, y), 2, Vector2i(0,0), 0)
|
||||||
|
@ -119,6 +129,6 @@ func generate_terrain(filename) -> bool:
|
||||||
# center camera to world map
|
# center camera to world map
|
||||||
emit_signal(
|
emit_signal(
|
||||||
"set_camera_position",
|
"set_camera_position",
|
||||||
Vector2(image_height/2.0*Globals.TILE_SIZE_X, image_width/2.0*Globals.TILE_SIZE_Y)
|
Vector2(image_size.x/2.0*Globals.TILE_SIZE_X, image_size.y/2.0*Globals.TILE_SIZE_Y)
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue