Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- objl = {};
- (function(exports)
- local function _isInstanceOf(Constructor, instance)
- return getmetatable(instance) == Constructor.__imt;
- end
- local function _checkClassMethod(Constructor, instance, className, methodName)
- if not _isInstanceOf(Constructor, instance) then
- error(Messages.MissingInstanceAtMethod
- :gsub('%%c', className)
- :gsub('%%m', methodName))
- end
- end
- local IS_MODULE_ROOM = tfm
- .get
- .room
- .name:byte(1) == 0x23;
- local Messages = {
- CannotAppendNonObject = "[objl] cannot append values except object/layer to a layer",
- MissingInstanceAtMethod = "[objl] missing instance of '%c' at method '%m'"
- };
- local ObjectTypes = {
- PHYSIC = 0,
- JOINT = 1,
- SHAMAN = 2
- };
- local PhysicTextureTypes = {
- WOOD = 0,
- ICE = 1,
- TRAMPOLINE = 2,
- LAVA = 3,
- CHOCOLATE = 4,
- EARTH = 5,
- GRASS = 6,
- SAND = 7,
- CLOUD = 8,
- WATER = 9,
- STONE = 10,
- SNOW = 11,
- COLOR = 12,
- CIRCLE = 13,
- INVISIBLE = 14,
- WEB = 15
- };
- local ShamanObjectsId = {
- ARROW = 0,
- LITTLEBOX = 1,
- BOX = 2,
- LITTLEBOARD = 3,
- BOARD = 4,
- BALL = 6,
- TRAMPOLINE = 7,
- ANVIL = 10,
- CANNON = 17,
- BOMB = 23,
- BALLOON = 28,
- RUNE = 32,
- SNOWBALL = 34,
- ICECUBE = 54
- };
- local initialObjectsId = {
- [ObjectTypes.PHYSIC] = 0,
- [ObjectTypes.JOINT] = 0
- };
- local objectProperties = {
- [ObjectTypes.PHYSIC] = {
- },
- [ObjectTypes.JOINT] = {
- },
- [ObjectTypes.SHAMAN] = {
- objType = true,
- x = true,
- y = true,
- vx = true,
- vy = true,
- ghost = true,
- rotation = true
- },
- };
- local naturalPhysics = {
- defaults = {
- damage = 0.2,
- friction = 0.3
- },
- [PhysicTextureTypes.ICE] = {
- friction = 0
- },
- [PhysicTextureTypes.TRAMPOLINE] = {
- damage = 1.2,
- friction = 0
- },
- [PhysicTextureTypes.LAVA] = {
- damage = 20,
- friction = 0
- },
- [PhysicTextureTypes.CHOCOLATE] = {
- friction = 20
- },
- [PhysicTextureTypes.CLOUD] = {
- miceCollision = false
- }
- };
- local enumObjectTypes = {
- JOINT = 0,
- PHYSIC = 1,
- SHAMAN = 2
- };
- local objsId = {
- [enumObjectTypes.JOINT] = {},
- [enumObjectTypes.PHYSIC] = {}
- };
- local bool, radian;
- bool = {
- convert = function(value)
- if value == true or
- value == 1 or
- value == 'true' then
- return true;
- end
- return false;
- end,
- enum = function(value)
- return value and 1 or 0;
- end
- };
- radian = {
- asDegrees = function(rad)
- local pi = math.pi;
- return (rad * pi) * (180 / pi);
- end
- };
- local function renderShamanObj(obj)
- local __data = obj.__data;
- if obj.__id then
- tfm.exec.removeObject(obj.__id);
- end
- obj.__id = tfm.exec.addShamanObject(__data.objId, __data.x, __data.y);
- end
- local freeId, objectsId, resolveNewId;
- freeId = function(objType, id)
- local idList = objectsId[objType];
- table.foreachi(idList, function(i, lid)
- if lid == id then
- table.remove(idList, i);
- return false;
- end
- end);
- end;
- objectsId = {
- [ ObjectTypes.JOINT ] = {},
- [ ObjectTypes.PHYSIC ] = {},
- [ ObjectTypes.SHAMAN ] = {}
- };
- resolveNewId = function(objType)
- local idList = objectsId[objType];
- local idLen = #idList;
- local initialId = initialObjectsId[objType];
- local availableId, prevId;
- table.sort(idList);
- table.foreachi(idList, function(i, id)
- if prevId and id > (prevId + 1) then
- availableId = id - 1;
- return false;
- elseif id > initialId then
- availableId = initialId;
- return false;
- end
- prevId = idList[i];
- end);
- if not availableId then
- if idLen > 0 then
- availableId = idList[idLen] + 1;
- else
- availableId = initialId;
- end
- end
- return availableId;
- end;
- local Joint, Layer, Physic, Shaman;
- Joint = {
- fn = {
- remove = function(self)
- _checkClassMethod(Joint, self, "objl.Joint", "remove");
- tfm.exec.removeJoint(self.__id);
- freeId(objectTpes.JOINT, self.__id);
- for i, layer in ipairs(self.__layers) do
- local bi = layer:findIndexByRef(self);
- if bi > 0 then
- table.remove(layer.__items, bi);
- end
- end
- -- gc
- self.__layers = nil;
- setmetatable(self, nil);
- end
- },
- };
- Joint.__mt = {
- __call = function()
- end
- };
- Joint.__imt = {
- __index = Joint.fn
- };
- setmetatable(Joint, Joint.__mt);
- Layer = {
- fn = {
- append = function(self, item)
- _checkClassMethod(Layer, self, "objl.Layer", "append");
- if not (_isInstanceOf(Layer, self) or
- _isInstanceOf(Physic, self) or
- _isInstanceOf(Shaman, self) or
- _isInstanceOf(Joint, self)) then
- error(Messages.CannotAppendNonObject);
- end
- table.insert(self.__items, item);
- if _isInstanceOf(Layer, item) then
- if not item.__parents then
- item.__parents = {};
- end
- table.insert(item.__parents, self);
- else
- if not item.__self then
- item.__self = {};
- end
- table.insert(item.__layers, self);
- end
- return self;
- end,
- findIndexByRef = function(self, ref)
- _checkClassMethod(Layer, self, "objl.Layer", "findIndexByRef");
- for i, item in ipairs(self.__items) do
- if item == ref then
- return i;
- end
- end
- return 0;
- end,
- get = function(self, index)
- _checkClassMethod(Layer, self, "objl.Layer", "get");
- return self.__items[index];
- end,
- remove = function(self, arg2)
- _checkClassMethod(Layer, self, "objl.Layer", "remove");
- if not _isInstanceOf(Layer, self) then
- error(Messages.MissingInstanceAtMethod
- :gsub('%i', 'objl.Layer')
- :gsub('%f', 'remove'))
- end
- if arg2 then
- local _type = type(arg2);
- if _type == 'table' then
- local i = self:findIndexByRef(arg2);
- if i > 0 then
- self.__items[i]:remove();
- end
- elseif _type == 'number' then
- local item = self.__items[arg2];
- if item then
- self.__items[arg2]:remove();
- end
- else
- error("bad argument #2 to 'remove' (table, nil or number expected, got ".. _type ..")");
- end
- return self;
- end
- for i, item in ipairs(self.__items) do
- item:remove();
- end
- for i, layer in ipairs(self.__parents) do
- local bi = layer:findIndexByRef(self);
- if bi > 0 then
- table.remove(layer.__parents, bi);
- end
- end
- -- gc
- self.__items = nil;
- self.__parents = nil;
- setmetatable(self, nil);
- end
- }
- };
- Layer.__mt = {
- __call = function()
- end
- };
- Layer.__imt = {
- __index = Layer.fn
- };
- setmetatable(Layer, Layer.__mt);
- Physic = {
- fn = {
- remove = function(self)
- _checkClassMethod(Physic, self, "objl.Physic", "remove");
- tfm.exec.removePhysicObject(self.__id);
- freeId(objectTpes.PHYSIC, self.__id);
- for i, layer in ipairs(self.__layers) do
- local bi = layer:findIndexByRef(self);
- if bi > 0 then
- table.remove(layer.__items, bi);
- end
- end
- -- gc
- self.__layers = nil;
- setmetatable(self, nil);
- end
- },
- };
- Physic.__mt = {
- __call = function(textureType, x, y, width, height, _options)
- local instance = {
- __data = {
- textureType = textureType
- },
- __id = resolveNewId(ObjectTypes.PHYSIC),
- __layers = {}
- };
- setmetatable(instance, Physic.__imt);
- return instance;
- end
- };
- Physic.__imt = {
- __index = Physic.fn
- };
- setmetatable(Physic, Physic.__mt);
- Shaman = {
- fn = {
- ghost = function(self, isGhost)
- _checkClassMethod(Shaman, self, "objl.Shaman", "ghost");
- if not isGhost then
- return self.__data.ghost;
- end
- self.__data.ghost = ghost;
- renderShamanObj(self);
- return self;
- end,
- move = function(self, x, y, offset, ...)
- _checkClassMethod(Shaman, self, "objl.Shaman", "move");
- local __data = self.__data;
- if offset then
- if x then
- __data.x = __data.x + x;
- end
- if y then
- __data.y = __data.y + y;
- end
- end
- __data.x = x or __data.x;
- __data.y = y or __data.y;
- tfm.exec.moveObject(self.__id, x, y, ...);
- return self;
- end,
- remove = function(self)
- _checkClassMethod(Shaman, self, "objl.Shaman", "remove");
- tfm.exec.removeObject(self.__id);
- for i, layer in ipairs(self.__layers) do
- local bi = layer:findIndexByRef(self);
- if bi > 0 then
- table.remove(layer.__items, bi);
- end
- end
- -- gc
- self.__layers = nil;
- setmetatable(self, nil);
- end,
- rotation = function(self, rads)
- _checkClassMethod(Shaman, self, "objl.Shaman", "rotation");
- if not rads then
- return self.__data.rotation;
- end
- self.__data.rotation = rads;
- renderShamanObj(self);
- return self;
- end,
- update = function(self, options)
- _checkClassMethod(Shaman, self, "objl.Shaman", "update");
- local _otype = type(options);
- if 'table' ~= type(options) then
- error("bad argument #1 to 'update' (table expected, got " .. _otype .. ")");
- end
- local __data = self.__data;
- local objType = ObjectTypes.SHAMAN;
- table.foreach(options, function(k, v)
- if objectProperties[objType][k] then
- __data[k] = v;
- end
- end);
- renderShamanObj(self);
- return self;
- end,
- x = function(self, x, offset, vx, offsetv)
- _checkClassMethod(Shaman, self, "objl.Shaman", "x");
- if not x then
- return self.__data.x;
- end
- self.__data.x = x;
- tfm.exec.moveObject(self.__id, x, nil, offset, vx, nil, offsetx);
- return self;
- end,
- y = function(self, y, offset, vy, offsetv)
- _checkClassMethod(Shaman, self, "objl.Shaman", "y");
- if not y then
- return self.__data.y;
- end
- self.__data.y = y;
- tfm.exec.moveObject(self.__id, nil, y, offset, nil, vy, offsetv);
- return self;
- end
- },
- };
- Shaman.__mt = {
- __call = function(t, objectId, x, y, ...)
- local objType = ObjectTypes.SHAMAN;
- local __data = {
- objId = objectId,
- x = x,
- y = y
- };
- local instance = {
- __data = __data,
- __layers = {}
- };
- table.foreach({ ... }, function(i, value)
- if i > 4 then
- return false;
- end
- if 'table' == type(value) then
- table.foreach(value, function(k, v)
- if objectProperties[objType][k] then
- __data[k] = v;
- end
- end);
- else
- local k = (i == 1 and "rotation") or
- (i == 2 and "vx") or
- (i == 3 and "vy") or
- (i == 4 and "ghost");
- __data[k] = value;
- end
- end);
- setmetatable(instance, Shaman.__imt);
- renderShamanObj(instance);
- return instance;
- end,
- __index = Shaman.fn
- };
- Shaman.__imt = {
- __index = Shaman.fn
- };
- setmetatable(Shaman, Shaman.__mt);
- exports.initialObjectsId = initialObjectsId;
- exports.naturalPhysics = naturalPhysics;
- exports.Joint = Joint;
- exports.Layer = Layer;
- exports.Physic = Physic;
- exports.PhysicTextureTypes = PhysicTextureTypes;
- exports.Shaman = Shaman;
- end)(objl);
Add Comment
Please, Sign In to add comment