Main.gd 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. extends Node2D
  2. var _score: int
  3. @onready var _mob_timer: Timer = $MobTimer
  4. @onready var _score_timer: Timer = $ScoreTimer
  5. @onready var _start_timer: Timer = $StartTimer
  6. @onready var _start_position_marker: Marker2D = $StartPosition
  7. @onready var _mob_path: Path2D = $MobPath
  8. @onready var _mob_spawn_location: PathFollow2D = $MobPath/MobSpawnLocation
  9. @export var mob_scene: PackedScene
  10. @onready var player: Player = $Player
  11. # Called when the node enters the scene tree for the first time.
  12. func _ready() -> void :
  13. new_game()
  14. # Called every frame. 'delta' is the elapsed time since the previous frame.
  15. func _process(delta: float) -> void:
  16. pass
  17. func new_game() -> void :
  18. _score = 0
  19. player.start(_start_position_marker.position)
  20. _start_timer.start()
  21. pass
  22. func game_over() -> void :
  23. _score_timer.stop()
  24. _mob_timer.stop()
  25. pass
  26. func _on_mob_timer_timeout() -> void:
  27. print("called")
  28. var mob: Mob = mob_scene.instantiate()
  29. var spawn_location := _mob_spawn_location
  30. spawn_location.progress_ratio = randf()
  31. var direction := spawn_location.rotation + PI/2
  32. direction += randf_range(-PI/4, PI/4)
  33. var velocity := Vector2(randf_range(150, 250), 0.0)
  34. mob.position = spawn_location.position
  35. mob.rotation = direction
  36. mob.linear_velocity = velocity.rotated(direction)
  37. add_child(mob)
  38. func _on_score_timer_timeout() -> void:
  39. _score += 1
  40. func _on_start_timer_timeout() -> void:
  41. _mob_timer.start()
  42. _score_timer.start()