@tool
extends CameraToolBasic
class_name ReedCameraShakeController
## =========================
## Runtime
## =========================

var _players: Array[ReedCameraShakePlayer] = []
var _current_offset := Vector2.ZERO

func _ready() -> void:
	if Engine.is_editor_hint():
		return
	
	set_process(true)

func _exit_tree() -> void:
	_players.clear()
	_current_offset = Vector2.ZERO

func _process(delta: float) -> void:
	if Engine.is_editor_hint():
		return
	
	if not enabled:
		_current_offset = Vector2.ZERO
		return

	var offset := Vector2.ZERO

	for p in _players:
		offset += p.evaluate(delta)

	# 移除已经结束的 player
	_players = _players.filter(func(p): return p.is_active())

	_current_offset = offset

## =========================
## Public API
## =========================
func play_shake(preset: ReedCameraShakePreset) -> void:
	if not preset:
		return

	var player := ReedCameraShakePlayer.new()
	player.play(preset)
	_players.append(player)

func stop_all() -> void:
	for p in _players:
		p.stop()
	_players.clear()
	_current_offset = Vector2.ZERO

## =========================
## Pipeline Output
## =========================

func get_camera_offset() -> Vector2:
	return _current_offset
