summaryrefslogtreecommitdiff
path: root/Scenes/Maps/Base01.gd
blob: adeac93841a8a7e85fee8251bcca59d7de5b6bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extends "res://Scenes/Maps/Map.gd"


func _ready():
	super._ready()
	
	place_boxes(
		get_node("Areas/MapArea/TileMap"),
		40,
		[],
		get_node("Areas/MapArea/Transitions").get_children()
	)


# set box to random position without polygon points
func place_boxes(
	tilemap: TileMap,
	number_of_boxes: int,
	entities: Array = [],
	entry_entities: Array = [],
	include_edges: bool = true
):
	var number_of_layers = 7
	var placeable_cells = []
	
	for layer_idx in range(number_of_layers):
		placeable_cells += tilemap.get_used_cells(layer_idx).filter(func(coords):
			var data: TileData = tilemap.get_cell_tile_data(layer_idx, coords)
			
			# not data == scene collection source
			if (
				placeable_cells.has(coords) and
				(data and data.get_collision_polygons_count(0) > 0 or not data)
			):
				placeable_cells.remove_at(placeable_cells.find(coords))
			
			return data and data.get_collision_polygons_count(0) == 0
		)
	
	for entity in entities:
		placeable_cells.erase(tilemap.local_to_map(entity.position))
	
	var tile_size = tilemap.tile_set.tile_size
	for entity in entry_entities:
		placeable_cells.erase(tilemap.local_to_map(entity.position))
		placeable_cells.erase(tilemap.local_to_map(
			entity.position + Vector2(0, tile_size.y)
		))
		placeable_cells.erase(tilemap.local_to_map(
			entity.position + Vector2(tile_size.x, 0)
		))
		placeable_cells.erase(tilemap.local_to_map(
			entity.position + Vector2(0, -tile_size.y)
		))
		placeable_cells.erase(tilemap.local_to_map(
			entity.position + Vector2(-tile_size.x, 0)
		))
	
	if include_edges:
		var rect := tilemap.get_used_rect()
		for x in range(rect.position.x, rect.end.x - 1):
			placeable_cells.erase(Vector2i(x, 0))
			placeable_cells.erase(Vector2i(x, rect.end.y - 1))
		for y in range(rect.position.y, rect.end.y - 1):
			placeable_cells.erase(Vector2i(0, y))
			placeable_cells.erase(Vector2i(rect.end.x - 1, y))
	
	placeable_cells.shuffle()
	var BoxScene = preload("res://Scenes/Entities/Objects/Box.tscn")
	for number in range(number_of_boxes):
		var box = BoxScene.instantiate()
		box.position = tilemap.map_to_local(placeable_cells[number])
		tilemap.add_child(box)