modules/world: add basic sti support

This commit is contained in:
Kazhnuz 2019-04-07 14:03:44 +02:00
parent f3543680ba
commit 9a47584feb

View file

@ -1,11 +1,16 @@
local cwd = (...):gsub('%.baseworld$', '') .. "."
local BaseWorld = Object:extend() local BaseWorld = Object:extend()
local Sti = require(cwd .. "libs.sti")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- All functions to init the world and the map -- All functions to init the world and the map
function BaseWorld:new(scene, actorlist) function BaseWorld:new(scene, actorlist, mapfile)
self.scene = scene self.scene = scene
self:setActorList(actorlist) self:setActorList(actorlist)
self:setMap(mapfile)
self.actors = {} self.actors = {}
end end
@ -17,6 +22,19 @@ function BaseWorld:setActorList(actorlist)
end end
end end
function BaseWorld:setMap(mapfile)
if mapfile == nil then
self.haveMap = false
self.haveBackgroundColor = false
self.backcolor = {128, 128, 128}
else
self.haveMap = true
self.map = Sti(mapfile)
self.haveBackgroundColor = true
self.backcolor = self.map.backgroundcolor or {128, 128, 128}
end
end
-- ACTOR MANAGEMENT FUNCTIONS -- ACTOR MANAGEMENT FUNCTIONS
-- Basic function to handle actors -- Basic function to handle actors
@ -66,22 +84,72 @@ function BaseWorld:getActors()
return self.actors return self.actors
end end
-- MAP FUNCTIONS
-- All map wrappers
function BaseWorld:getDimensions()
if self.haveMap then
return self.map.width * self.map.tilewidth,
self.map.height * self.map.tileheight
else
return core.screen:getDimensions()
end
end
function BaseWorld:setBackgroundColor(r, g, b)
self.backcolor = {r, g, b}
self.haveBackgroundColor = true
end
function BaseWorld:removeBackgroundColor()
self.haveBackgroundColor = false
end
function BaseWorld:getBackgroundColor()
return self.backcolor[1]/256, self.backcolor[2]/256, self.backcolor[3]/256
end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
-- All update functions -- All update functions
function BaseWorld:update(dt) function BaseWorld:update(dt)
self:updateMap(dt)
for i,v in ipairs(self.actors) do for i,v in ipairs(self.actors) do
v:update(dt) v:update(dt)
end end
end end
function BaseWorld:updateMap(dt)
if self.haveMap then
self.map:update(dt)
end
end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- All function to draw the map, world and actors -- All function to draw the map, world and actors
function BaseWorld:draw(dt) function BaseWorld:draw(dt)
self:drawBackgroundColor()
self:drawMap()
for i,v in ipairs(self.actors) do for i,v in ipairs(self.actors) do
v:draw(dt) v:draw(dt)
end end
end end
function BaseWorld:drawMap()
if self.haveMap then
self.map:draw()
end
end
function BaseWorld:drawBackgroundColor()
if self.haveBackgroundColor then
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
end
return BaseWorld return BaseWorld