brackeys-12/BuildingLevel.gd

133 lines
3.4 KiB
GDScript3
Raw Normal View History

2024-09-14 22:01:46 +00:00
extends TileMapLayer
@export var building_spots: Array[Vector2i]
2024-09-15 02:20:30 +00:00
@export var tree_spots: Array[Vector2i]
@export var rock_spots: Array[Vector2i]
2024-09-15 04:46:49 +00:00
@export var th_spots: Array[Vector2i]
2024-09-14 22:01:46 +00:00
var building_data = {}
2024-09-15 02:20:30 +00:00
var last_tile
var last_text
2024-09-15 03:33:24 +00:00
var rand = 0
2024-09-14 22:01:46 +00:00
2024-09-15 02:20:30 +00:00
const NAME_ICON = preload("res://NameIcon.tscn")
2024-09-14 22:01:46 +00:00
2024-09-15 03:33:24 +00:00
var building_to_tile_map = {
"tents": {
"source": 8,
"atlas": 0
},
"quarter": {
"source": 8,
"atlas": 1
},
"house": {
"source": 8,
"atlas": 2
},
"essencecompressor": {
2024-09-15 03:58:33 +00:00
"source": 9,
2024-09-15 03:33:24 +00:00
"atlas": 2
},
"woodcutter": {
2024-09-15 03:58:33 +00:00
"source": 9,
"atlas": 1
2024-09-15 03:33:24 +00:00
},
"stoneminer": {
2024-09-15 03:58:33 +00:00
"source": 9,
"atlas": 0
2024-09-15 03:33:24 +00:00
},
"foodgathering": {
"source": 7,
"atlas": 2
},
"foodprocessing": {
"source": 7,
"atlas": 3
},
"waterprocessor": {
"source": 7,
"atlas": 1
},
"waterpump": {
"source": 7,
"atlas": 0
}
}
2024-09-14 22:01:46 +00:00
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:
2024-09-15 04:46:49 +00:00
if not building_data.has(spot) and not tree_spots.has(spot) and not rock_spots.has(spot) and not th_spots.has(spot):
2024-09-14 22:01:46 +00:00
erase_cell(spot)
2024-09-15 02:20:30 +00:00
if last_tile != tile:
if last_text and is_instance_valid(last_text) and last_text.selected:
last_text._deselect()
2024-09-15 03:33:24 +00:00
rand = randi_range(0, 1)
2024-09-15 02:20:30 +00:00
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)
2024-09-15 04:46:49 +00:00
elif th_spots.has(tile):
last_text = NAME_ICON.instantiate()
last_text.text = "[center]Town Hall"
last_text.position = map_to_local(tile) + Vector2(-25, -10)
add_sibling(last_text)
2024-09-15 02:20:30 +00:00
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
2024-09-14 22:01:46 +00:00
if not building_mode:
return
2024-09-15 04:46:49 +00:00
if building_spots.has(tile) and not rock_spots.has(tile) and not tree_spots.has(tile) and not th_spots.has(tile):
2024-09-15 03:33:24 +00:00
var build_data = building_to_tile_map[Persister.get_value("building_key")]
set_cell(tile, build_data["source"], Vector2i(build_data["atlas"],rand), 0)
2024-09-15 02:20:30 +00:00
2024-09-14 22:01:46 +00:00
func place_building():
var tile = local_to_map(get_global_mouse_position())
2024-09-15 04:46:49 +00:00
if building_spots.has(tile) and not building_data.has(tile) and not tree_spots.has(tile) and not rock_spots.has(tile) and not th_spots.has(tile):
2024-09-14 22:01:46 +00:00
Persister.persist_data("building_mode", false)
building_data[tile] = {
"key": Persister.get_value("building_key"),
}
2024-09-15 04:22:02 +00:00
2024-09-15 04:25:59 +00:00
var build_data = Data.data.buildings[Persister.get_value("building_key")]
var cost = build_data.cost
2024-09-15 04:22:02 +00:00
if cost.has("wood"):
Persister.change_value("wood", -int(cost.wood))
if cost.has("stone"):
Persister.change_value("stone", -int(cost.stone))
2024-09-15 04:25:59 +00:00
if build_data.type == "housing":
Persister.change_value("max_population", int(build_data.amount))
building_data[tile].max_population = int(build_data.amount)
2024-09-14 22:01:46 +00:00
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])