Browse Source

:truck: Isolate spawning logic into a dedicated scene

It would be easier to add new enemies.
DricomDragon 2 years ago
parent
commit
6e275613aa

+ 9 - 2
godot/component/entity/octo/Octo.gd

@@ -6,12 +6,19 @@ export var max_speed = 250
 
 var mob_types = ["walk", "swim", "fly"]
 
+
 func _ready():
 	$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]
 	$AnimatedSprite.play()
 
+
 func _on_VisibilityNotifier2D_screen_exited():
-	queue_free()
+	die()
+
+
+func _on_Spawner_reset():
+	die()
+
 
-func _on_start_game():
+func die():
 	queue_free()

+ 39 - 0
godot/component/entity/spawner/MobSpawner.gd

@@ -0,0 +1,39 @@
+class_name MobSpawner
+extends Path2D
+# Responsible of enemy life-cycle
+
+
+signal reset
+
+export (PackedScene) var Mob
+
+
+func spawn():
+	$MobSpawnLocation.set_offset(randi())
+	var newRotation = $MobSpawnLocation.rotation + rand_range(PI / 4, 3 * PI / 4)
+
+	var newMob = Mob.instance()
+	newMob.rotation = newRotation
+	newMob.position = $MobSpawnLocation.position
+	newMob.linear_velocity = Vector2(rand_range(newMob.min_speed, newMob.max_speed), 0).rotated(newRotation)
+
+	connect("reset", newMob, "_on_Spawner_reset")
+
+	add_child(newMob)
+
+
+func _on_MobTimer_timeout():
+	spawn()
+	$MobTimer.start()
+
+
+func _on_HUD_start_game():
+	emit_signal("reset")
+
+
+func _on_Main_game_started():
+	$MobTimer.start()
+
+
+func _on_Main_game_over():
+	$MobTimer.stop()

+ 21 - 0
godot/component/entity/spawner/MobSpawner.tscn

@@ -0,0 +1,21 @@
+[gd_scene load_steps=4 format=2]
+
+[ext_resource path="res://component/entity/spawner/MobSpawner.gd" type="Script" id=1]
+[ext_resource path="res://component/entity/octo/Octo.tscn" type="PackedScene" id=2]
+
+[sub_resource type="Curve2D" id=1]
+_data = {
+"points": PoolVector2Array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480, 0, 0, 0, 0, 0, 480, 720, 0, 0, 0, 0, 0, 720, 0, 0, 0, 0, 0, 0 )
+}
+
+[node name="MobSpawner" type="Path2D"]
+curve = SubResource( 1 )
+script = ExtResource( 1 )
+Mob = ExtResource( 2 )
+
+[node name="MobTimer" type="Timer" parent="."]
+process_mode = 0
+wait_time = 0.53
+
+[node name="MobSpawnLocation" type="PathFollow2D" parent="."]
+[connection signal="timeout" from="MobTimer" to="." method="_on_MobTimer_timeout"]

+ 9 - 3
godot/project.godot

@@ -9,10 +9,15 @@
 config_version=4
 
 _global_script_classes=[ {
+"base": "Path2D",
+"class": "MobSpawner",
+"language": "GDScript",
+"path": "res://component/entity/spawner/MobSpawner.gd"
+}, {
 "base": "RigidBody2D",
-"class": "Enemy",
+"class": "Octo",
 "language": "GDScript",
-"path": "res://component/entity/enemy/Enemy.gd"
+"path": "res://component/entity/octo/Octo.gd"
 }, {
 "base": "Area2D",
 "class": "Player",
@@ -20,7 +25,8 @@ _global_script_classes=[ {
 "path": "res://component/entity/player/Player.gd"
 } ]
 _global_script_class_icons={
-"Enemy": "",
+"MobSpawner": "",
+"Octo": "",
 "Player": ""
 }
 

+ 10 - 18
godot/run/Main.gd

@@ -1,10 +1,13 @@
 extends Node2D
 
-export (PackedScene) var Mob
+
+signal game_started
+signal game_over
 
 var score = 0
 var music_position = 0.0
 
+
 func new_game():
 	score = 0
 	$Player.spawn($StartPositon.position)
@@ -13,38 +16,27 @@ func new_game():
 	$HUD.show_message("Get ready")
 	$Music.play(music_position)
 
+
 func game_over():
 	$ScoreTimer.stop()
-	$MobTimer.stop()
 
 	$HUD.show_game_over()
 
 	music_position = $Music.get_playback_position()
 	$Music.stop()
 	$DeathSound.play()
+	emit_signal("game_over")
+
 
 func _ready():
 	randomize() # Plant seed for random number generation
 
+
 func _on_StartTimer_timeout():
-	$MobTimer.start()
 	$ScoreTimer.start()
+	emit_signal("game_started")
+
 
 func _on_ScoreTimer_timeout():
 	score += 1
 	$HUD.update_score(score)
-
-func _on_MobTimer_timeout():
-	$MobPath/MobSpawnLocation.set_offset(randi())
-
-	var newMob = Mob.instance()
-
-	var newRotation = $MobPath/MobSpawnLocation.rotation + rand_range(PI / 4, 3 * PI / 4)
-
-	newMob.rotation = newRotation
-	newMob.position = $MobPath/MobSpawnLocation.position
-	newMob.linear_velocity = Vector2(rand_range(newMob.min_speed, newMob.max_speed), 0).rotated(newRotation)
-	
-	$HUD.connect("start_game", newMob, "_on_start_game")
-
-	add_child(newMob)

+ 7 - 19
godot/run/Main.tscn

@@ -1,17 +1,12 @@
-[gd_scene load_steps=9 format=2]
+[gd_scene load_steps=8 format=2]
 
 [ext_resource path="res://component/ui/hud/HUD.tscn" type="PackedScene" id=1]
 [ext_resource path="res://run/HouseInAForestLoop.ogg" type="AudioStream" id=2]
 [ext_resource path="res://component/ui/background/Background.tscn" type="PackedScene" id=3]
 [ext_resource path="res://component/entity/player/Player.tscn" type="PackedScene" id=4]
-[ext_resource path="res://component/entity/enemy/Enemy.tscn" type="PackedScene" id=5]
 [ext_resource path="res://effect/loose/gameover.wav" type="AudioStream" id=6]
 [ext_resource path="res://run/Main.gd" type="Script" id=7]
-
-[sub_resource type="Curve2D" id=1]
-_data = {
-"points": PoolVector2Array( 0, 0, 0, 0, -1.05261, 2.78944, 0, 0, 0, 0, 477.895, 2.78944, 0, 0, 0, 0, 460.055, 715.427, 0, 0, 0, 0, 5.76184, 708.778, 0, 0, 0, 0, -1.05261, 2.78944 )
-}
+[ext_resource path="res://component/entity/spawner/MobSpawner.tscn" type="PackedScene" id=8]
 
 [node name="Root" type="Node"]
 
@@ -23,10 +18,6 @@ script = ExtResource( 7 )
 __meta__ = {
 "_edit_group_": true
 }
-Mob = ExtResource( 5 )
-
-[node name="MobTimer" type="Timer" parent="Main"]
-wait_time = 0.53
 
 [node name="ScoreTimer" type="Timer" parent="Main"]
 
@@ -37,16 +28,11 @@ one_shot = true
 [node name="StartPositon" type="Position2D" parent="Main"]
 position = Vector2( 245.263, 375.79 )
 
-[node name="MobPath" type="Path2D" parent="Main"]
-curve = SubResource( 1 )
-
-[node name="MobSpawnLocation" type="PathFollow2D" parent="Main/MobPath"]
-position = Vector2( -1.05261, 2.78944 )
-rotation = -6.07153e-08
-
 [node name="Player" parent="Main" instance=ExtResource( 4 )]
 position = Vector2( 245.983, 380.055 )
 
+[node name="MobSpawner" parent="Main" instance=ExtResource( 8 )]
+
 [node name="HUD" parent="Main" instance=ExtResource( 1 )]
 
 [node name="Music" type="AudioStreamPlayer" parent="Main"]
@@ -54,9 +40,11 @@ stream = ExtResource( 2 )
 
 [node name="DeathSound" type="AudioStreamPlayer" parent="Main"]
 stream = ExtResource( 6 )
-[connection signal="timeout" from="Main/MobTimer" to="Main" method="_on_MobTimer_timeout"]
+[connection signal="game_over" from="Main" to="Main/MobSpawner" method="_on_Main_game_over"]
+[connection signal="game_started" from="Main" to="Main/MobSpawner" method="_on_Main_game_started"]
 [connection signal="timeout" from="Main/ScoreTimer" to="Main" method="_on_ScoreTimer_timeout"]
 [connection signal="timeout" from="Main/StartTimer" to="Main/Player" method="spawn"]
 [connection signal="timeout" from="Main/StartTimer" to="Main" method="_on_StartTimer_timeout"]
 [connection signal="hit" from="Main/Player" to="Main" method="game_over"]
+[connection signal="start_game" from="Main/HUD" to="Main/MobSpawner" method="_on_HUD_start_game"]
 [connection signal="start_game" from="Main/HUD" to="Main" method="new_game"]