gai-godot-games/tutorial-game/scenes/main.gd

75 lines
1.4 KiB
GDScript
Raw Permalink Normal View History

2024-10-17 14:37:17 +02:00
extends Node2D
@export var mob_scene: PackedScene
var score: int = 0
func _ready() -> void:
$Player.hide()
func _process(delta: float) -> void:
pass
func _on_game_start_timer_timeout() -> void:
$ScoreIncrementTimer.start()
$MobSpawnTimer.start()
func new_game():
score = 0
$HUD.update_score(score)
$HUD.show_timeout_message("Get Ready")
$Music.play()
$Player.start($PlayerStartPosition.position)
$GameStartTimer.start()
func game_over():
$Music.stop()
$DeathSound.play()
$ScoreIncrementTimer.stop()
$MobSpawnTimer.stop()
$Player.disable()
$HUD.show_game_over()
get_tree().call_group("mobs", "dissolve")
func _on_player_hit() -> void:
game_over()
func _on_mob_spawn_timer_timeout() -> void:
$Path2D/MobSpawnLocation.progress_ratio = randf()
var mob: RigidBody2D = mob_scene.instantiate()
mob.position = $Path2D/MobSpawnLocation.position
mob.rotation = $Path2D/MobSpawnLocation.rotation + PI / 2
mob.rotation += randf_range(-PI / 4, PI / 4)
# ensure the mob spawns off screen
mob.position += Vector2(-100, 0).rotated(mob.rotation)
var velocity: Vector2 = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(mob.rotation)
add_child(mob)
func _on_score_increment_timer_timeout() -> void:
score += 1
$HUD.update_score(score)
func _on_hud_start_game() -> void:
new_game()