extends Sprite

class_name LogEntity

const h = [
	preload('res://images/game1/log_h_1.tres'),
	preload('res://images/game1/log_h_2.tres'),
	preload('res://images/game1/log_h_3.tres'),
	preload('res://images/game1/log_h_4.tres'),
]
const v = [
	preload('res://images/game1/log_v_1.tres'),
	preload('res://images/game1/log_v_2.tres'),
	preload('res://images/game1/log_v_3.tres'),
	preload('res://images/game1/log_v_4.tres'),
]

var type : int
var index : int
var collision_area : Area2D
var back_sensor : Area2D
var front_sensor : Area2D

func _init(_type=Type.H, _rotation=0):
	type = _type
	index = _rotation
	
	var physics_shape = Vector2(2 if type == Type.H else 1, 2 if type == Type.V else 1)
	var box_offset = Vector2(0, 8) if type == Type.H else Vector2(8, 0)
	
	# Collision box
	collision_area = Area2D.new()
	var shape = CollisionShape2D.new()
	shape.shape = RectangleShape2D.new()
	shape.shape.extents = Vector2(8, 8) * physics_shape
	collision_area.add_child(shape)
	collision_area.position = box_offset
	add_child(collision_area)
	
	# Sensors
	back_sensor = Area2D.new()
	shape = CollisionShape2D.new()
	shape.shape = RectangleShape2D.new()
	shape.shape.extents = Vector2(6, 6) * physics_shape
	back_sensor.add_child(shape)
	back_sensor.position = box_offset
	add_child(back_sensor)
	
	front_sensor = Area2D.new()
	shape = CollisionShape2D.new()
	shape.shape = RectangleShape2D.new()
	shape.shape.extents = Vector2(6, 6) * physics_shape
	front_sensor.add_child(shape)
	front_sensor.position = box_offset
	add_child(front_sensor)
	
	if type == Type.H:
		back_sensor.position.y -= 16
		front_sensor.position.y += 16
	else:
		back_sensor.position.x -= 16
		front_sensor.position.x += 16
	
	front_sensor.set_collision_layer_bit(0, 0)
	front_sensor.set_collision_mask_bit(0, 1)
	front_sensor.set_collision_mask_bit(1, 0)
	back_sensor.set_collision_layer_bit(0, 0)
	back_sensor.set_collision_mask_bit(0, 1)
	back_sensor.set_collision_mask_bit(1, 0)
	
	update()

func spawn_at(pos : Vector2):
	position = pos
	
	# Offset for special i=2
	if index == 2:
		if type == Type.H: position.y -= 16
		else: position.x -= 16

func update():
	var atlas = h
	if type == Type.V:
		atlas = v
	
	texture = atlas[index]
	
	if type == Type.H:
		if index == 2:
			offset.y += 16
		else:
			offset.y = 0
	else:
		if index == 2:
			offset.x += 16
		else:
			offset.x = 0

func push(from : Vector2, direction=1) -> bool:
	var sensor = front_sensor if direction == 1 else back_sensor
	if sensor.get_overlapping_bodies().size() > 0 || sensor.get_overlapping_areas().size() > 0:
		return false
	
	#Check if push is possible
	var authorized_positions = []
	var pos = (position / 16) - Vector2(1, 1)
	if type == Type.H:
		if direction == -1:
			pos += Vector2(0, 2)
		authorized_positions.append(pos)
		authorized_positions.append(pos + Vector2(1, 0))
	elif type == Type.V:
		if direction == -1:
			pos += Vector2(2, 0)
		authorized_positions.append(pos)
		authorized_positions.append(pos + Vector2(0, 1))
	
	var authorized = false
	for p in authorized_positions:
		if from == p:
			authorized = true
			break
	if !authorized: return false
	
	index += direction
	if index < 0:
		index = 3
	if index > 3:
		index = 0
	
	if type == Type.H:
		position.y += 16 * direction
	else:
		position.x += 16 * direction
	
	#todo: fall in hole
	
	update()
	return true

enum Type {H, V}
