modules/world: put timers and sprites functions in the base actor

This commit is contained in:
Kazhnuz 2019-05-05 21:01:54 +02:00
parent 6225f1fb4e
commit 1d26588904
2 changed files with 95 additions and 92 deletions

View file

@ -26,8 +26,6 @@ local cwd = (...):gsub('%.actor2D$', '') .. "."
local BaseActor = require(cwd .. "baseactor") local BaseActor = require(cwd .. "baseactor")
local Actor2D = BaseActor:extend() local Actor2D = BaseActor:extend()
local Timer = require(cwd .. "utils.timer")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
@ -35,8 +33,6 @@ function Actor2D:new(world, type, x, y, w, h, isSolid)
Actor2D.super.new(self, world, type) Actor2D.super.new(self, world, type)
self:initPhysics(x, y, w, h, isSolid) self:initPhysics(x, y, w, h, isSolid)
self:register() self:register()
self:initTimers()
end end
function Actor2D:initPhysics(x, y, w, h, isSolid) function Actor2D:initPhysics(x, y, w, h, isSolid)
@ -57,34 +53,12 @@ function Actor2D:initPhysics(x, y, w, h, isSolid)
self.isSolid = isSolid or false self.isSolid = isSolid or false
self:setFilter() self:setFilter()
self:setSprite()
end end
function Actor2D:setBounceFactor(newBounceFactor) function Actor2D:setBounceFactor(newBounceFactor)
self.bounceFactor = newBounceFactor or 0 self.bounceFactor = newBounceFactor or 0
end end
-- TIMER FUNCTIONS
-- Control the integrated timers of the actor
function Actor2D:initTimers()
self.timers = {}
end
function Actor2D:addTimer(name, t)
self.timers[name] = Timer(self, name, t)
end
function Actor2D:updateTimers(dt)
for k,v in pairs(self.timers) do
v:update(dt)
end
end
function Actor2D:timerResponse(name)
-- here come the timer responses
end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
-- Theses functions are activated every steps -- Theses functions are activated every steps
@ -228,74 +202,9 @@ function Actor2D:applyGravity(dt)
end end
end end
-- DRAW & SPRITES FUNCTIONS -- DRAW FUNCTIONS
-- Draw the actors. -- Draw the actors.
function Actor2D:setSprite(spritename, ox, oy)
self.sprite = {}
self.sprite.name = spritename or nil
self.sprite.ox = ox or 0
self.sprite.oy = oy or 0
self.sprite.sx = 1
self.sprite.sy = 1
self.sprite.exist = (spritename ~= nil)
self.sprite.clone = nil
end
function Actor2D:cloneSprite()
if self.sprite.name ~= nil then
self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
end
end
function Actor2D:changeAnimation(animation, restart)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart)
else
self.sprite.clone:changeAnimation(animation, restart)
end
end
function Actor2D:setCustomSpeed(customSpeed)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed)
else
self.sprite.clone:setCustomSpeed(customSpeed)
end
end
function Actor2D:updateSprite(dt)
if (self.sprite.clone ~= nil) then
self.sprite.clone:update(dt)
end
end
function Actor2D:setSpriteScallingX(sx)
local sx = sx or 1
self.sprite.sx = sx
end
function Actor2D:setSpriteScallingY(sy)
local sy = sy or 1
self.sprite.sy = sy
end
function Actor2D:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky)
if (self.sprite.name ~= nil) then
local x = x + self.sprite.ox
local y = y + self.sprite.oy
local sx = sx or self.sprite.sx
local sy = sy or self.sprite.sy
if (self.sprite.clone ~= nil) then
self.sprite.clone:draw(x, y, r, sx, sy, ox, oy, kx, ky)
else
self.assets.sprites[self.sprite.name]:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
end
end
end
function Actor2D:getCenter() function Actor2D:getCenter()
return (self.x + (self.w / 2)), (self.y + (self.h / 2)) return (self.x + (self.w / 2)), (self.y + (self.h / 2))
end end

View file

@ -22,8 +22,11 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
local cwd = (...):gsub('%.baseactor$', '') .. "."
local BaseActor = Object:extend() local BaseActor = Object:extend()
local Timer = require(cwd .. "utils.timer")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
@ -31,6 +34,8 @@ function BaseActor:new(world, type)
self.type = type or "" self.type = type or ""
self:setManagers(world) self:setManagers(world)
self:initKeys() self:initKeys()
self:initTimers()
self:setSprite()
self:setDebugColor(1, 1, 1) self:setDebugColor(1, 1, 1)
end end
@ -70,4 +75,93 @@ function BaseActor:getInput(keys)
self.keys = keys or core.input.fakekeys self.keys = keys or core.input.fakekeys
end end
-- TIMER FUNCTIONS
-- Control the integrated timers of the actor
function BaseActor:initTimers()
self.timers = {}
end
function BaseActor:addTimer(name, t)
self.timers[name] = Timer(self, name, t)
end
function BaseActor:updateTimers(dt)
for k,v in pairs(self.timers) do
v:update(dt)
end
end
function BaseActor:timerResponse(name)
-- here come the timer responses
end
-- SPRITES FUNCTIONS
-- Handle the sprite of the actor
function BaseActor:setSprite(spritename, ox, oy)
self.sprite = {}
self.sprite.name = spritename or nil
self.sprite.ox = ox or 0
self.sprite.oy = oy or 0
self.sprite.sx = 1
self.sprite.sy = 1
self.sprite.exist = (spritename ~= nil)
self.sprite.clone = nil
end
function BaseActor:cloneSprite()
if self.sprite.name ~= nil then
self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
end
end
function BaseActor:changeAnimation(animation, restart)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart)
else
self.sprite.clone:changeAnimation(animation, restart)
end
end
function BaseActor:setCustomSpeed(customSpeed)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed)
else
self.sprite.clone:setCustomSpeed(customSpeed)
end
end
function BaseActor:updateSprite(dt)
if (self.sprite.clone ~= nil) then
self.sprite.clone:update(dt)
end
end
function BaseActor:setSpriteScallingX(sx)
local sx = sx or 1
self.sprite.sx = sx
end
function BaseActor:setSpriteScallingY(sy)
local sy = sy or 1
self.sprite.sy = sy
end
function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky)
if (self.sprite.name ~= nil) then
local x = x + self.sprite.ox
local y = y + self.sprite.oy
local sx = sx or self.sprite.sx
local sy = sy or self.sprite.sy
if (self.sprite.clone ~= nil) then
self.sprite.clone:draw(x, y, r, sx, sy, ox, oy, kx, ky)
else
self.assets.sprites[self.sprite.name]:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
end
end
end
return BaseActor return BaseActor