summaryrefslogtreecommitdiff
path: root/Scenes/Maps/Base01.gd
blob: 96e5520b109675fdd39589389d84709c5b29ddb7 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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()
	)
	
	place_boxes(
		get_node("Areas/MapArea2/TileMap"),
		40,
		[],
		get_node("Areas/MapArea2/Objects").get_children()
	)
	get_node("Areas/MapArea2").transitioned_to.connect(func():
		handle_doors(
			get_node("Areas/MapArea2/Objects").get_children(),
			door_condition_enemies(Utilities.get_enemy_children(get_node("Areas/MapArea2/TileMap")))
		)
	)
	
	place_boxes(
		get_node("Areas/MapArea6/TileMap"),
		40,
		[],
		get_node("Areas/MapArea6/Objects").get_children()
	)
	get_node("Areas/MapArea6").transitioned_to.connect(func():
		handle_doors(
			get_node("Areas/MapArea6/Objects").get_children(),
			door_condition_enemies(Utilities.get_enemy_children(get_node("Areas/MapArea6/TileMap")))
		)
	)


# 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))
		# erase in 2 times around
		for n in range(1, 3):
			placeable_cells.erase(tilemap.local_to_map(
				entity.position + Vector2(0, tile_size.y * n)
			))
			placeable_cells.erase(tilemap.local_to_map(
				entity.position + Vector2(tile_size.x * n, 0)
			))
			placeable_cells.erase(tilemap.local_to_map(
				entity.position + Vector2(0, -tile_size.y * n)
			))
			placeable_cells.erase(tilemap.local_to_map(
				entity.position + Vector2(-tile_size.x * n, 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)


func handle_doors(doors: Array, condition):
	for door in doors:
		door.enable()
	
	condition.fulfilled.connect(func():
		for door in doors:
			door.disable()
	)
	
	condition.check()

func door_condition_enemies(enemies: Array):
	var condition = Utilities.Condition.new()
	condition.function = func():
		for enemy in enemies:
			enemy.tree_exited.connect(func():
				enemies.erase(enemy)
				if enemies.is_empty():
					condition.fulfilled.emit()
			)
	
		if enemies.is_empty():
			return true
		
		return false
	
	return condition