Code: Select all
local actors = { n = 0 }
actors.__index = actors
function actors.add(self, actor)
self.n = self.n + 1
self[self.n] = actor
end
function actors.draw(self)
for i=self.n,1,-1 do
self[i]:draw()
end
end
function love.load()
-- actor(name, position, isVisible)
local chris = actor("Chris", "center", true)
local tristan = actor("Tristan", "left", false)
actors:add(chris)
actors:add(tristan)
chris:speak("Hello there!")
tristan:show()
tristan:speak("Hi there!")
end
function love.draw()
actors:draw()
end
All the self stuff isn't rly necessary, but I've been coding stuff like classes for so long I just can't stop writing it like that xD
The issue with current snippit is that you won't be able to use chris or tristan in update code, so you would need either some kinda lookup like actors.getActor(name) function or instead of adding by number u add by name:
Code: Select all
local actors = {}
actors.__index = actors
function actors.add(self, actor)
self[actor.name] = actor -- or self[actor:getName()] not sure how this class library works, never used one for lua
end
function actors.draw(self)
for name,actor in pairs(self) do
actor:draw()
end
end
Code: Select all
function love.update(dt)
if SOME TRUE OR FALSE STATEMENT HERE then
actors["chris"]:speak("Omg it's TRUE")
end
end