extends Node2D

var _score: int
@onready var _mob_timer: Timer = $MobTimer
@onready var _score_timer: Timer = $ScoreTimer
@onready var _start_timer: Timer = $StartTimer
@onready var _start_position_marker: Marker2D = $StartPosition
@onready var _mob_path: Path2D = $MobPath
@onready var _mob_spawn_location: PathFollow2D = $MobPath/MobSpawnLocation
@export var mob_scene: PackedScene
@onready var player: Player = $Player

# Called when the node enters the scene tree for the first time.
func _ready() -> void :
	new_game()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass
	
func new_game() -> void :
	_score = 0
	player.start(_start_position_marker.position)
	_start_timer.start()
	pass

func game_over() -> void :
	_score_timer.stop()
	_mob_timer.stop()
	pass


func _on_mob_timer_timeout() -> void:
	print("called")
	var mob: Mob = mob_scene.instantiate()
	
	var spawn_location := _mob_spawn_location
	spawn_location.progress_ratio = randf()
	var direction := spawn_location.rotation + PI/2
	direction += randf_range(-PI/4, PI/4)
	var velocity := Vector2(randf_range(150, 250), 0.0)
	
	mob.position = spawn_location.position
	mob.rotation = direction
	mob.linear_velocity = velocity.rotated(direction)
	
	add_child(mob)


func _on_score_timer_timeout() -> void:
	_score += 1

func _on_start_timer_timeout() -> void:
	_mob_timer.start()
	_score_timer.start()
