diff --git a/scenes/comet_spawner.tscn b/scenes/comet_spawner.tscn new file mode 100644 index 0000000..4e8e413 --- /dev/null +++ b/scenes/comet_spawner.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://6c6g7e5v3ifr"] + +[ext_resource type="Script" path="res://scripts/comet_spawner.gd" id="1_ngrtm"] + +[node name="CometSpawner" type="Node2D"] +script = ExtResource("1_ngrtm") + +[node name="Comets" type="Node" parent="."] diff --git a/scripts/comet_spawner.gd b/scripts/comet_spawner.gd new file mode 100644 index 0000000..6a5910e --- /dev/null +++ b/scripts/comet_spawner.gd @@ -0,0 +1,38 @@ +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(0, random_position) + +func _process(_delta: float) -> void: + viewport_size = get_viewport_rect().size + if $Comets.get_child_count() <= 0: + screen_cleared.emit() + + +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) + +