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
|
extends Node2D
export(NodePath) var TILEMAP_NP: NodePath
export(PackedScene) var WALKER_UNIT_NP: PackedScene
export(int, 5, 100, 1) var starting_unit_count: int = 5
export(int, 5, 10, 1) var initial_safe_zone_size: int = 5
onready var tilemap: TileMap = get_node(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 _place_safe_zone() -> void:
for i in initial_safe_zone_size:
for j in initial_safe_zone_size:
tilemap.set_cell(i, j, Global.WORLD_TILE_PATH)
tilemap.set_cell(-i, -j, Global.WORLD_TILE_PATH)
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 rect: Rect2 = 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 poses: Vector2= Vector2(rect.position.x + x, rect.position.y + y)
if tilemap.get_cell(int(poses.x), int(poses.y)) == TileMap.INVALID_CELL:
tilemap.set_cellv(poses, Global.WORLD_TILE_WALL)
func _on_world_gen_walker_started(id: int) -> void:
print("Walker unit %s started." % id)
func _on_world_gen_walker_finished(id: int) -> void:
print("Walker unit %s finished." % id)
func _on_world_gen_walker_died(id: int) -> void:
print("Walker unit %s died." % id)
func _on_world_gen_spawn_walker_unit(location: Vector2) -> void:
print("Spawning new walking unit.")
_spawn_walker_unit(location)
|