extends StaticBody


var ticksound = preload("res://sounds/SFX/timer/tick.wav")
var dingsound = preload("res://sounds/SFX/timer/ding.wav")


enum ACTIVATION_TYPES {
	SHOOT,
	SLAP,
	BOTH,
}

export var challenge_number = 1

export var time = 5.0

export(ACTIVATION_TYPES) var type = ACTIVATION_TYPES.SHOOT


func _ready():
	deactivate()
	
	
func _process(delta):
	if $Timer.time_left > 0:
		$Tick.pitch_scale = 1
		if $Timer.time_left < 5:
			$Tick.pitch_scale = 1.5

		if !$Tick.playing:
			$Tick.stream = ticksound
			$Tick.play()
			


func take_damage(amount, damage_type = 0, source = null):
	if damage_type == ext.DAMAGE_TYPES.RANGED and type == ACTIVATION_TYPES.SHOOT:
		activate()
	
	if damage_type == ext.DAMAGE_TYPES.MELEE and type == ACTIVATION_TYPES.SLAP:
		activate()
		
	if (damage_type == ext.DAMAGE_TYPES.RANGED or damage_type == ext.DAMAGE_TYPES.MELEE) and type == ACTIVATION_TYPES.BOTH:
		activate()
			
			
			
func activate():
	$Timer.start(time)
	var blocks = get_tree().get_nodes_in_group("challenge"+str(challenge_number))
	for block in blocks:
		block.visible = true
		block.set_collision_layer_bit(0,true)
		block.set_collision_mask_bit(0,true)
		
		
func deactivate():
	var blocks = get_tree().get_nodes_in_group("challenge"+str(challenge_number))
	for block in blocks:
		block.visible = false
		block.set_collision_layer_bit(0,false)
		block.set_collision_mask_bit(0,false)


func _on_Timer_timeout():
	$Tick.pitch_scale = 1
	$Tick.stream = dingsound
	$Tick.play()
	deactivate()
