extends Actor

export var player_number: int = 1

onready var player_hurtbox = $Hurtbox_Component/CollisionShape2D

onready var gun = $Gun


#var sound_effects = preload("res://src/playerC/resources/sound_effects.tres")

func _ready() -> void:
	movement_state_machine.init_animated_actor(self, movement_animations, movement_component)
	movement_component.player_number = player_number

	# Experimenting with sound based resources I did this and it worked
	#sound_effect_player.stream = sound_effects.sounds["pew"]
	#print(sounds.sounds["pew"])
	#sound_effects.play()
	
	# Trying something new with a custom resourse.
	for i in SoundEffects:
		sound_effects_dict[i.animation_name + str(i.frame_number)] = i.sound
	print (sound_effects_dict)	

func _unhandled_input(event: InputEvent) -> void:
	movement_state_machine.process_input(event)

func _physics_process(delta: float) -> void:
	movement_state_machine.process_physics(delta)
#	#TODO: So much hack
#	if direction.x != 0:
#		gun.set_scale( Vector2(direction.x,1))

	# Commented out to try parent transforms instead
#	if direction.x < 0:
#		var t = Transform2D()
#		# Translation
#		t.origin = Vector2(gun.offset_position.x * -1, gun.offset_position.y)
#		gun.transform = t
#	elif direction.x > 0:
#		var t = Transform2D()
#		# Translation
#		t.origin = Vector2(gun.offset_position.x, gun.offset_position.y)
#		gun.transform = t

	# Attempting a Kinematic2D level scale for flip operations instead.
	#self.transform
	#self.scale.x = -1

func _process(delta: float) -> void:
	movement_state_machine.process_frame(delta)
	PlayerInfo.player_position = global_position
	play_sound_frame(movement_animations.animation , movement_animations.frame)


##TODO: I don't like player class having do do this.
## Figure out how to programatically signal subscribe
#func _on_AnimatedSprite_animation_finished():
#	if movement_animations.frames.get_animation_loop(movement_animations.animation) == false:
#		movement_state_machine.current_state.animation_index += 1

func _on_Hurtbox_area_entered(area):
	print("Ouch.",area)
	movement_state_machine.change_state($Controllers/state_machine/hurt)

func set_hurtbox(enabled: bool):
	#player_hurtbox.disabled = enabled #nope
	player_hurtbox.set_deferred("disabled", !enabled) #WTF Apparently this is how to do it
	#$Hurtbox.monitoring = enabled #nope
	#$Hurtbox.set_deferred("monitoring", enabled) #works
	#$Hurtbox.monitoring = false #nope
	#$Hurtbox.set_monitoring(enabled) # Nope

func shoot_projectile():
	var is_shooting = false
	is_shooting = gun.shoot(direction.x)

#func play_sound():
#	sound_effects.stream = "res://assets/Sounds/crappy pew.wav"


