brackeys-12/tile_map.gd

144 lines
3.5 KiB
GDScript3
Raw Normal View History

extends TileMap
var GridSize = 33
var Dic = {}
2024-09-14 00:09:38 +00:00
var Building_Mode := false
var Drag_Mode := false
2024-09-14 01:02:26 +00:00
var building_key
func generate_percentage(percentages: Array):
var array_length = 0
for percentage in percentages.size():
if percentages[percentage] < 0:
percentages[percentage] = 0
array_length += percentages[percentage]
var arr: Array
arr.resize(array_length)
var sum = 0
for percentage in percentages.size():
for num in percentages[percentage]:
arr[num + sum] = percentage
sum += percentages[percentage]
return arr[randi()%array_length] + 1
func abs(num: int):
if num < 0:
num = -num
return num
func _ready():
2024-09-14 00:09:38 +00:00
Triggerer.listen("select_building", select_building)
Triggerer.listen("drag_building", drag_building)
2024-09-14 01:16:28 +00:00
#for x in GridSize:
#for y in GridSize:
#var layer_number: int
#var x_value: int = abs(GridSize/2 - x)
#var y_value: int = abs(GridSize/2 - y)
#var max_value: int = maxi(x_value, y_value)
#match max_value:
#16,15:
#layer_number = 1
#14:
#layer_number = generate_percentage([3,1])
#13:
#layer_number = generate_percentage([2,1])
#12:
#layer_number = generate_percentage([1,1])
#11:
#layer_number = generate_percentage([1,2,1])
#10:
#layer_number = generate_percentage([0,3,2])
#9:
#layer_number = generate_percentage([0,1,1])
#8:
#layer_number = generate_percentage([0,1,2,1])
#7:
#layer_number = generate_percentage([0,0,3,2])
#6:
#layer_number = generate_percentage([0,0,1,1])
#5:
#layer_number = generate_percentage([0,0,1,2,1])
#4:
#layer_number = generate_percentage([0,0,0,3,2])
#3:
#layer_number = generate_percentage([0,0,0,1,1])
#2:
#layer_number = generate_percentage([0,0,0,1,2])
#1,0:
#layer_number = 5
#print("x- ",x," y- ",y," layer- ",layer_number)
#for layer in layer_number:
#set_cell(layer, Vector2(x, y), 2, Vector2i(1, 1), 0)
#Dic[str(Vector2(x,y))] = {
#"Layer": layer_number,
#"Placed": false
#}
func _process(delta):
2024-09-14 00:09:38 +00:00
if Input.is_action_just_released("lclick"):
release_mouse()
if Building_Mode:
var tile = local_to_map(get_global_mouse_position())
for x in GridSize:
for y in GridSize:
2024-09-14 01:24:54 +00:00
if Dic.has(str(Vector2(x,y))) and !Dic[str(Vector2(x,y))].get("Placed"):
erase_cell(Dic[str(Vector2(x,y))].get("Layer"), Vector2(x,y))
if Dic.has(str(tile)):
set_cell(Dic[str(tile)].get("Layer"), tile, 1, Vector2i(0,0), 0)
func _on_mouse_handler_clicked():
2024-09-09 18:19:38 +00:00
if Building_Mode:
2024-09-14 00:09:38 +00:00
place_building()
2024-09-14 00:20:51 +00:00
else:
show_info_building()
2024-09-14 00:09:38 +00:00
2024-09-14 00:20:51 +00:00
func show_info_building():
var tile = local_to_map(get_global_mouse_position())
2024-09-14 00:32:06 +00:00
if Dic.has(str(tile)) and Dic[str(tile)]["Placed"]:
2024-09-14 00:20:51 +00:00
Triggerer.trigger("show_info", Dic[str(tile)])
2024-09-14 00:09:38 +00:00
func select_building(data):
Building_Mode = true
2024-09-14 01:02:26 +00:00
building_key = data.key
2024-09-14 00:09:38 +00:00
for x in GridSize:
for y in GridSize:
if !Dic[str(Vector2(x,y))].get("Placed"):
erase_cell(Dic[str(Vector2(x,y))].get("Layer"), Vector2(x,y))
func drag_building(data):
Building_Mode = true
Drag_Mode = true
2024-09-14 01:02:26 +00:00
building_key = data.key
2024-09-14 00:09:38 +00:00
func release_mouse():
if Drag_Mode and Building_Mode:
place_building()
2024-09-14 01:02:26 +00:00
Building_Mode = false
2024-09-14 00:09:38 +00:00
func place_building():
var tile = local_to_map(get_global_mouse_position())
2024-09-14 00:32:06 +00:00
if Dic.has(str(tile)):
Building_Mode = false
2024-09-14 01:02:26 +00:00
Dic[str(tile)]["Key"] = building_key
2024-09-14 00:32:06 +00:00
Dic[str(tile)]["Placed"] = true
Drag_Mode = false
func _on_mouse_handler_rclicked():
2024-09-14 00:09:38 +00:00
Building_Mode = false
2024-09-09 18:19:38 +00:00
for x in GridSize:
for y in GridSize:
if !Dic[str(Vector2(x,y))].get("Placed"):
erase_cell(Dic[str(Vector2(x,y))].get("Layer"), Vector2(x,y))