I have a couple of questions concerning middleclass and stateful. I've been working for a couple of months with these two libraries in order to make a simple game template (here) (just for me, a simple base to bootstrap the development of OOP games).
1/ middleclass: In the github page, it's said it's best to write
Code: Select all
local class = require 'middleclass'
2/ stateful: in the case where my states are not defined in the same file as the "stateful" class, do you have a solution to avoid making the "stateful" class global, which may end in a lot of global variables in a big program ?
Here is a little example of my problem :
main.lua
Code: Select all
Hero = require 'Hero' -- here, I MUST make the Hero class global to add states in an external file
require 'HeroStateWalking'
my_hero = Hero:new()
-- some stuff
Code: Select all
local class = require 'lib.middleclass'
local Stateful = require 'lib.stateful'
local Hero = class('Hero'):include(Stateful)
function Hero:initialize()
self.x = 0
self.y = 0
end
function Hero:move(x,y)
self.x = self.x + x
self.y = self.y + y
end
function Hero:draw()
end
Code: Select all
local HeroStateWalking = Hero:addState('Walking')
function HeroStateWalking:draw()
print('walking drawing')
end
superzazu