Comets.gd/scripts/comet_spawner.gd

41 lines
1.0 KiB
GDScript

extends Node2D
signal comet_shot (size : int)
signal screen_cleared
const COMET = preload("res://scenes/comet.tscn")
var comet_count := 1
var viewport_size := Vector2.ZERO
var random_position := Vector2.ZERO
func _ready() -> void:
viewport_size = get_viewport_rect().size
for comet in range(comet_count):
random_position.x = randf_range(viewport_size.x - 100, viewport_size.x)
random_position.y = randf_range(viewport_size.y - 100, viewport_size.y)
spawn_comet(2, random_position)
func _process(_delta: float) -> void:
viewport_size = get_viewport_rect().size
if $Comets.get_child_count() <= 0:
screen_cleared.emit()
queue_free()
func spawn_comet(size : int, pos : Vector2) -> void:
var comet := COMET.instantiate()
comet.position = pos
comet.current_size = size
comet.exploded.connect(comet_cleanup)
$Comets.add_child(comet)
func comet_cleanup(new_size : int, current_position : Vector2) -> void:
comet_shot.emit(new_size + 1)
if new_size >= 0:
for n in range(2):
spawn_comet(new_size, current_position)