Finish the_main_game_scene of first 2D game

This commit is contained in:
Daniel Siepmann 2024-03-26 13:31:39 +01:00
parent a0f5b4e46c
commit dc649047bc
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
4 changed files with 95 additions and 2 deletions

54
first_2d_game/Main.gd Normal file
View file

@ -0,0 +1,54 @@
extends Node
## The scene to use for mobs
@export var mob_scene : PackedScene
var score
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_mob_timer_timeout():
var mob = mob_scene.instantiate()
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
var direction = mob_spawn_location.rotation + PI / 2
mob.position = mob_spawn_location.position
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
add_child(mob)
func _on_score_timer_timeout():
score += 1
func _on_start_timer_timeout():
$MobTimer.start()
$ScoreTimer.start()
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()

39
first_2d_game/Main.tscn Normal file
View file

@ -0,0 +1,39 @@
[gd_scene load_steps=5 format=3 uid="uid://dmuja863phygg"]
[ext_resource type="PackedScene" uid="uid://ckfhsdod4ghug" path="res://Player.tscn" id="1_4p8ah"]
[ext_resource type="Script" path="res://Main.gd" id="1_ftc8b"]
[ext_resource type="PackedScene" uid="uid://df0p8vv7unjvu" path="res://Mob.tscn" id="2_sjjbm"]
[sub_resource type="Curve2D" id="Curve2D_7kfby"]
_data = {
"points": PackedVector2Array(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)
}
point_count = 5
[node name="Main" type="Node"]
script = ExtResource("1_ftc8b")
mob_scene = ExtResource("2_sjjbm")
[node name="Player" parent="." instance=ExtResource("1_4p8ah")]
[node name="MobTimer" type="Timer" parent="."]
wait_time = 0.5
[node name="ScoreTimer" type="Timer" parent="."]
[node name="StartTimer" type="Timer" parent="."]
wait_time = 2.0
one_shot = true
[node name="StartPosition" type="Marker2D" parent="."]
position = Vector2(240, 450)
[node name="MobPath" type="Path2D" parent="."]
curve = SubResource("Curve2D_7kfby")
[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
[connection signal="hit" from="Player" to="." method="game_over"]
[connection signal="timeout" from="MobTimer" to="." method="_on_mob_timer_timeout"]
[connection signal="timeout" from="ScoreTimer" to="." method="_on_score_timer_timeout"]
[connection signal="timeout" from="StartTimer" to="." method="_on_start_timer_timeout"]

View file

@ -4,7 +4,7 @@ extends RigidBody2D
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names() var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
$AnimatedSprite2D.play(mob_types[randi() % mob_types.size()) $AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.

View file

@ -49,7 +49,7 @@ func _process(delta):
func _on_body_entered(body): func _on_body_entered(body):
hide() hide()
hit.emit() hit.emit()
$CollisionShape2D.set_deffered('disabled', true) $CollisionShape2D.set_deferred('disabled', true)
func start(pos): func start(pos):
position = pos position = pos