extends Node3D

static var FUSE_SOUND:AudioStream = preload("res://assets/sounds/fuse.ogg")
static var EXPLOSION_SOUND:AudioStream = preload("res://assets/sounds/explode.wav")
static var JITTER_AMOUNT:int = 10
static var ENFORCEMENT_DEVICE_JITTER_AMOUNT:float = 0.005

var grow_explosion:bool = false
var original_position:Vector3

func reset_rule_enforcement_device() -> void:
	'''
	Start the fuse sound
	'''
	
	%SOUNDS.stream = FUSE_SOUND
	%SOUNDS.volume_db = -10
	%SOUNDS.play()
	visible = true
	%EXPLOSION.visible = false
	grow_explosion = false
	%EXPLOSION.scale = Vector3(0.355,0.355,0.355)

func explode() -> void:
	'''
	Make the rule enforcement device go boom
	'''
	
	%SOUNDS.stream = EXPLOSION_SOUND
	%SOUNDS.volume_db = 0
	%SOUNDS.play()
	grow_explosion = true
	%EXPLOSION.visible = true

func _ready() -> void:
	original_position = position
	reset_rule_enforcement_device()

func _process(_delta: float) -> void:
	%TIME_LEFT.text = ("%.02f" % GLOBALVARS.seconds_left).replace(".",":")
	position = original_position + Vector3(
		randf_range(-ENFORCEMENT_DEVICE_JITTER_AMOUNT,ENFORCEMENT_DEVICE_JITTER_AMOUNT),
		randf_range(-ENFORCEMENT_DEVICE_JITTER_AMOUNT,ENFORCEMENT_DEVICE_JITTER_AMOUNT),
		randf_range(-ENFORCEMENT_DEVICE_JITTER_AMOUNT,ENFORCEMENT_DEVICE_JITTER_AMOUNT)
	)
	if grow_explosion and %EXPLOSION.scale < Vector3(3,3,3):
		%EXPLOSION.scale += Vector3(0.25,0.25,0.25)
		%EXPLOSION.rotation_degrees += Vector3(
			randf_range(-JITTER_AMOUNT,JITTER_AMOUNT),
			randf_range(-JITTER_AMOUNT,JITTER_AMOUNT),
			randf_range(-JITTER_AMOUNT,JITTER_AMOUNT)
		)
	elif grow_explosion:
		%EXPLOSION.visible = false
		visible = false
