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

View File

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

View File

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