Created an invader spawner that gives points when shot

This commit is contained in:
Rain Clark 2024-05-16 14:38:23 -04:00
parent 5ad6c6d3b1
commit 560f66b32d
4 changed files with 49 additions and 3 deletions

View File

@ -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"]

View File

@ -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"]

View File

@ -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

View File

@ -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()