Main.gd 975 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. extends Node
  2. export (PackedScene) var Mob
  3. var score
  4. func _ready():
  5. randomize()
  6. func new_game():
  7. score = 0
  8. $HUD.update_score(score)
  9. $Player.start($StartPosition.position)
  10. $StartTimer.start()
  11. $HUD.show_message("Get Ready")
  12. $Music.play()
  13. func game_over():
  14. $DeathSound.play()
  15. $Music.stop()
  16. $ScoreTimer.stop()
  17. $MobTimer.stop()
  18. $HUD.show_game_over()
  19. func _on_MobTimer_timeout():
  20. # choose a random location on the Path2D
  21. $MobPath/MobSpawnLocation.set_offset(randi())
  22. var mob = Mob.instance()
  23. add_child(mob)
  24. var direction = $MobPath/MobSpawnLocation.rotation + PI/2
  25. mob.position = $MobPath/MobSpawnLocation.position
  26. # add some randomness to the direction
  27. direction += rand_range(-PI/4, PI/4)
  28. mob.rotation = direction
  29. mob.set_linear_velocity(Vector2(rand_range(mob.MIN_SPEED, mob.MAX_SPEED), 0).rotated(direction))
  30. func _on_StartTimer_timeout():
  31. $MobTimer.start()
  32. $ScoreTimer.start()
  33. func _on_ScoreTimer_timeout():
  34. score += 1
  35. $HUD.update_score(score)