-- On charge le module de scene et créer l'objet de scene des niveaux
local Scene       = require "core.modules.scenes"
local Level       = Scene:extend()

-- Chargement des controllers
local World   = require "scenes.levels.world"
local Camera = require "scenes.levels.camera"
local PlayerManager = require "scenes.levels.players"

local leveldatas  = require "datas.levels"

-- INIT FUNCTIONS
-- Initialize and launch the level

function Level:new(levelID, missionID)
  self:reset()
  Level.super.new(self)
  self.assets:batchImport("scenes.levels.assets")
  previousMenu = previous
  self:loadMission(levelID, missionID)

  self:register()
end

function Level:reset()
  self.datas        = {}

  self.pause        = false
  self.map          = nil
  self.backcolor    = {0, 0, 0}

end

function Level:loadMission(levelID, missionID)
  local datas, missiondatas
  self.datas = require("datas.levels." .. levelID)

  local mapfile = "assets.maps." .. self.datas.mapfile
  self.world = World(self, self.datas.mapfile, "scenes.levels.entities")
  self.world:loadMap()
  self.assets:setMusic("assets/music/" .. self.datas.music)

  self:launchMission()
end

function Level:launchMission()
  self.playermanager = PlayerManager(self)
  --self:resetSpawnAndEntities()
  self.assets:silence()
  self.assets:playMusic()

  self.playermanager:addPlayer(1)
  self.playermanager:spawnPlayer(1)

  self.camera = Camera(self, self.playermanager.startx, self.playermanager.starty)
end


-- UPDATE FUNCTIONS
-- Update the level

function Level:update(dt)
  local keys = self.sources[1].keys

  if keys["start"].isPressed then
    self.world:setActivity(self.pause)
    self.assets:setActivity(self.pause)
    self.pause = (self.pause == false)
  end
end

-- DRAW FUNCTIONS
-- draw level elements

function Level:draw(dt)
  utils.graphics.resetColor()

  if (self.pause == false) then
    self.playermanager:drawHUD()
  end
end

function Level:exit()
  self.controller:reset()
end

return Level
