38 lines
1.2 KiB
GDScript
38 lines
1.2 KiB
GDScript
extends Node2D
|
|
class_name Maze
|
|
|
|
const PLAYER = preload("res://scene/player.tscn")
|
|
const ENEMY = preload("res://scene/enemy.tscn")
|
|
|
|
var spawn_locations: Array[Vector2] = []
|
|
|
|
@onready var hud: HUD = $HUD
|
|
@onready var spawn_zones: Node2D = $SpawnZones
|
|
@onready var entity_container: Node = $EntityContainer
|
|
|
|
func _ready() -> void:
|
|
var spawn_zone_array := spawn_zones.get_children()
|
|
for spawn_zone in spawn_zone_array:
|
|
var zone := spawn_zone.get_child(0) as CollisionShape2D
|
|
var top_left = zone.transform.get_origin() - Vector2(24, 24)
|
|
for x in range(zone.shape.size.x / 16):
|
|
for y in range(zone.shape.size.y / 16):
|
|
spawn_locations.append(top_left + Vector2(x * 16, y * 16))
|
|
hud.update_entity_count(spawn_locations.size())
|
|
var player_loc: Vector2 = spawn_locations.pick_random()
|
|
for spawn in spawn_locations:
|
|
var entity
|
|
if spawn == player_loc:
|
|
entity = PLAYER.instantiate()
|
|
entity.color_changed.connect(hud._on_player_color_changed)
|
|
entity.lives_changed.connect(hud._on_player_lives_changed)
|
|
else:
|
|
entity = ENEMY.instantiate()
|
|
entity.position = spawn
|
|
entity.death.connect(_on_entity_death)
|
|
entity_container.add_child(entity)
|
|
|
|
func _on_entity_death():
|
|
var entities = entity_container.get_child_count()
|
|
hud.update_entity_count(entities)
|