brackeys-12/BuildingLevel.gd
2024-09-14 22:20:30 -04:00

67 lines
2 KiB
GDScript

extends TileMapLayer
@export var building_spots: Array[Vector2i]
@export var tree_spots: Array[Vector2i]
@export var rock_spots: Array[Vector2i]
var building_data = {}
var last_tile
var last_text
const NAME_ICON = preload("res://NameIcon.tscn")
func _process(delta: float) -> void:
var tile = local_to_map(get_global_mouse_position())
var building_mode = Persister.get_value("building_mode")
for spot in building_spots:
if not building_data.has(spot) and not tree_spots.has(spot) and not rock_spots.has(spot):
erase_cell(spot)
if last_tile != tile:
if last_text and is_instance_valid(last_text) and last_text.selected:
last_text._deselect()
if not Persister.get_value("building_mode"):
if rock_spots.has(tile):
last_text = NAME_ICON.instantiate()
last_text.text = "[center]Rock"
last_text.position = map_to_local(tile) + Vector2(-25, -10)
add_sibling(last_text)
elif tree_spots.has(tile):
last_text = NAME_ICON.instantiate()
last_text.text = "[center]Tree"
last_text.position = map_to_local(tile) + Vector2(-25, -10)
add_sibling(last_text)
elif building_data.has(tile):
last_text = NAME_ICON.instantiate()
last_text.text = "[center]%s" % [Data.data.buildings[building_data[tile].key].name]
last_text.position = map_to_local(tile) + Vector2(-25, -10)
add_sibling(last_text)
last_tile = tile
if not building_mode:
return
if building_spots.has(tile) and not rock_spots.has(tile) and not tree_spots.has(tile):
set_cell(tile, 0, Vector2i(0,0), 0)
func place_building():
var tile = local_to_map(get_global_mouse_position())
if building_spots.has(tile) and not building_data.has(tile) and not tree_spots.has(tile) and not rock_spots.has(tile):
Persister.persist_data("building_mode", false)
building_data[tile] = {
"key": Persister.get_value("building_key"),
}
Persister.persist_data("drag_mode", false)
func show_info_building():
var tile = local_to_map(get_global_mouse_position())
if building_data.has(tile):
Triggerer.trigger("show_info", building_data[tile])