class_name GirlMobNPC
extends GirlMob

onready var player = $"../../Player"
onready var jump_collider_ground = $"JumpColliderG"
onready var jump_collider_block = $"JumpColliderB"
onready var jump_collider_block_stair = $"StairColliderJCB"
onready var collider_line = $"ColliderLine"
export var alternate_hair: bool = false
var timer = 0.0

func flip(toggle: bool):
	.flip(toggle)
	if jump_collider_ground != null:
		if toggle:
			jump_collider_ground.set_position(Vector2(12, 24))
			jump_collider_block.set_position(Vector2(32, 0))
			jump_collider_block_stair.set_position(Vector2(32, 0))
		else:
			jump_collider_ground.set_position(Vector2(-12, 24))
			jump_collider_block.set_position(Vector2(-32, 0))
			jump_collider_block_stair.set_position(Vector2(-32, 0))

func _ready():
	lightning = $"../../Lightning"
	navmap  = get_world_2d().get_navigation_map()
	collider_line.set_visible(false)
	if alternate_hair:
		hair.set_sprite_frames(preload("res://asset/hair_black.tres"))
	jump_burst *= 1.5
	frozen = true

func _physics_process(delta: float):
	timer += delta
	
	if timer < 0.168:
		pass
		#return
	else:
		timer = 0.0
	
	if frozen:
		return

	var pathx = Navigation2DServer.map_get_path(
		navmap, 
		get_position(), 
		player.get_position(), 
		false)
	
	if pathx.size() < 2:
		return
	
	var path
	var used_next = false
	if pathx.size() < 3 or abs(position.x - pathx[1].x) > 50.0:
		path = pathx[1]
	else:
		path = pathx[2]
		used_next = true
	
	if abs(position.x - path.x) < 5.0 and used_next:
		jump_grace_timer.start()
	
	if DEBUG:
		collider_line.clear_points()
		for point in pathx:
			collider_line.add_point(point - get_position())
		collider_line.set_visible(true)
	
	if path.x > position.x:
		velocity.x += speed
		flip(true)
	else:
		velocity.x -= speed
		flip(false)
	
	if abs(real_velocity.y) < 0.01:
		if jump_collider_ground.get_overlapping_bodies().size() == 0:
			jump_grace_timer.start()
		if jump_collider_block.get_overlapping_bodies().size() == 1 and\
		   jump_collider_block_stair.get_overlapping_areas().size() == 0 and\
		   stair_collider_up.get_overlapping_areas().size() == 0 and\
		   stair_collider_down.get_overlapping_areas().size() == 0:
			jump_grace_timer.start()
	#print(str(get_position()) + " " + str(navigation_agent.get_next_location()))


func _kill_the_player(_body):
	if !frozen:
		player.lives -= 1
		frozen = true
		lightning.play()
