Fairy = {}
Fairy.type = "Fairy"
Fairy.supertype = Entity.type
Fairy.display = Animation:new(animation.fairy.flying)
setmetatable(Fairy, Entity)

function Fairy:new(x,y)
	local o = Entity:new(x,y)

	-- behaviour
	o.pos = {x = x, y = y}
	o.speed = 1.4
	o.range = 20
	o.vision_range = 120
	o.target = {x = x, y = y}
	o.hover_distance = 40

	-- animations
	o.body = Animation:new(animation.fairy.flying)
	o:centerOffset(o.body)
	o:createBox(o.body)

	-- light
	local light_data = {}
	light_data.radius = 80
	light_data.shine_radius = 80
	light_data.flicker = nil
	light_data.color = hex2rgb("#fed100")
	o.light = Light:new(o.pos.x,o.pos.y,light_data)

	-- timer
	o.particle_timer = 0
	o.particle_time = 5

	o:id()

	setmetatable(o, self)
	self.__index = self
	return o
end

function Fairy:doLogic()
	if self:checkVisionLine(main_player,self.vision_range) then

		self.target.x = main_player.pos.x + main_player.target_offset.x
		self.target.y = main_player.pos.y + main_player.target_offset.y

		local below = 1
		while not isThereObjectAt(
			self.pos.x,
			self.pos.y + below * game.scale,
			LoadedObjects.Collisions
		) do
			below = below + 1
			if below >= self.hover_distance then break end
		end
		local top = 1
		while not isThereObjectAt(
			self.pos.x,
			self.pos.y - top * game.scale,
			LoadedObjects.Collisions
		) do
			top = top + 1
			if top >= self.hover_distance then break end
		end
		self.target.y = self.target.y - top + below
	end

	local distance_x = self.target.x - self.pos.x
	local distance_y = self.target.y - self.pos.y
	local angle = getAngleFromVector(vector(distance_x,distance_y))
	local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)

	if distance < self.range then
		local random_x = math.random(-1, 1)
		local random_y = math.random(-1, 1)
		self.vel.x = self.vel.x * 0.9 + random_x/10
		self.vel.y = self.vel.y * 0.9 + random_y/10
	else
		local random_x = math.random(-6, 6)
		local random_y = math.random(-6, 6)
		self.vel.x = math.cos(angle)*self.speed + random_x/10
		self.vel.y = math.sin(angle)*self.speed + random_y/10
	end
end

function Fairy:handleAnimation()
	self.body:animate()
	--if self:isCollidingWith(main_player) then self.sprite_tint = {1,0,0} else self.sprite_tint = {1,1,1} end
	self:draw(self.body)

	self.particle_timer = self.particle_timer + 1
	if self.particle_timer >= self.particle_time then
		local vector = vector(self.vel.x,self.vel.y)
		local angle = getAngleFromVector(vector)
		self.particle_timer = 0
		local particle_data = {}
		particle_data.animation = animation.particle.simple
		particle_data.animation_speed = 1
		particle_data.sprite_tint = hex2rgb("#fed100")
		particle_data.sprite_alpha_fade = true
		particle_data.direction = angle-math.rad(180+math.random(60)-30)
		particle_data.speed = 1
		particle_data.time = 0.75
		particle_data.func = function(self)
				self.speed = self.speed - 0.01
				self.vel.x = self.speed * math.cos(self.direction)
				self.vel.y = self.speed * math.sin(self.direction)
		end
		Particle:new(self.pos.x,self.pos.y,particle_data)
	end
end

function Fairy:doPhysics()
	-- horizontal collision
	self:moveX(
		self.vel.x,
		function()
			self.vel.x = 0
		end
	)
	-- vertical collision
	self:moveY(
		self.vel.y,
		function()
			self.vel.y = 0
		end
	)
	-- final position
	self:adjustLight()
end

function Fairy:debug()
	Entity.debug(self)
	self:checkVisionLineDebug(main_player,self.vision_range)
end
