extends Node2D
onready var juicemaster = get_tree().get_nodes_in_group("juicemaster")[0]

export (NodePath) var mouse_path
onready var mouse = get_node(mouse_path)

export (NodePath) var decal_manager_path
onready var decal_manager = get_node(decal_manager_path)

export (NodePath) var inverter_path
onready var inverter = get_node(inverter_path)

export (PackedScene) var decal_to_spawn

var breathing_intensity = 0.0

var time_till_next_shatter = 0.0
var time_till_next_slice = 0.0

func _process(delta):
	time_till_next_shatter = max(time_till_next_shatter-delta, 0.0)
	time_till_next_slice = max(time_till_next_slice-delta, 0.0)
	if Input.is_action_just_pressed("jump"):
		# if spacebar is pressed - spawn a decal
		spawn_decal_at_mouse()
		pass
	if Input.is_action_just_pressed("move_up"):
		# slap in random direction
		var direction = Vector2.RIGHT.rotated(TAU*randf())
		juicemaster.be_slapped(direction * 20, direction * 1000)
		pass
	if Input.is_action_just_pressed("move_left"):
		# decrease \/ breathing intensity
		breathing_intensity -= 10
		breathing_intensity = clamp(breathing_intensity, 0, 100)
		juicemaster.breath_target_amplitude(Vector2.ONE * breathing_intensity)
		pass
	if Input.is_action_just_pressed("move_right"):
		# increase /\ breathing intensity
		breathing_intensity += 10
		breathing_intensity = clamp(breathing_intensity, 0, 100)
		juicemaster.breath_target_amplitude(Vector2.ONE * breathing_intensity)
		pass
	if Input.is_action_just_pressed("move_down"):
		# shiver screen
		juicemaster.shiver_at_amplitude(20)
		pass
	if Input.is_action_just_pressed("l_click") and time_till_next_shatter <= 0:
		# split shard
		time_till_next_shatter = 0.5
		var where = mouse.position + (get_viewport().size/2.0)
#		print(where)
		juicemaster.hitstop(0.4)
		juicemaster.shatter_at_position(where, 4, 15, 100)
		pass
	if Input.is_action_just_pressed("r_click") and time_till_next_slice <= 0:
		# split entire screen
		time_till_next_slice = 0.3
		juicemaster.hitstop(0.2)
		var where = mouse.position + (get_viewport().size/2.0)
#		print(where)
		juicemaster.split_all(where, Vector2.RIGHT.rotated(randf()*TAU))
		
		pass
	if Input.is_action_just_pressed("Q"):
		# flash screen
		inverter.flash_image("Flash", 0.1)
		pass
	if Input.is_action_just_pressed("E"):
		# flash subliminal message on screen
		inverter.flash_image("KillLarge", 0.1)
		pass
	if Input.is_action_just_pressed("F"):
		$Label.visible = !$Label.visible
	pass	


func spawn_decal_at_mouse():
	decal_manager.spawn_decal_at_world_position(decal_to_spawn, mouse.global_position - decal_manager.texture_offset*Vector2(0,0.175))
	pass
