跳转至内容

23. 重写,对象,自动导入

3.19,想明白了,重写 BYTEPATH,将引入的外部库全部自己手写,然后结构还按原来的教程来,教程结束后重构整个项目。

Object

lua
Object = {}
Object.__index = Object

function Object:new()
end

function Object:extend()
    local cls = {}
    for k, v in paris(self) do
        if k:find("__") == 1 then
            cls[k] = v
        end
    end
    cls.__index = cls
    cls.super = self
    setmetatable(cls, self)
    return cls
end

function Object:__call(...)
    local obj = setmetatable({}, self)
    obj:new(...)
    return obj
end

return Object

Auto Require

lua
function requireFiles(file_list)
    for _, file in ipairs(file_list) do
        require(file:sub(1, -5))
    end
end

function recursiveEnumerate(dir, file_list)
    for _, item in ipairs(love.filesystem.getDirectoryItems(dir)) do
        local path = dir .. "/" .. item
        local info = love.filesystem.getInfo(path)
        if info then
            if info.type == "file" then
                table.insert(file_list, path)
            elseif info.type == "directory" then
                recursiveEnumerate(path, file_list)
            end
        end
    end
end