extends KinematicBody2D

var drill_frames : int = 0
var y_velocity : float = 0

var item_preload = preload("res://Item.tscn")

func _physics_process(delta):
	if Input.is_action_pressed("ui_down"):
		if not ["drill_start", "drill_spin"].has($AnimatedSprite.animation):
			$AnimatedSprite.animation = "drill_start"

		drill_frames += 1
		
		if drill_frames < 32:
			var x_move = Input.get_axis("ui_left", "ui_right") * 20
		
			move_and_slide(Vector2(x_move, 0))
			
			if drill_frames == 31:
				y_velocity = 80
		else:
			$AnimatedSprite.animation = "drill_spin"
			
			y_velocity += delta * 120 * 6
			
			var x_move = Input.get_axis("ui_left", "ui_right") * 40
		
			move_and_slide(Vector2(x_move, y_velocity))
			
			self.is_on_floor()
			
			if $BottomCast.is_colliding():
				y_velocity = -60
				
				var colliding_with = $BottomCast.get_collider()
				if colliding_with is TileMap:
					var tile_pos : Vector2 = colliding_with.world_to_map(position + Vector2(-4, 10))
					
					mine_block(colliding_with, tile_pos)
	else:
		drill_frames = 0
		
		if $BottomCast.is_colliding():
			y_velocity = 60
		else:
			y_velocity += delta * 60 * 6
		
		if Input.is_action_pressed("ui_up"):
			y_velocity = -60
		
		var x_move = Input.get_axis("ui_left", "ui_right") * 60
		
		move_and_slide(Vector2(x_move, y_velocity))
		
		if y_velocity == -60:
			$AnimatedSprite.animation = "fly"
		elif x_move == 0:
			$AnimatedSprite.animation = "idle"
		elif x_move > 0:
			$AnimatedSprite.flip_h = true
			
			if $RightCast.is_colliding() and $BottomCast.is_colliding():
				$AnimatedSprite.animation = "side_hit"
				
				var colliding_with = $RightCast.get_collider()
				if $AnimatedSprite.frame == 2 and colliding_with is TileMap:
					var tile_pos : Vector2 = colliding_with.world_to_map(position + Vector2(0, 2))
					
					mine_block(colliding_with, tile_pos)
			else:
				$AnimatedSprite.animation = "gallop"
				
				if $AnimatedSprite.frame == 3 and not $step.playing:
					$step.play()
		else:
			$AnimatedSprite.flip_h = false
			
			if $LeftCast.is_colliding() and $BottomCast.is_colliding():
				$AnimatedSprite.animation = "side_hit"
				
				var colliding_with = $LeftCast.get_collider()
				if $AnimatedSprite.frame == 2 and colliding_with is TileMap:
					var tile_pos : Vector2 = colliding_with.world_to_map(position + Vector2(-8, 2))
					
					mine_block(colliding_with, tile_pos)
			else:
				$AnimatedSprite.animation = "gallop"
				
				if $AnimatedSprite.frame == 3 and not $step.playing:
					$step.play()

func mine_block(colliding_with : TileMap, tile_pos : Vector2):
	$"../../Score".score += 1
	
	$"../CPUParticles2D".position = colliding_with.map_to_world(tile_pos) + Vector2(8, 8)
	$"../CPUParticles2D".emitting = true
	$"../../Camera2D".shake()
	$"../../Cover".frame = 0
	
	$block.play()
	
	match colliding_with.get_cell(tile_pos.x, tile_pos.y):
		1:
			var new_item = item_preload.instance()
			new_item.item_type = "bone"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 1
		2:
			var new_item = item_preload.instance()
			new_item.item_type = "staff"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 2
		3:
			var new_item = item_preload.instance()
			new_item.item_type = "key"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 2
		4:
			var new_item = item_preload.instance()
			new_item.item_type = "book"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 2
		5:
			var new_item = item_preload.instance()
			new_item.item_type = "soul"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 3
		6:
			var new_item = item_preload.instance()
			new_item.item_type = "gem"
			$"../../Storage".add_child(new_item)
			$"../../Storage".materials.append(new_item)
			$"../../Score".score += 5
	
	colliding_with.set_cell(tile_pos.x, tile_pos.y, -1)
