197 lines
4.1 KiB
Lua
197 lines
4.1 KiB
Lua
local cwd = (...):gsub('%.baseworld$', '') .. "."
|
|
|
|
local BaseWorld = Object:extend()
|
|
|
|
local Sti = require(cwd .. "libs.sti")
|
|
|
|
-- INIT FUNCTIONS
|
|
-- All functions to init the world and the map
|
|
|
|
function BaseWorld:new(scene, actorlist, mapfile)
|
|
self.scene = scene
|
|
|
|
self:initPlayers()
|
|
self:setActorList(actorlist)
|
|
self:setMap(mapfile)
|
|
self.actors = {}
|
|
end
|
|
|
|
function BaseWorld:setActorList(actorlist)
|
|
if actorlist == nil then
|
|
error("FATAL: You must set an actorlist to your world")
|
|
else
|
|
self.obj = require(actorlist)
|
|
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
|
|
-- Basic function to handle actors
|
|
|
|
function BaseWorld:newActor(name, x, y)
|
|
self.obj.index[name](self, x, y)
|
|
end
|
|
|
|
function BaseWorld:registerActor(actor)
|
|
table.insert(self.actors, actor)
|
|
end
|
|
|
|
function BaseWorld:removeActor(actor)
|
|
for i,v in ipairs(self.actors) do
|
|
if v == actor then
|
|
table.remove(self.actors, i)
|
|
end
|
|
end
|
|
end
|
|
|
|
function BaseWorld:moveActor(actor, x, y, filter)
|
|
actor.x = x
|
|
actor.y = y
|
|
-- as the baseworld have no collision function, we return empty collision
|
|
-- datas, but from the same type than bump2D will return
|
|
return x, y, {}, 0
|
|
end
|
|
|
|
function BaseWorld:queryRect(x, y, w, h)
|
|
local query = {}
|
|
local x2, y2 = x + w, y + h
|
|
for i,v in ipairs(self.actors) do
|
|
if (v.x >= x) and (v.x + v.w >= x1) and
|
|
(v.y >= y) and (v.y + v.h >= y1) then
|
|
|
|
table.insert(query, v)
|
|
end
|
|
end
|
|
|
|
return v
|
|
end
|
|
|
|
function BaseWorld:countActors()
|
|
return #self.actors
|
|
end
|
|
|
|
function BaseWorld:getActors()
|
|
return self.actors
|
|
end
|
|
|
|
-- PLAYER MANAGEMENT FUNCTIONS
|
|
-- Basic function to handle player actors
|
|
|
|
function BaseWorld:initPlayers()
|
|
self.players = {}
|
|
end
|
|
|
|
function BaseWorld:addPlayer(actor, sourceid)
|
|
local player = {}
|
|
player.actor = actor
|
|
player.sourceid = sourceid or 1
|
|
|
|
table.insert(self.players, player)
|
|
end
|
|
|
|
function BaseWorld:sendInputToPlayers(actor)
|
|
for i,v in ipairs(self.players) do
|
|
--TODO: make the player get from a selected source inputs
|
|
local keys = self.scene.sources[v.sourceid].keys
|
|
v.actor:getInput(keys)
|
|
end
|
|
end
|
|
|
|
function BaseWorld:removePlayer(actor)
|
|
for i,v in ipairs(self.players) do
|
|
if (v.actor == actor) then
|
|
table.remove(self.players, i)
|
|
end
|
|
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
|
|
-- All update functions
|
|
|
|
function BaseWorld:update(dt)
|
|
self:updateMap(dt)
|
|
self:sendInputToPlayers(dt)
|
|
self:updateActors(dt)
|
|
end
|
|
|
|
function BaseWorld:updateActors(dt)
|
|
for i,v in ipairs(self.actors) do
|
|
v:update(dt)
|
|
end
|
|
end
|
|
|
|
function BaseWorld:updateMap(dt)
|
|
if self.haveMap then
|
|
self.map:update(dt)
|
|
end
|
|
end
|
|
|
|
|
|
-- DRAW FUNCTIONS
|
|
-- All function to draw the map, world and actors
|
|
|
|
function BaseWorld:draw(dt)
|
|
self:drawBackgroundColor()
|
|
self:drawMap()
|
|
self:drawActors()
|
|
end
|
|
|
|
function BaseWorld:drawActors()
|
|
for i,v in ipairs(self.actors) do
|
|
v:draw()
|
|
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
|