project-witchy/imperium-porcorum.love/scenes/levels/world.lua

132 lines
3.2 KiB
Lua

local World2D = require "core.modules.world.world2D"
local World = World2D:extend()
local Obj = require "scenes.levels.entities"
local Sti = require "libs.sti"
-- INIT FUNCTIONS
-- All functions to init the world and the map
function World:new(scene, mapfile)
self.scene = scene
self.map = Sti("assets/maps/" .. mapfile .. ".lua")
self.obj = Obj
self:initActors()
self:setGravity()
self.backcolor = self.map.backgroundcolor or {0, 0, 0}
self.activeObjects = 0
self:register()
self.isActive = true
end
function World:load()
self:loadMapCollisions()
self:loadMapActors()
end
function World:getStartPosition()
local startx, starty
for k, objectlayer in pairs(self.map.layers) do
if objectlayer.name == "playerstart" then
for k, object in pairs(objectlayer.objects) do
startx = object.x + (object.width/2)
starty = object.y + (object.height) - 12
end
self.map:removeLayer("playerstart")
end
end
return startx, starty
end
-- MAP FUNCTIONS
-- All map wrappers
function World:getDimensions()
return self.map.width * self.map.tilewidth,
self.map.height * self.map.tileheight
end
function World:setBackgroundColor(r, g, b)
self.backcolor = {r, g, b}
end
function World:getBackgroundColor()
return self.backcolor[1]/256, self.backcolor[2]/256, self.backcolor[3]/256
end
-- UPDATE FUNCTIONS
-- All update functions
function World:update(dt)
self.scene.playermanager:update(dt)
self:updateEntities(dt)
self:updateMap(dt)
self.scene.camera:update(dt)
end
function World:updateEntities(dt)
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self.scene.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
visibleThings[i]:update(dt)
end
self.activeObjects = len
end
function World:updateMap(dt)
self.map:update(dt)
end
-- DRAW FUNCTIONS
-- All function to draw the map, world and entities
function World:draw()
-- Ona attache puis détache la caméra pour dessiner le monde, afin que celui
-- reste "fixe" tandis que le jouer bouge.
self.scene.camera:floorCoord()
self:drawBackgroundColor()
self.scene.camera:attach()
self:drawMap()
self:drawEntities()
self.scene.camera:detach()
end
function World:drawEntities()
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self.scene.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
visibleThings[i]:draw(dt)
end
end
function World:drawMap()
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos
local tx, ty = self.scene.camera:getCoord()
local scale = self.scene.camera:getScale()
local tx = tx
local ty = ty
tx = math.floor(tx)
ty = math.floor(ty)
self.map:draw(-tx, -ty, scale, scale)
end
function World:drawBackgroundColor()
local r, g, b = self.backcolor[1], self.backcolor[2], self.backcolor[3]
love.graphics.setColor(r/256, g/256, b/256)
love.graphics.rectangle("fill", 0, 0, 480, 272)
utils.graphics.resetColor()
end
return World