extends Node


var playing = false

onready var player = preload('res://scenes/game1/Player.tscn').instance()
onready var player_camera = player.get_node("Camera2D")

onready var levels = [
	$Game/Level1,
	$Game/Level2,
]

var current_level = 0

func _ready():
	launch_game()
	$Opening1.play('default')
	yield($Opening1, 'animation_finished')
	$Opening1.hide()

func launch_game():
	# Prepare levels
	spawn_entities()
	
	# Hide opening
	$Opening2.hide()
	
	# Prepare player
	var level = levels[current_level]
	level.add_child_below_node(level.get_node("Entities"), player)
	playing = true
	player_camera.make_current()
	player.connect('frame_changed', self, 'update_camera')
	
	# Spawn player
	var spawn_pos = Vector2()
	#todo: get spawn pos from map
	player.position = spawn_pos + Vector2(8, 8)

func spawn_entities():
	for node in levels:
		var entities = Node2D.new()
		entities.name = 'Entities'
		node.add_child_below_node(node.get_node('Props'), entities)
		
		for layer in node.get_children():
			if !(layer is TileMap): continue
			var tm  = layer as TileMap
			var tileset = tm.tile_set
			
			# Flatfish cells
			var cells = []
			for tile in ['flatfish_s', 'flatfish_s_hidden', 'flatfish_t', 'flatfish_t_hidden']:
				cells += tm.get_used_cells_by_id(tileset.find_tile_by_name(tile))
			
			for cell in cells:
				var fish = Flatfish.new()
				fish.position = cell * 16 + Vector2(8, 8)
				var t = tm.is_cell_transposed(cell.x, cell.y)
				var x = tm.is_cell_x_flipped(cell.x, cell.y)
				var  y = tm.is_cell_y_flipped(cell.x, cell.y)
				if t && x:
					fish.rotation_degrees = 90
				elif x && y:
					fish.rotation_degrees = 180
				elif t && y:
					fish.rotation_degrees  = 270
				entities.add_child(fish)
				tm.set_cell(cell.x, cell.y, -1)
				

var go_right = false
var go_left = false
var do_interact = false

func _input(event):
	if playing:
		if event.is_action_pressed('right'):
			go_right = true
		if event.is_action_pressed('left'):
			go_left = true
		if event.is_action_pressed('interact'):
			do_interact = true
		move()
		interact()
		
		if event.is_action_released('right'):
			go_right = false
		if event.is_action_released('left'):
			go_left = false
		if event.is_action_released('interact'):
			do_interact = false

func move():
	if playing && !is_camera_moving && !is_interacting:
		if go_right && !go_left:
			move_right()
		elif go_left && !go_right:
			move_left()

func move_right():
	is_camera_moving = true
	player.play('moving')
	update_camera()
	
	yield(player, 'animation_finished')
	
	player.play('default')
	player.frame = 0
	offset_player(16)
	is_camera_moving = false
	
	move()

func move_left():
	is_camera_moving = true
	offset_player(-16)
	player.play('moving', true)
	update_camera()
	
	yield(player, 'animation_finished')
	
	player.play('default')
	player.frame = 0
	is_camera_moving = false
	
	move()

func offset_player(offset):
	var r = int(ceil(player.rotation_degrees / 90.0)) % 4
	if r == 0: player.position.x += offset
	elif r == 1: player.position.y += offset
	elif r == 2: player.position.x -= offset
	elif r == 3: player.position.y -= offset
	
	print(player.position / 16)

const animation_steps = [3, 6, 10, 13]
var is_camera_moving = false
func update_camera():
	if is_camera_moving && !is_interacting:
		player_camera.position = Vector2(animation_steps[player.frame], 0)
	else:
		player_camera.position = Vector2()

var is_interacting = false
func interact():
	if !playing || is_camera_moving || is_interacting || !do_interact: return
	is_interacting = true
	
	var level = levels[current_level]
	var entities = level.get_node('Entities').get_children()
	
	var fish = null
	for e in entities:
		if e is Flatfish && e.position == player.position:
			fish = e
			break
	
	if fish != null:
		fish.appear()
		yield(get_tree().create_timer(0.2), "timeout")
		
		fish.do_rotation()
		player.rotation_degrees += 45
		yield(get_tree().create_timer(0.2), "timeout")
		
		fish.do_rotation()
		player.rotation_degrees += 45
		yield(get_tree().create_timer(0.2), "timeout")
		
		fish.disappear()
	
	is_interacting = false
	if fish != null: interact()
