require("engine.vector")

Player = {
	speed = 1,
	velocity = vec(0, 0),
	aabb = {
		x = 0,
		y = 0,
		w = 10,
		h = 10,
		normal = {},
	},
	pointer = vec(0, 0),
	looking_at = nil,
}

function Player:update(dt)
	self:input()
	if self.aabb.normal.y ~= -1 then
		self.velocity.y += 10 * dt
	end

	local targeting = world.utils.get_tile(self.pointer.x, self.pointer.y)
	if targeting ~= nil then
		self.looking_at = targeting
	else
		self.looking_at = nil
	end

	collision.move(self.aabb, self.velocity.x, self.velocity.y)
end

function Player:draw()
	gfx.rect_fill(Player.aabb.x, Player.aabb.y, 10, 10, gfx.COLOR_WHITE)
	gfx.circ(self.pointer.x + (world.cell_size / 2), self.pointer.y + (world.cell_size / 2), 3, gfx.COLOR_WHITE)
end

function Player:input()
	local input_dir = vector.utils.input_vector()
	input_dir = util.vec_normalize(vector.utils.input_vector())
	self.velocity.x = input_dir.x * self.speed
	self.pointer = world.utils.to_tile(
		self.aabb.x + (input_dir.x * world.cell_size),
		self.aabb.y + (input_dir.y * world.cell_size)
	)

	if input.pressed(input.BTN1) and self.aabb.normal.y == -1 then
		self.velocity.y = -3
	end

	if input.pressed(input.BTN2) and self.looking_at ~= nil then
		self.looking_at.aabb = nil
		for i, v in pairs(collision.colliders) do
			print(v.aabb.x, v.aabb.y, v.aabb.w, v.aabb.h)
		end
		world.utils.set_tile(self.pointer.x, self.pointer.y, nil)
	end
end
