extends Spatial


var crabhead = preload("res://objects/enemies/crabhead/Crabhead.tscn")
var shockwave = preload("res://objects/enemies/misc/Shockwave.tscn")

var immune = preload("res://sounds/SFX/hurt/ineffective.mp3")
var aggro = preload("res://sounds/SFX/hurt/fist_aggro.wav")
var strike = preload("res://sounds/SFX/hurt/fist/fist_strike.wav")
var hurt = preload("res://sounds/SFX/hurt/fist/fist_hurt.wav")
var death = preload("res://sounds/SFX/hurt/fist/fist_death.wav")

export var arena_size = 12
export var speed = 7
export var health = 200

var max_health

export var make_shockwave = false

enum ATTACKS {
	MOVE,
	SLAM,
	FOLLOW,
	CRABHEAD,
}

var activated = false
var target = null
var animating = false

var crabhead_spawned = false

var current_attack = ATTACKS.SLAM

var new_pos : Vector3
var initial_translate = Vector3(0,0,0)


func _ready():
	$KinematicBody.translation.y = 0
	initial_translate = translation
	make_shockwave = false
	max_health = health

func _on_Attack_body_entered(body):
	if body != null:
		if body.has_method("take_damage") and !body.is_in_group("fist"):
			body.take_damage(130, ext.DAMAGE_TYPES.MELEE)



func choose_position():
	randomize()
	var new_x = (randi() % arena_size*2) - arena_size
	var new_z = (randi() % arena_size*2) - arena_size
	return Vector3(new_x, translation.y, new_z)


func _on_MoveTimer_timeout():
	new_pos = choose_position()

	
	
func _process(delta):
	if make_shockwave:
		make_shockwave = false
		var new_shockwave = shockwave.instance()
		new_shockwave.transform = transform
		new_shockwave.translation.y -= 5
		new_shockwave.scale = Vector3(1,1,1)
		get_parent().add_child(new_shockwave)
		$Hurt.pitch_scale = rand_range(0.8,1.2)
		$Hurt.stream = strike
		$Hurt.play()
	
	
	if activated:
		if current_attack == ATTACKS.SLAM or current_attack == ATTACKS.MOVE:
			if new_pos != null and !animating:
				translation.x = move_toward(translation.x, initial_translate.x + new_pos.x, speed*delta)
				translation.z = move_toward(translation.z, initial_translate.z + new_pos.z, speed*delta)
				
				if (translation.x == initial_translate.x + new_pos.x) and (translation.z == initial_translate.z + new_pos.z):
					#yield(get_tree().create_timer(rand_range(1.5,4.0)), "timeout")
					
					if current_attack == ATTACKS.SLAM:
						#yield(get_tree().create_timer(3.0), "timeout")
						$AnimationPlayer.play("slam")
						animating = true
						current_attack == ATTACKS.MOVE
					else:
						choose_attack()
		
		elif current_attack == ATTACKS.FOLLOW:
			#print("searching")
			$KinematicBody/Sprite3D.modulate = Color.aqua
			if !animating:
				translation.x = move_toward(translation.x, target.translation.x, speed*delta)
				translation.z = move_toward(translation.z, target.translation.z, speed*delta)
				
				if (abs(translation.x - target.translation.x) < 1) and (abs(translation.z - target.translation.z) < 1):
					$KinematicBody/Sprite3D.modulate = Color.white
					current_attack = ATTACKS.MOVE
					$AnimationPlayer.play("slam")
					animating = true
			
		elif current_attack == ATTACKS.CRABHEAD:
			if !crabhead_spawned:
				crabhead_spawned = true
				$CrabheadSpawned.start(8)
				var new_crabhead = crabhead.instance()
				new_crabhead.transform = $KinematicBody/Spawner.global_transform
				new_crabhead.scale = Vector3(1.25,1.25,1.25)
				new_crabhead.change_color(Color.aqua)
				new_crabhead.speed *= 1.5
				get_parent().add_child(new_crabhead)
			choose_attack()


func _on_Aggro_body_entered(body):
	if !activated:
		if body != null:
			if body.is_in_group("player"):
				$AnimationPlayer.play("awaken")
				activated = true
				target = body
				$KinematicBody/Aggro/CollisionShape.disabled = true
				$Hurt.pitch_scale = rand_range(0.8,1.2)
				$Hurt.stream = aggro
				$Hurt.play()
			
			
			
func choose_attack():
		
	if health > 0:	
		var attack_chance = randi()%8

		if attack_chance <= 1:
			current_attack = ATTACKS.FOLLOW
		elif attack_chance == 2:
			current_attack = ATTACKS.CRABHEAD
		else:
			current_attack = ext.choose([ATTACKS.MOVE, ATTACKS.SLAM])
			#current_attack = ATTACKS.SLAM
			choose_position()
			

func take_damage(amount, type = ext.DAMAGE_TYPES.MISC, source = null):
	if health > 0:
		if activated and current_attack != ATTACKS.FOLLOW:
			if type == ext.DAMAGE_TYPES.MELEE:
				amount *= 2
			health -= amount
			speed += ext.choose([0,1])
			$KinematicBody/Healthbar.visible = true
			$KinematicBody/Healthbar.scale.x = max(0,(float(health)/float(max_health))*5)
			if health <= 0:
				$KinematicBody/Healthbar.visible = false
				$KinematicBody/Sprite3D.modulate = Color.red
				$Hurt.pitch_scale = rand_range(0.8,1.2)
				$Hurt.stream = death
				$Hurt.play()
				yield($Hurt,"finished")
				queue_free()
			else:
				$KinematicBody/Sprite3D.modulate = Color.orange
				var t = Timer.new()
				t.set_wait_time(0.2)
				t.set_one_shot(true)
				self.add_child(t)
				t.start()
				yield(t, "timeout")
				t.queue_free()
				$KinematicBody/Sprite3D.modulate = Color.white
				$Hurt.pitch_scale = rand_range(0.8,1.2)
				$Hurt.stream = hurt
				$Hurt.play()
		else:
			$Hurt.pitch_scale = rand_range(0.8,1.2)
			$Hurt.stream = immune
			$Hurt.play()


func _on_CrabheadSpawned_timeout():
	crabhead_spawned = false


func _on_AnimationPlayer_animation_finished(anim_name):
	animating = false
	
	if anim_name == "slam":
		choose_attack()

