extends Node2D

signal can_leave
var can_leave = false

onready var dialogue_box = $UI/Dialog
onready var music_player = get_node('/root/MusicPlayer') as MusicPlayer

#0
onready var oil_tanker = $OilTanker
onready var crab_0 = $OilTanker/Crab
export (String, FILE, "*.json") var dialogue_1
var leaving_0 = false

#1
onready var oil_tanker_inside_1 = $InsideOilTanker
onready var player_1 = $InsideOilTanker/Player
onready var next_trigger_1 = $InsideOilTanker/NextTrigger
onready var dialogue_trigger_1 = $InsideOilTanker/DialogueTrigger
export (String, FILE, "*.json") var dialogue_2

#2
onready var oil_tanker_inside_2 = $InsideOilTanker2
onready var camera_2 = $InsideOilTanker2/Camera2D
onready var player_2 = $InsideOilTanker2/Player
onready var prop1 = $InsideOilTanker2/Parallax/Prop1
onready var prop2 = $InsideOilTanker2/Parallax/Prop2
onready var next_trigger_2 = $InsideOilTanker2/NextTrigger
export (String, FILE, "*.json") var dialogue_3
export (String, FILE, "*.json") var dialogue_4

#3
onready var herbert_must_go_3 = $HerbertMustGo
export (String, FILE, "*.json") var dialogue_5

func _ready():
	assert(dialogue_1 != null)
	assert(dialogue_2 != null)
	assert(dialogue_3 != null)
	assert(dialogue_4 != null)
	assert(dialogue_5 != null)
	
	crab_0.connect('animation_finished', self, 'end_step_0')
	next_trigger_1.connect("body_entered", self, 'end_step_1')
	dialogue_trigger_1.connect("body_entered", self, 'dialogue_2')
	next_trigger_2.connect("body_entered", self, 'end_step_2')
	
	yield(dialogue_box.start_dialogue(dialogue_1), 'end')
	can_leave = true
	emit_signal('can_leave')
	
#	oil_tanker.frame = 12
#	end_step_0()
#	end_step_1(player_1)
#	end_step_2(player_2)

func dialogue_2(body : Node):
	print('end_step_1')
	if body.name == "Player":
		yield(dialogue_box.start_dialogue(dialogue_2), 'end')
		can_leave = true
		emit_signal('can_leave')

func end_step_0():
	print('end_step_0')
	if crab_0.frame > 1:
		leaving_0 = true
		
		if !can_leave:
			yield(self, 'can_leave')
		can_leave = false
		
		oil_tanker.queue_free()
		player_1.sleep = false

func end_step_1(body : Node):
	print('end_step_1')
	if body.name == "Player":
		player_1.sleep = true
		
		if !can_leave:
			yield(self, 'can_leave')
		can_leave = false
		
		oil_tanker_inside_1.queue_free()
		
		player_2.sleep = false
		camera_2.current = true
		yield(dialogue_box.start_dialogue(dialogue_3), 'end')
		can_leave = true
		emit_signal('can_leave')

func end_step_2(body : Node):
	print('end_step_2')
	if body.name == "Player":
		player_2.sleep = true
		
		if !can_leave:
			yield(self, 'can_leave')
		can_leave = false
		
		yield(dialogue_box.start_dialogue(dialogue_4), 'end')
		oil_tanker_inside_2.queue_free()
		herbert_must_go_3.play('default')
		
		yield(get_tree().create_timer(1), "timeout")
		yield(dialogue_box.start_dialogue(dialogue_5), 'end')
		yield(get_tree().create_timer(0.2), "timeout")
		music_player.fade_out()
		get_tree().change_scene("res://scenes/campfire2.tscn")

func _input(event):
	if oil_tanker != null && !leaving_0:
		if event.is_action_pressed('right'):
			if !crab_0.is_playing():
				crab_0.play('default')
		if event.is_action_released('right'):
			crab_0.stop()
		
		if event.is_action_pressed('left'):
			if !crab_0.is_playing() && crab_0.frame > 0:
				crab_0.play('default', true)
		if event.is_action_released('left'):
			crab_0.stop()

func _process(delta):
	if player_2 != null && !player_2.sleep:
		var screen_width = ProjectSettings.get_setting("display/window/size/width")
		camera_2.position.x = floor(clamp(player_2.position.x - screen_width / 2.0, 0, oil_tanker_inside_2.texture.get_width() - screen_width))
		prop2.position.x = floor((260 - camera_2.position.x) * 0.5)
		prop1.position.x = floor((300 - camera_2.position.x) * 0.75)
