extends Camera2D

class_name CameraGame

var _shake_config := {
	'amplitude': .0,
	'transition': Tween.TRANS_SINE,
	'easing': Tween.EASE_IN_OUT,
	'priority': 0
	}


func start_shake(duration:float = .2, frequency:float = 15.0, amplitude:float = 5.0, priority:int = 0):
	if priority >= _shake_config.priority:
		_shake_config.priority = priority
		_shake_config.amplitude = amplitude
		$Duration.wait_time = duration
		$Frequency.wait_time = 1 / frequency
		
		$Duration.start()
		$Frequency.start()
		
		_new_shake()
	
	
	pass

func _new_shake():
	var rand := Vector2(
		rand_range(-_shake_config.amplitude, _shake_config.amplitude),
		rand_range(-_shake_config.amplitude, _shake_config.amplitude)
	)
	
	$Tween.interpolate_property(self, "offset", self.offset, rand,  $Frequency.wait_time, _shake_config.transition, _shake_config.easing)
	$Tween.start()
	

func _reset():
	$Tween.interpolate_property(self, "offset", self.offset, Vector2.ZERO,  $Frequency.wait_time, _shake_config.transition, _shake_config.easing)
	$Tween.start()
	_shake_config.priority = 0

func _on_Frequency_timeout() -> void:
	_new_shake()



func _on_Duration_timeout() -> void:
	_reset()
	$Frequency.stop()
