From c43f983b4efc24b9bb96b0dd400dc3835ad9bc7c Mon Sep 17 00:00:00 2001 From: Colby Klein Date: Sat, 31 Dec 2011 18:52:36 -0800 Subject: [PATCH] Add 32log to global scripts dir. This will be useful. --- Scripts/32log.lua | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Scripts/32log.lua diff --git a/Scripts/32log.lua b/Scripts/32log.lua new file mode 100644 index 0000000000..340bc39df1 --- /dev/null +++ b/Scripts/32log.lua @@ -0,0 +1,39 @@ +--[[ +-- 32 lines of goodness. +-- Links to original source of code and usage documentation: +-- http://love2d.org/wiki/32_lines_of_goodness +-- http://love2d.org/forums/viewtopic.php?f=5&t=3344 +--]] + +local mt_class = {} + +function mt_class:extends(parent) + self.super = parent + setmetatable(mt_class, {__index = parent}) + parent.__members__ = parent.__members__ or {} + return self +end + +local function define(class, members) + class.__members__ = class.__members__ or {} + for k, v in pairs(members) do + class.__members__[k] = v + end + function class:new(...) + local newvalue = {} + for k, v in pairs(class.__members__) do + newvalue[k] = v + end + setmetatable(newvalue, {__index = class}) + if newvalue.__init then + newvalue:__init(...) + end + return newvalue + end +end + +function class(name) + local newclass = {} + _G[name] = newclass + return setmetatable(newclass, {__index = mt_class, __call = define}) +end