Files
rgb/scripts/maze.gd
2024-02-24 08:36:06 -06:00

63 lines
2.2 KiB
GDScript

extends Node2D
class_name Maze
const PLAYER = preload("res://scene/player.tscn")
const ENEMY = preload("res://scene/enemy.tscn")
const ROCKET = preload("res://scene/rocket.tscn")
const MAIN_MENU = "res://scene/main_menu.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:
HasWon.inital_load = false
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()
var alliegences: Array[Buff.colors] = []
for spawn in spawn_locations:
var entity: Entity
if spawn == player_loc:
entity = PLAYER.instantiate()
entity.alliance = get_random_alliance(alliegences)
hud.set_alliance(entity.alliance)
entity.color_changed.connect(hud._on_player_color_changed)
entity.lives_changed.connect(hud._on_player_lives_changed)
else:
entity = ENEMY.instantiate()
entity.alliance = get_random_alliance(alliegences)
entity.position = spawn
entity.death.connect(_on_entity_death)
entity_container.add_child(entity)
func get_random_alliance(alliegences: Array[Buff.colors]) -> Buff.colors:
if alliegences.size() == 0:
alliegences.append_array([Buff.colors.RED, Buff.colors.GREEN, Buff.colors.BLUE])
return alliegences.pop_at(randi_range(0, alliegences.size() - 1))
func _on_entity_death(entity: Entity):
var entities = entity_container.get_child_count() - 1
hud.update_entity_count(entities)
entity.queue_free()
func _on_rocket_timer_timeout() -> void:
var rocket_location: Node2D = spawn_zones.get_children().pick_random().get_child(0)
var rocket = ROCKET.instantiate()
rocket.global_position = rocket_location.global_position
rocket.game_over.connect(_on_game_over)
add_child(rocket)
hud.rocket_placed()
func _on_game_over() -> void:
get_tree().change_scene_to_file(MAIN_MENU)