CursedBook = Entity:New(x,y)

 function CursedBook:New(x,y)
 	local o = Entity:New(x,y)

  o.type = "cursed_book"
  -- behaviour
 	o.pos = {x = x, y = y}
  o.speed = 0.01
  o.range = 20
  o.target = {x = x, y = y}

  o.status = 0
  -- 0 - sleep
  -- 1 - getting up
  -- 2 - flying
  -- 3 - attack windup
  -- 4 - attack
  o.spawn_range = 100
  o.attack_range = 50

  -- animations
  o.body = Animation:New(animation.cursed_book.spawn)
  o.sprite_tint = {0.7,0.7,0.7}
  o:centerOffset(o.body)
  o:getBoundingBox(o.body)

  -- light
  o.light_range = 500
  o.light = CreateLight(o.pos.x,o.pos.y,o.light_range,2,HEX2RGB("#fe00d1"))

  table.insert(LoadedObjects.Entities,o)
  o.id = #LoadedObjects.Entities

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

function CursedBook:Smart()
  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 distance_x = self.target.x - self.pos.x
  local distance_y = self.target.y - self.pos.y
  local angle = GetAngleFromVector(distance_x,distance_y)
  local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)

  if self.status == 0 then
    if distance < self.spawn_range then
      self.status = 1
    end
  elseif self.status == -1 then
    if distance < self.range then
      self.vel.x = 0
      self.vel.y = 0
    else
      self.vel.x = math.cos(angle)*self.speed*distance
      self.vel.y = math.sin(angle)*self.speed*distance
    end
  elseif self.status == 2 then
    if distance < self.attack_range then
      self.status = 3
    end
  elseif self.status == 4 then

  end
end

function CursedBook:HandleAnimation()
  if self.status == 1 then
    if self.body.path == "assets/entities/cursed_book/spawn" then
      self.body.speed = 1/3
      local tint = 0.7 + 0.3 * (self.body.frame-1)/self.body.frames
      self.sprite_tint = {tint,tint,tint}
      if self.body.frame == self.body.frames then
        self.status = 2
        self.body = self.body:ChangeTo(animation.cursed_book.flying)
        self.sprite_tint = {1,1,1}
        --self:getBoundingBox(self.body,2,2,-2,-2)
        self:centerOffset(self.body)
      end
    end
  elseif self.status == 3 then
    if self.body.path == "assets/entities/cursed_book/flying" then
      self.body = self.body:ChangeTo(animation.cursed_book.attack_transition)
      self.body.speed = 1/3
      self:centerOffset(self.body)
      if self.body.frame == self.body.frames then
        self.status = 4
        self.body = self.body:ChangeTo(animation.cursed_book.attack_loop)
        self:centerOffset(self.body)
      end
    end
  end
  self.body:Animate()
  self:Draw(self.body)
end

function CursedBook:DoPhysics()
  if self.isFlying then
    local random_x = math.random(-4, 4)/100
    local random_y = math.random(-4, 4)/100
    self.vel.x = self.vel.x + random_x
    self.vel.y = self.vel.y + random_y
  end
  -- move

  self:CollisionMove()
  self:LightAdjust()
end

function CursedBook:Debug()
	-- draw center GREEN
	love.graphics.setColor(0,1,0)
	love.graphics.circle("line", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, self.spawn_range)
	love.graphics.setColor(1,0,0)
	love.graphics.circle("line", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, self.attack_range)
  Entity.Debug(self)
end
