add class() for easy definition of classes

This commit is contained in:
binarycat 2022-03-16 19:59:42 -04:00
parent d20e5392f8
commit 1549976382
4 changed files with 14 additions and 9 deletions

7
code/class.lua Normal file
View File

@ -0,0 +1,7 @@
function class(super, self)
assert(super == nil or super.__index == super)
self = self or {}
self.__index = self
setmetatable(self, super)
return self
end

View File

@ -1,8 +1,8 @@
Decoration = {}
Decoration.type = "Decoration"
Decoration.supertype = Entity.type
Decoration.display = Animation:new(animation.particle.simple)
setmetatable(Decoration, Entity)
Decoration = class(Entity, {
type = "Decoration",
supertype = Entity.type,
display = Animation:new(animation.particle.simple),
})
function Decoration:new(x,y,animation,light_data)
local o = Entity:new(x,y)
@ -22,7 +22,6 @@ function Decoration:new(x,y,animation,light_data)
o:id()
setmetatable(o, self)
self.__index = self
return o
end

View File

@ -1,6 +1,5 @@
LoadedObjects.Entities = {}
Entity = {}
Entity.type = "Entity"
Entity = class(nil, {type = "Entity"})
function Entity:new(x,y)
local o = {}
@ -26,7 +25,6 @@ function Entity:new(x,y)
o.sprite_flip = { x = 1, y = 1}
setmetatable(o, self)
self.__index = self
return o
end

View File

@ -7,6 +7,7 @@ require "data/sfx"
require "code/locale"
-- support functions
require "code/class"
require "code/math"
require "code/draw"
require "code/hex"