local Base    = require "core.modules.world.actors.actor2D"
local Entity  = Base:extend() -- On créer la classe des entitées, c'est la classe de base

function Entity:new(world, type, x, y, w, h, isSolid)
  self.playerID = -1
  self.camera = world.scene.camera -- TODO: change that
  Entity.super.new(self, world, type, x, y, w, h, isSolid)
end

function Entity:initGravity()
  self.gacc = 550
  Entity.super.initGravity(self)
end

function Entity:setGravityFlag(flag)
  if (flag) then
    self:setYGravity(self.gacc)
  else
    self:setYGravity()
  end
end

function Entity:setMotion(xsp, ysp)
  self.xsp, self.ysp = xsp, ysp
end

function Entity:setMotionDirection(dir,spd)
  -- On commence par convertir l'angle en radians
  local dir = math.rad(dir)

  local xsp, ysp, cos, sin
  cos = math.cos(dir)
  sin = math.sin(dir)
  xsp = (spd * cos)
  ysp = (spd * sin)

  self.xsp, self.ysp = xsp, ysp
end

function Entity:gravity(dt)
  self.ysp = self.ysp + self.ygrav * dt
end

function Entity:applyFriction(dt)
  self.xsp = utils.math.toZero(self.xsp, self.xfrc * dt)
  self.ysp = utils.math.toZero(self.ysp, self.yfrc * dt)
end

function Entity:purge()
  self:remove()
end

function Entity:checkGround(ny)
  if not (self.grav == 0) then
    if ny < 0 then self.onGround = true end
  end
end

function Entity:getDirection()
  if not (utils.math.sign(self.xsp) == 0) then
    self.direction = utils.math.sign(self.xsp)
  end
end

function Entity:isInsideView()
  local camx, camy, camw, camh = self.world.cameras:getViewCoordinate(1)
  local x, y, w, h = self.x, self.y, self.w, self.h
  local border = border or 0
  if      (x + w + border >= camx) and (x < camx + camw + border)
      and (y + h + border >= camy) and (y < camy + camh + border) then
    return true
  else
    return false
  end
end

return Entity
