143 lines
3.5 KiB
GDScript
143 lines
3.5 KiB
GDScript
extends TileMap
|
|
|
|
var GridSize = 33
|
|
var Dic = {}
|
|
var Building_Mode := false
|
|
var Drag_Mode := false
|
|
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():
|
|
Triggerer.listen("select_building", select_building)
|
|
Triggerer.listen("drag_building", drag_building)
|
|
|
|
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):
|
|
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:
|
|
if !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():
|
|
if Building_Mode:
|
|
place_building()
|
|
else:
|
|
show_info_building()
|
|
|
|
|
|
func show_info_building():
|
|
var tile = local_to_map(get_global_mouse_position())
|
|
if Dic.has(str(tile)) and Dic[str(tile)]["Placed"]:
|
|
Triggerer.trigger("show_info", Dic[str(tile)])
|
|
|
|
func select_building(data):
|
|
Building_Mode = true
|
|
building_key = data.key
|
|
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
|
|
building_key = data.key
|
|
|
|
|
|
func release_mouse():
|
|
if Drag_Mode and Building_Mode:
|
|
place_building()
|
|
Building_Mode = false
|
|
|
|
|
|
func place_building():
|
|
var tile = local_to_map(get_global_mouse_position())
|
|
if Dic.has(str(tile)):
|
|
Building_Mode = false
|
|
Dic[str(tile)]["Key"] = building_key
|
|
Dic[str(tile)]["Placed"] = true
|
|
|
|
Drag_Mode = false
|
|
|
|
|
|
func _on_mouse_handler_rclicked():
|
|
Building_Mode = false
|
|
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))
|