brackeys-12/tile_map.gd
NnYGames 45f9c3ba88 Added map
Added map that is randomly generated but has a pattern of placing tiles in a higher layer near the center
Also made it so placing a tower would change according to the top layer
2024-09-10 04:34:30 +03:00

100 lines
2.5 KiB
GDScript

extends TileMap
var GridSize = 33
var Dic = {}
var Building_Mode: bool = false
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():
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), 0, Vector2i(0,0), 0)
Dic[str(Vector2(x,y))] = {
"Layer": layer_number,
"Placed": false
}
func _process(delta):
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))
pass
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:
var tile = local_to_map(get_global_mouse_position())
Dic[str(tile)]["Placed"] = true
func _on_mouse_handler_rclicked():
Building_Mode = !Building_Mode
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))
pass