Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- ltbge: a Lua text-based game engine
- -- version 1.0
- -- by ntnick
- --
- -- Bonus function!
- -- this function is by Konlab and theOriginalBIT.
- function string.seperate(str, separator)
- local t = {}
- for s in str:gmatch("[^"..separator.."]+") do
- t[#t+1] = s
- end
- return t
- end
- -------------------------------------------------
- if(not string.seperate) then
- error("ltbge: couldn't find string.seperate.",1)
- end
- ltbge = {
- class = {
- seaol = "001";
- };
- core = {
- version = 1.0;
- seaol = "002";
- };
- ascii = {
- seaol = "003";
- };
- files = {
- seaol = "004";
- };
- time = {
- seaol = "005";
- };
- colours = {
- bash = {
- -- Taken from the Arch Wiki.
- txtblk='\\e[0;30m';
- txtred='\\e[0;31m';
- txtgrn='\\e[0;32m';
- txtylw='\\e[0;33m';
- txtblu='\\e[0;34m';
- txtpur='\\e[0;35m';
- txtcyn='\\e[0;36m';
- txtwht='\\e[0;37m';
- bldblk='\\e[1;30m';
- bldred='\\e[1;31m';
- bldgrn='\\e[1;32m';
- bldylw='\\e[1;33m';
- bldblu='\\e[1;34m';
- bldpur='\\e[1;35m';
- bldcyn='\\e[1;36m';
- bldwht='\\e[1;37m';
- unkblk='\\e[4;30m';
- undred='\\e[4;31m';
- undgrn='\\e[4;32m';
- undylw='\\e[4;33m';
- undblu='\\e[4;34m';
- undpur='\\e[4;35m';
- undcyn='\\e[4;36m';
- undwht='\\e[4;37m';
- bakblk='\\e[40m';
- bakred='\\e[41m';
- bakgrn='\\e[42m';
- bakylw='\\e[43m';
- bakblu='\\e[44m';
- bakpur='\\e[45m';
- bakcyn='\\e[46m';
- bakwht='\\e[47m';
- txtrst='\\e[0m';
- };
- seaol = "006";
- };
- }
- -- Key:
- -- <String className> = required variable
- -- [Function callback] = not required variable
- -- ltbge.class.extend( <String className>, <Table o>, [Function callback] )
- -- Extend a class.
- -- For example:
- -- ltbge.class.extend("Hello",{
- -- sayHello = function() print("Hello, world!") end;
- -- })
- -- and later on, you can do this:
- -- ltbge.Hello.sayHello()
- --
- -- If you want to use callbacks:
- -- ltbge.class.extend("Hello",{
- -- sayHello = function() print("Hello, world!") end;
- -- }, function() print("ltbge.Hello created") end)
- function ltbge.class.extend(className,o,...)
- if(type(className) ~= "string") then
- error("ltbge.class.extend: expected string, not "..type(className),1)
- end
- if(type(o) ~= "table") then
- error("ltbge.class.extend: expected table, not "..type(className),1)
- end
- for i=1,#ltbge do
- if(type(ltbge[i]) == "table") then
- if(not ltbge[i].seaol) then
- ltbge[className] = o
- break
- end
- end
- end
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.class.extend: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- end
- -- ltbge.class.del( <String className>, [Function callback] )
- -- Delete a class.
- -- For example:
- -- ltbge.class.extend("mo",{hello = "Hi"})
- -- ltbge.class.del("mo")
- --
- -- If you want to use callbacks:
- -- ltbge.class.extend("mo",{hello = "Hi"},function() print("Hi") end)
- -- ltbge.class.del("mo",function() print("Hello, world!") end)
- function ltbge.class.del(className,...)
- if(type(className) ~= "string") then
- error("ltbge.class.del: expected string, not "..type(className),1)
- end
- if(ltbge[className]) then
- if(not ltbge[className].seaol) then
- ltbge[className] = nil
- end
- end
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.class.del: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- end
- -- ltbge.ascii.getAsciiFromFile( <String file>, [Function callback] )
- -- Get ASCII art from a file.
- -- For example:
- -- local myAscii = ltbge.ascii.getAsciiFromFile("resetti.txt")
- -- for i=1, #myAscii do
- -- print(myAscii[i])
- -- end
- --
- -- If you want to use callbacks:
- -- local myAscii = ltbge.ascii.getAsciiFromFile("resetti.txt",function() print("Loaded Resetti.txt") end)
- -- for i=1, #myAscii do
- -- print(myAscii[i])
- -- end
- function ltbge.ascii.getAsciiFromFile(file,...)
- if(type(file) ~= "string") then
- error("ltbge.ascii.getAsciiFromFile: expected string, not "..type(file),1)
- end
- local f = io.open(file,"rb")
- if(f == nil) then
- error("ltbge.ascii.getAsciiFromFile: file "..file.." does not exist",1)
- end
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.ascii.getAsciiFromFile: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- return string.seperate(f,"\n")
- end
- -- ltbge.files.writeTo( <String file>, <String content>, [Function callback] )
- -- Write data to a file.
- -- For example:
- -- ltbge.files.writeTo("resetti.txt","NO RESETTING!")
- --
- -- If you want to use callbacks:
- -- ltbge.files.writeTo("resetti.txt","NO RESETTING!",function()
- -- print("Wrote to resetti.txt")
- -- end)
- function ltbge.files.writeTo(file,content,...)
- if(type(file) ~= "string") then
- error("ltbge.files.writeTo: expected string, not "..type(file),1)
- end
- if(type(content) ~= "string" and type(content) ~= "number") then
- error("ltbge.files.writeTo: expected string/number, not "..type(content),1)
- end
- local f = io.open(file,"w")
- f:write(content)
- f:close()
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.files.writeTo: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- end
- -- ltbge.time.getTime([Function callback])
- -- Returns the time.
- -- For example:
- -- local time = ltbge.time.getTime()
- -- print(time.hour..":"..time.min)
- --
- -- If you want to use callbacks:
- -- local n = ltbge.time.getTime(function()
- -- print("Time obtained")
- -- end)
- -- print(n.hour..":"..n.min)
- function ltbge.time.getTime(...)
- local o = os.date()
- local f = string.seperate(o," ")
- local n = string.seperate(f[2],":")
- local g = {hour = n[2], min = n[3]}
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.time.getTime: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- return g
- end
- -- ltbge.time.get24HourTime([Function callback])
- -- Returns 24-hour time.
- -- For example:
- -- local time = ltbge.time.get24HourTime()
- -- print("24-hour time: "..time.hour..":"..time.min)
- --
- -- If you want to use callbacks:
- -- local n = ltbge.time.get24HourTime(function()
- -- print("24-hour time obtained")
- -- end)
- function ltbge.time.get24HourTime(...)
- local o = os.date()
- local f = string.seperate(o," ")
- local n = string.seperate(f[2],":")
- -- To get 24-hour time:
- -- Take the hour of the current time and multiply it by 2.
- -- Example: 12:45:65 would be 24:45:65.
- --
- -- To convert 24-hour time to regular time:
- -- Take the hour of the current time and divide it by 2.
- -- Example: 24:45:65 would be 12:45:65.
- local g = {hour = n[2]*2, min = n[3]}
- if(arg[1]) then
- if(type(arg[1]) ~= "function") then
- error("ltbge.time.get24HourTime: expected function, not "..type(arg[1]),1)
- end
- (arg[1])()
- end
- return g
- end
- -- That's it.
- -- Use ltbge responsibly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement