From 560f66b32dc174d6a4297c534c184496a106e93c Mon Sep 17 00:00:00 2001 From: Melon Bread Date: Thu, 16 May 2024 14:38:23 -0400 Subject: [PATCH] Created an invader spawner that gives points when shot --- scenes/game.tscn | 6 +++++- scenes/invader_spawner.tscn | 14 ++++++++++++++ scripts/game.gd | 6 ++++-- scripts/invader_spawner.gd | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 scenes/invader_spawner.tscn create mode 100644 scripts/invader_spawner.gd diff --git a/scenes/game.tscn b/scenes/game.tscn index 3be544e..314c6c4 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=6 format=3 uid="uid://bdarunpk3c2eh"] +[gd_scene load_steps=7 format=3 uid="uid://bdarunpk3c2eh"] [ext_resource type="Script" path="res://scripts/game.gd" id="1_6727i"] [ext_resource type="PackedScene" uid="uid://cbo47ftx0vcro" path="res://scenes/ship.tscn" id="1_esyqo"] [ext_resource type="FontFile" uid="uid://bkcxbtx1vcj8w" path="res://assets/fonts/Color Basic.otf" id="2_gwtm0"] [ext_resource type="PackedScene" uid="uid://0yktrbyp6cr3" path="res://scenes/game_over_screen.tscn" id="3_yy075"] [ext_resource type="PackedScene" uid="uid://cemhlom425afn" path="res://scenes/stars.tscn" id="4_064lo"] +[ext_resource type="PackedScene" uid="uid://e647ck5fxo17" path="res://scenes/invader_spawner.tscn" id="5_itvtr"] [node name="Game" type="Node2D"] script = ExtResource("1_6727i") @@ -58,8 +59,11 @@ color = Color(0, 0, 0, 1) [node name="Stars" parent="Background" instance=ExtResource("4_064lo")] +[node name="InvaderSpawner" parent="." instance=ExtResource("5_itvtr")] + [node name="Ship" parent="." instance=ExtResource("1_esyqo")] z_index = 1 position = Vector2(622, 309) +[connection signal="shot" from="InvaderSpawner" to="." method="_on_invader_spawner_shot"] [connection signal="crashed" from="Ship" to="." method="game_over"] diff --git a/scenes/invader_spawner.tscn b/scenes/invader_spawner.tscn new file mode 100644 index 0000000..fa4e2bb --- /dev/null +++ b/scenes/invader_spawner.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=2 format=3 uid="uid://e647ck5fxo17"] + +[ext_resource type="Script" path="res://scripts/invader_spawner.gd" id="1_y3j7g"] + +[node name="InvaderSpawner" type="Node2D"] +script = ExtResource("1_y3j7g") + +[node name="SpawnTimer" type="Timer" parent="."] +wait_time = 30.0 +autostart = true + +[node name="Invaders" type="Node" parent="."] + +[connection signal="timeout" from="SpawnTimer" to="." method="_on_spawn_timer_timeout"] diff --git a/scripts/game.gd b/scripts/game.gd index acb3427..01609d6 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -38,6 +38,10 @@ func game_over() -> void: if score > high_score: save_score() +func _on_invader_spawner_shot() -> void: + # This is not the most modular way to do this. + score += 500 + func increase_score(comet_size : int) -> void: # We add one since comet_size starts count at 0 comet_size += 1 @@ -59,5 +63,3 @@ func load_score() -> int: return file.get_var(score) else: return 0 - - diff --git a/scripts/invader_spawner.gd b/scripts/invader_spawner.gd new file mode 100644 index 0000000..907b129 --- /dev/null +++ b/scripts/invader_spawner.gd @@ -0,0 +1,26 @@ +extends Node2D + +signal shot + +const INVADER = preload("res://scenes/invader.tscn") + +var can_spawn = false + +func _process(delta: float) -> void: + if can_spawn: + spawn_invader() + $SpawnTimer.wait_time = randi_range(20, 40) + + +func _on_spawn_timer_timeout() -> void: + can_spawn = true + +func spawn_invader() -> void: + var invader := INVADER.instantiate() + invader.shot.connect(player_shot) + $Invaders.add_child(invader) + can_spawn = false + $SpawnTimer.start() + +func player_shot() -> void: + shot.emit()