extends "res://scripts/Level.gd"


var talk = [
	"YOU'RE MINE NOW!",
	"GAME OVER!",
	"FEEL MY SOLES NOW!",
	"COULDN'T HANDLE IT, EH?",
	"STOMP'ED!",
	"YOU'RE DONE!",
	"YOU'RE UNDER MY TOES NOW!",
	"ENJOYING THE VIEW?",
	"MY LITTLE BUG!",
	"I HOPE YOU LOVE TRAMPLE!",
	"FEEL THE POWER OF MY TOES!",
	"I CAUGHT YOU!",
	"YOU CAN'T RUN FROM ME!"	
]

var item = 0

func _ready():
	if Game.effects: 
		Game.effects.fade_out_black(0.3)
		Game.effects.change_pos(Vector2(0,0))
		Game.effects.position = Vector2(0,0)
		Game.effects.stop_all_effects()

	randomize()
	talk = DLC.get_content_talk(Game.character)
	var talk_item = randi() % talk.size()
	$Talk.text = self.talk[talk_item]
	
	
	
	$CoinCollected.text = "You've collected " + str(Game.coins) + " coins!"
	Game.coins = 0
	
	if Game.stomped: $Sprite.texture = Utils.load_file("res://@content/"+Game.character+"/character/gameover_stomped.jpg")
	else: $Sprite.texture = Utils.load_file("res://@content/" + Game.character + "/character/gameover.jpg")
	
	
	
	# warning-ignore:return_value_discarded
	Controller.connect("controller_right", self, "on_change")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_left", self, "on_change")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_escape", self, "on_escape")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_jump", self, "on_enter")
	
	if Game.effects: 
		Game.effects.change_pos(Vector2(0,0))
		Game.effects.position = Vector2(0,0)


# warning-ignore:unused_argument
func _process(delta):
	if Game.effects: 
		Game.effects.change_pos(Vector2(0,0))
		Game.effects.position = Vector2(0,0)

	
func on_change():
	if item == 1:
		$Selection.rect_position = $Back1.rect_position
		item = 0
	else:
		$Selection.rect_position = $Back2.rect_position
		item = 1


func disconnect_controls():
	Controller.disconnect("controller_right", self, "on_change")
	Controller.disconnect("controller_left", self, "on_change")
	Controller.disconnect("controller_enter", self, "on_enter")
	Controller.disconnect("controller_escape", self, "on_escape")
	Controller.disconnect("controller_jump", self, "on_enter")

func on_enter():
	if item == 1:
		on_escape()
		return
	
	disconnect_controls()	
	
	if Game.effects:
		Game.effects.fade_in_white(0.2)
	Utils.execute_after(Game, "next_secret_level", 0.3)
	Audio.play_sfx("unlock_treasure")
		
	pass
	
func on_escape():
	disconnect_controls()
	if Game.effects:
		Game.effects.fade_in_black(0.3)
	Utils.execute_after(Game, "next_level", 0.4)
	Audio.play_sfx("not_enough_coins")