summaryrefslogtreecommitdiff
path: root/src/tools/world_generator/scripts/walker_head.gd
blob: 8bc5c4b8af315ff4fde5ab4ee7502265e76a43ad (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
extends Node2D

export(NodePath) var GROUND_TILEMAP_NP: NodePath
export(NodePath) var WALL_TILEMAP_NP: NodePath
export(PackedScene) var WALKER_UNIT_NP: PackedScene
export(int, 4, 100, 1) var STARTING_UNIT_COUNT: int = 6
export(int, 2, 10, 1) var INITIAL_SAFE_ZONE_SIZE: int = 2

onready var ground_tilemap: TileMap = get_node(GROUND_TILEMAP_NP)
onready var wall_tilemap: TileMap = get_node(WALL_TILEMAP_NP)

var max_x: int = 0
var max_y: int = 0
var map_margin: int = 10
var units: int = 0


func _ready() -> void:
	Event.connect("world_gen_walker_started", self, "_on_world_gen_walker_started")
	Event.connect("world_gen_walker_finished", self, "_on_world_gen_walker_finished")
	Event.connect("world_gen_walker_died", self, "_on_world_gen_walker_died")
	Event.connect("world_gen_spawn_walker_unit", self, "_on_world_gen_spawn_walker_unit")
	_place_safe_zone()
	_spawn_walker_units()
	_fill_empty_space()


func get_cells_around(size: int=INITIAL_SAFE_ZONE_SIZE) -> Array:
	var locations: Array = []
	for i in range(-size, size):
		for j in range(-size, size):
			locations.append(Vector2(i, j))

	return locations


func _place_safe_zone() -> void:
	var size: int = INITIAL_SAFE_ZONE_SIZE
	for i in range(-size, size):
		for j in range(-size, size):
			ground_tilemap.set_cell(i, j, get_random_tile())


func get_random_tile() -> int:
	return randi() % Global.GROUND_TILE_AMOUNT


func _spawn_walker_units() -> void:
	for i in STARTING_UNIT_COUNT:
		_spawn_walker_unit(Vector2.ZERO)


func _spawn_walker_unit(spawn_position: Vector2) -> void:
	var walker_unit: Node2D = WALKER_UNIT_NP.instance()
	add_child(walker_unit)
	units += 1
	walker_unit.position = spawn_position
	walker_unit.start(units)


func _fill_empty_space() -> void:
	var locations: Array = _get_empty_cells_location()
	for location in locations:
		# doesn't matter which index is set, as long as its form the wall tilemap
		wall_tilemap.set_cellv(location, 0)


func _get_empty_cells_location() -> Array:
	var locations: Array = []
	var rect: Rect2 = ground_tilemap.get_used_rect()
	var margin: int = map_margin
	for x in range(-margin, rect.size.x + margin):
		for y in range (-margin, rect.size.y + margin):
			var location: Vector2 = Vector2(rect.position.x + x, rect.position.y + y)
			if ground_tilemap.get_cell(int(location.x), int(location.y)) == TileMap.INVALID_CELL:
				locations.append(location)

	return locations


func _on_world_gen_walker_started(id: int) -> void:
	# print("Walker unit %s started." % id)
	pass


func _on_world_gen_walker_finished(id: int) -> void:
	# print("Walker unit %s finished." % id)
	pass


func _on_world_gen_walker_died(id: int) -> void:
	# print("Walker unit %s died." % id)
	pass


func _on_world_gen_spawn_walker_unit(location: Vector2) -> void:
	# print("Spawning new walking unit.")
	pass
	_spawn_walker_unit(location)