extends Node2D

@onready var _bgm = $AudioStreamPlayer
@onready var _spr = $Icon

var _prev = 0.0
var _timer = 0.0

func _ready() -> void:
	pass # Replace with function body.

func _process(delta: float) -> void:
	_timer -= delta * 2
	if _timer < 0:
		_timer = 0
		
	var pos = _bgm.get_playback_position() # 再生位置(秒).
	var bpm = _bgm.stream.get_bpm() # BPM
	var bar = 60 / bpm # 1拍の秒.
	var measure = int(pos / bar) + 1 # 小節(1始まり)
	var not_beat = false
	if (measure-1)%(4*8) <= 11:
		not_beat = true # 1〜7小節はビート演出なし
		# 代わりに回転する.
		_spr.rotation += delta * 4
	else:
		_spr.rotation = 0
		
	var now = fmod(pos, bar)
	if now < _prev:
		if not_beat:
			pass # 演出なし
		else:
			_timer = 1.0 # 拍が変わった.
	var sc = 1 + 1 * circ_in(_timer)
	_spr.scale = Vector2(sc, sc)
	_prev = now

# 円	
func circ_in(t:float) -> float:
	return -(sqrt(1 - t * t) - 1)
func circ_out(t:float) -> float:
	return sqrt(1 - (t - 1) * (t - 1))
