【发布时间】:2014-07-17 00:29:03
【问题描述】:
我正在使用 Lua 和 LÖVE 开发一款游戏。
我更喜欢基于闭包的 OOP 方法,每个关卡都是一个函数的新实例,其中包含运行游戏所需的所有局部变量和函数。
我想把这个单一的功能分解成多个文件。我目前的解决方案与我在这里所做的类似:concatenating multiple files into a single one - 但我真的认为这还不理想。
例如,我将有一个 load.lua 文件,其中包含来自下面 sn-p 的 self.load 函数。
giant函数的代码sn-p参考如下:
levelSetup = function()
local time = 60
local timer = time
local timerIsRunning = true
local danger = 10
local handSize = 2
local itemsLeft = handSize
local curHand = 0
local lastHand = 10
local multiplier = 1
local self = {}
------------------------------------------------------
-- Initialize Values based on level Creation
------------------------------------------------------
self.load = function()
if curLevel.time == 1 then
time = 60
elseif curLevel.time == 2 then
time = 40
multiplier = multiplier*1.5
else
time = 20
multiplier = multiplier*2
end
if curLevel.danger == 1 then
danger = 10 --low catastrophe chance
elseif curLevel.danger == 2 then
danger = 30 --medium chance
multiplier = multiplier*1.5
else
danger = 50--high chance!
multiplier = multiplier*2
end
if curLevel.handSize == 1 then
handSize = 2
elseif curLevel.handSize == 2 then
handSize = 3
multiplier = multiplier*1.5
else
handSize = 4
multiplier = multiplier*2
end
itemsLeft = handSize
timer = time
self.nextHand()
end
return self
end
什么是最好的解决方案?
【问题讨论】:
标签: lua code-cleanup love2d