diff --git a/first_2d_game/Main.gd b/first_2d_game/Main.gd new file mode 100644 index 0000000..46617b4 --- /dev/null +++ b/first_2d_game/Main.gd @@ -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() diff --git a/first_2d_game/Main.tscn b/first_2d_game/Main.tscn new file mode 100644 index 0000000..b6b3154 --- /dev/null +++ b/first_2d_game/Main.tscn @@ -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"] diff --git a/first_2d_game/Mob.gd b/first_2d_game/Mob.gd index afd1618..f59f0fc 100644 --- a/first_2d_game/Mob.gd +++ b/first_2d_game/Mob.gd @@ -4,7 +4,7 @@ extends RigidBody2D # Called when the node enters the scene tree for the first time. func _ready(): 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. diff --git a/first_2d_game/Player.gd b/first_2d_game/Player.gd index e9ad682..06744c4 100644 --- a/first_2d_game/Player.gd +++ b/first_2d_game/Player.gd @@ -49,7 +49,7 @@ func _process(delta): func _on_body_entered(body): hide() hit.emit() - $CollisionShape2D.set_deffered('disabled', true) + $CollisionShape2D.set_deferred('disabled', true) func start(pos): position = pos