extends RefCounted
class_name ReedCameraShakePlayer

var _preset: ReedCameraShakePreset
var _time := 0.0
var _strength := 0.0
var _active := false

var _noise := FastNoiseLite.new()
var _noise_seed := randi()

func play(preset: ReedCameraShakePreset) -> void:
	_preset = preset
	_time = 0.0
	_strength = 0.0
	_active = true
	_noise.seed = _noise_seed

func stop() -> void:
	_active = false

func is_active() -> bool:
	return _active

func evaluate(delta: float) -> Vector2:
	if not _active or not _preset:
		return Vector2.ZERO

	_time += delta

	var total := _preset.fade_in + _preset.hold + _preset.fade_out
	if _time >= total:
		_active = false
		return Vector2.ZERO

	# ===== 强度曲线 =====
	if _time < _preset.fade_in:
		_strength = _time / _preset.fade_in
	elif _time < _preset.fade_in + _preset.hold:
		_strength = 1.0
	else:
		var t := (_time - _preset.fade_in - _preset.hold) / _preset.fade_out
		_strength = 1.0 - t

	# ===== Noise 偏移 =====
	var shake_t := _time * _preset.frequency
	var offset := Vector2(
		_noise.get_noise_1d(shake_t),
		_noise.get_noise_1d(shake_t + 1000)
	)

	return Vector2(
		offset.x * _preset.amplitude.x,
		offset.y * _preset.amplitude.y
	) * _strength
