Main.gd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extends Node
  2. @export var mob_scene: PackedScene
  3. var score
  4. func game_over():
  5. $ScoreTimer.stop()
  6. $MobTimer.stop()
  7. $HUD.show_game_over()
  8. $Music.stop()
  9. $DeathSound.play()
  10. func new_game():
  11. get_tree().call_group(&"mobs", &"queue_free")
  12. score = 0
  13. $Player.start($StartPosition.position)
  14. $StartTimer.start()
  15. $HUD.update_score(score)
  16. $HUD.show_message("Get Ready")
  17. $Music.play()
  18. func _on_MobTimer_timeout():
  19. # Create a new instance of the Mob scene.
  20. var mob = mob_scene.instantiate()
  21. # Choose a random location on Path2D.
  22. var mob_spawn_location = get_node(^"MobPath/MobSpawnLocation")
  23. mob_spawn_location.progress = randi()
  24. # Set the mob's direction perpendicular to the path direction.
  25. var direction = mob_spawn_location.rotation + PI / 2
  26. # Set the mob's position to a random location.
  27. mob.position = mob_spawn_location.position
  28. # Add some randomness to the direction.
  29. direction += randf_range(-PI / 4, PI / 4)
  30. mob.rotation = direction
  31. # Choose the velocity for the mob.
  32. var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
  33. mob.linear_velocity = velocity.rotated(direction)
  34. # Spawn the mob by adding it to the Main scene.
  35. add_child(mob)
  36. func _on_ScoreTimer_timeout():
  37. score += 1
  38. $HUD.update_score(score)
  39. func _on_StartTimer_timeout():
  40. $MobTimer.start()
  41. $ScoreTimer.start()