project-witchy/imperium-porcorum.love/core/modules/world/actors/baseactor.lua

254 lines
5.7 KiB
Lua

-- actor2D.lua :: the global implementation of an actor. Basically, it abstract
-- everything that isn't 2D or 3D related to the actor system.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local cwd = (...):gsub('%.baseactor$', '') .. "."
local BaseActor = Object:extend()
local Timer = require(cwd .. "utils.timer")
-- INIT FUNCTIONS
-- Initialise the actor and its base functions
function BaseActor:new(world, type, isSolid)
self.type = type or ""
self.isSolid = isSolid or false
self.depth = 0
self:setManagers(world)
self:initKeys()
self:initTimers()
self:setSprite()
self:initPhysics()
self:setDebugColor(1, 1, 1)
self:register()
end
function BaseActor:setManagers(world)
self.world = world
self.scene = world.scene
self.obj = world.obj
self.assets = self.scene.assets
end
function BaseActor:setDebugColor(r,g,b)
self.debug = {}
self.debug.r = r
self.debug.g = g
self.debug.b = b
end
function BaseActor:register()
self.world:registerActor(self)
self.isDestroyed = false
end
function BaseActor:destroy()
self.world:removeActor(self)
self.isDestroyed = true
end
-- PHYSICS INITIALISATION
-- Basic initialization of the physic systems
function BaseActor:initPhysics()
self:initMovement()
self:initGravity()
self:setBounceFactor()
self:setFilter()
end
function BaseActor:setBounceFactor(newBounceFactor)
self.bounceFactor = newBounceFactor or 0
end
function BaseActor:setFilter()
-- Init the bump filter
self.filter = function(item, other)
if (other.isSolid) then
return "slide"
else
return "cross"
end
end
end
function BaseActor:initMovement( )
-- Empty placeholder function
end
function BaseActor:initGravity( )
-- Empty placeholder function
end
-- UPDATE FUNCTIONS
-- Theses functions are activated every steps
function BaseActor:updateStart(dt)
end
function BaseActor:update(dt)
self:updateStart(dt)
self:updateTimers(dt)
self:autoMove(dt)
self:updateSprite(dt)
self:updateEnd(dt)
end
function BaseActor:updateEnd(dt)
end
function BaseActor:autoMove(dt)
-- The base actor don't have coordinate
-- so the autoMove is only usefull to its
-- 2D and 3D childrens
end
-- INPUT FUNCTIONS
-- get input from the world object
function BaseActor:initKeys()
self.keys = core.input.fakekeys
end
function BaseActor:getInput(keys)
self.keys = keys or core.input.fakekeys
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
-- DRAW FUNCTIONS
-- Draw the actors.
function BaseActor:drawStart()
end
function BaseActor:draw()
self:drawStart()
self:drawEnd()
end
function BaseActor:drawEnd()
end
function BaseActor:drawHUD(id, height, width)
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