SHOW:
|
|
- or go back to the newest paste.
| 1 | wO={}
| |
| 2 | wO.global={}
| |
| 3 | wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
| |
| 4 | wO.global.isA=function(self, arg) | |
| 5 | if self.className==arg then | |
| 6 | return true | |
| 7 | elseif self.className~="base" then | |
| 8 | - | if wO.classes[self.extends]:isA(arg) then |
| 8 | + | if self.extends:isA(arg) then |
| 9 | return true | |
| 10 | end | |
| 11 | else | |
| 12 | return false | |
| 13 | end | |
| 14 | end | |
| 15 | wO.global.className="base" | |
| 16 | - | wO.global.new=function() return {} end
|
| 16 | + | wO.global.__settings={new=function(...) return {} end}
|
| 17 | wO.global.omt={}
| |
| 18 | ||
| 19 | setmetatable(wO.global, {__tostring=function(a) return "Class "..a.className end})
| |
| 20 | - | --love only code, do NOT use if not using love2D |
| 20 | + | |
| 21 | - | wO.importAllClasses= |
| 21 | + | |
| 22 | - | function(...) |
| 22 | + | |
| 23 | - | local lfs=love.filesystem |
| 23 | + | local function newClass(a, ...) |
| 24 | - | local folder=(arg[1] or "classes") |
| 24 | + | |
| 25 | - | for i,v in pairs(lfs.enumerate(folder)) do |
| 25 | + | b=(type(arg[1])=="table" and arg[1] or arg[2]) |
| 26 | - | if lfs.isDirectory(folder.."/"..v) then |
| 26 | + | b.extends=(type(arg[2])=="table" and wO.classes[arg[1]] or wO.classes["base"]) |
| 27 | - | wO.importAllClasses(folder.."/"..v) |
| 27 | + | |
| 28 | - | elseif lfs.isFile(folder.."/"..v) then |
| 28 | + | |
| 29 | - | require(folder.."/"..v) |
| 29 | + | local mt={}
|
| 30 | mt.__index=b.extends | |
| 31 | mt.__tostring=function(a) return "Class "..a.className end | |
| 32 | mt.__call=function(a,...) local arg={...} return newObj(a.className,...) end
| |
| 33 | if not b.mt then | |
| 34 | - | function wO.importClass(name,...) |
| 34 | + | |
| 35 | - | local lfs=love.filesystem |
| 35 | + | |
| 36 | - | local folder=(arg[1] or "") |
| 36 | + | |
| 37 | - | for i,v in pairs(lfs.enumerate(folder)) do |
| 37 | + | |
| 38 | - | if lfs.isDirectory(folder.."/"..v) then |
| 38 | + | |
| 39 | - | wO.importAllClasses(name,folder.."/"..v) |
| 39 | + | |
| 40 | - | elseif lfs.isFile(folder.."/"..v) and v==name then |
| 40 | + | |
| 41 | - | require(folder.."/"..v) |
| 41 | + | |
| 42 | end | |
| 43 | ||
| 44 | local function newObj(a,...) | |
| 45 | - | --love only code ends here |
| 45 | + | local o=(wO.classes[a].__settings.new(...)) |
| 46 | local omt={}
| |
| 47 | - | function wO.newClass(a, ...) |
| 47 | + | for i,v in pairs(wO.classes[a].omt) do |
| 48 | omt[i]=v | |
| 49 | - | b=(arg[1] or {})
|
| 49 | + | |
| 50 | omt.__index=wO.classes[a] | |
| 51 | setmetatable(o, omt) | |
| 52 | - | local mt |
| 52 | + | |
| 53 | - | if #arg==2 then |
| 53 | + | |
| 54 | - | mt={__index=wO.classes[arg[2]]}
|
| 54 | + | |
| 55 | - | b.extends=arg[2] |
| 55 | + | local function addClassInheritance(name, b) |
| 56 | return function(...) | |
| 57 | - | mt={__index=wO.classes["base"]}
|
| 57 | + | newClass(name, b, arg[1]) |
| 58 | - | b.extends="base" |
| 58 | + | |
| 59 | end | |
| 60 | - | mt.__tostring=function(a) return "Class "..a.className end mt.__call=function(a,...) local arg={...} return wO.newObj(a.className,...) end
|
| 60 | + | |
| 61 | local function addClass(name) | |
| 62 | return function(...) | |
| 63 | local arg={...}
| |
| 64 | if type(arg[1])=="table" then | |
| 65 | newClass(name,...) | |
| 66 | else | |
| 67 | return addClassInheritance(name,arg[1]) | |
| 68 | end | |
| 69 | - | b.mt=nil |
| 69 | + | |
| 70 | - | if not b.omt then |
| 70 | + | |
| 71 | - | b.omt={__index=wO.classes[a],__tostring=function(a) return "Object "..o.name..", a "..o.className end}
|
| 71 | + | |
| 72 | setmetatable(_G,{__index=function(_,a) return (a=="class" and addClass or (a==("newObj" or "new") and newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code. |