Last active
September 7, 2017 17:31
-
-
Save The0x539/acbacebcc423e6d85404ff247658673f to your computer and use it in GitHub Desktop.
The LunaLua meta-API, now with metatables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local _X = {} | |
local names = {} | |
--Resolves a file. Can be expanded to include directories like graphics or resources or what have you. | |
function _X.loadFile(name) | |
return Misc.resolveFile(name) | |
end | |
--Loads an API into _X, such that it can be accessed as a global from any code using _x | |
function _X.loadAPI(name) | |
if names[name] then | |
error("Tried to load API \""..name.."\" into _X, but the namespace is occupied by a function in _X") | |
else | |
_X[name] = API.load(name) | |
end | |
end | |
--Loads a PNG image after resolving its filename. Extension optional. | |
function _X.loadImage(name) | |
return Graphics.loadImage(_X.loadFile(name) or _X.loadFile(name..".png")) | |
end | |
--Same deal as loadImage but for audio. Not always necessary, because Audio.SfxOpen isn't, and DOES require file extension. | |
function _X.loadSFX(name) | |
return Audio.SfxOpen(_X.loadFile(name)) | |
end | |
--Creates a particles.lua Emitter, with filename extension optional, and x/y defaulting to zero. | |
local particles = API.load("particles") | |
function _X.loadEmitter(name,x,y) | |
x = x or 0 | |
y = y or 0 | |
return particles.Emitter(x,y, _X.loadFile(name) or _X.loadFile(name..".ini")) | |
end | |
--This is where the magic happens. | |
--Accepts one argument, a table. This table should be the output of getfenv(1) in the block of code making use of _X. | |
--Returns one value, also a table. This table should be second argument for setfenv(1) in the block of code making use of _X. | |
function _X.init(env) | |
local t = {} | |
local mt = { | |
__index = function(_,k) | |
return _X[k] or env[k] | |
end, | |
__newindex = function(_,k,v) | |
env[k] = v | |
end | |
} | |
setmetatable(t,mt) | |
return t | |
end | |
for k,_ in pairs(_X) do | |
names[k] = true | |
end | |
return _X |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment