diff --git a/class.lua b/class.lua index d1ff6a8..ce9a679 100644 --- a/class.lua +++ b/class.lua @@ -83,7 +83,7 @@ local function new(args) end -- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons). -if class_commons ~= false and not common then +if common_class ~= false and not common then common = {} function common.class(name, prototype, parent) local init = prototype.init or (parent or {}).init @@ -94,7 +94,6 @@ if class_commons ~= false and not common then end end - -- the module return setmetatable({new = new, inherit = inherit}, {__call = function(_,...) return new(...) end}) diff --git a/init.lua b/init.lua index 44290ec..e96a9a3 100644 --- a/init.lua +++ b/init.lua @@ -24,14 +24,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- -local _NAME = (...) -if not (common and common.class and common.instance) then - class_commons = true +local _NAME, common_local = ..., common +if not (type(common) == 'table' and common.class and common.instance) then + assert(common_class ~= false, 'No class commons specification available.') require(_NAME .. '.class') end local Shapes = require(_NAME .. '.shapes') local Spatialhash = require(_NAME .. '.spatialhash') +-- reset global table `common' (required by class commons) +if common_local ~= common then + common_local, common = common, common_local +end + local newPolygonShape = Shapes.newPolygonShape local newCircleShape = Shapes.newCircleShape local newPointShape = Shapes.newPointShape @@ -48,7 +53,7 @@ function HC:init(cell_size, callback_collide, callback_stop) self.on_collide = callback_collide or __NULL__ self.on_stop = callback_stop or __NULL__ - self._hash = common.instance(Spatialhash, cell_size) + self._hash = common_local.instance(Spatialhash, cell_size) end function HC:clear() @@ -57,7 +62,7 @@ function HC:clear() self._ghost_shapes = {} self.groups = {} self._colliding_only_last_frame = {} - self._hash = common.instance(Spatialhash, self._hash.cell_size) + self._hash = common_local.instance(Spatialhash, self._hash.cell_size) return self end @@ -296,9 +301,9 @@ function HC:setSolid(shape, ...) end -- the module -HC = common.class("HardonCollider", HC) +HC = common_local.class("HardonCollider", HC) local function new(...) - return common.instance(HC, ...) + return common_local.instance(HC, ...) end return setmetatable({HardonCollider = HC, new = new}, diff --git a/polygon.lua b/polygon.lua index 0a43782..965a53b 100644 --- a/polygon.lua +++ b/polygon.lua @@ -24,10 +24,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- -local _PACKAGE = (...):match("^(.+)%.[^%.]+") -if not (common and common.class and common.instance) then - class_commons = true +local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common +if not (type(common) == 'table' and common.class and common.instance) then + assert(common_class ~= false, 'No class commons specification available.') require(_PACKAGE .. '.class') + common_local, common = common, common_local end local vector = require(_PACKAGE .. '.vector-light') @@ -454,6 +455,6 @@ function Polygon:intersectsRay(x,y, dx,dy) return tmin ~= math.huge, tmin end -Polygon = common.class('Polygon', Polygon) -newPolygon = function(...) return common.instance(Polygon, ...) end +Polygon = common_local.class('Polygon', Polygon) +newPolygon = function(...) return common_local.instance(Polygon, ...) end return Polygon diff --git a/shapes.lua b/shapes.lua index 3fadc26..d532fa6 100644 --- a/shapes.lua +++ b/shapes.lua @@ -26,15 +26,20 @@ THE SOFTWARE. local math_min, math_sqrt, math_huge = math.min, math.sqrt, math.huge -local _PACKAGE = (...):match("^(.+)%.[^%.]+") -if not (common and common.class and common.instance) then - class_commons = true +local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common +if not (type(common) == 'table' and common.class and common.instance) then + assert(common_class ~= false, 'No class commons specification available.') require(_PACKAGE .. '.class') end local vector = require(_PACKAGE .. '.vector-light') local Polygon = require(_PACKAGE .. '.polygon') local GJK = require(_PACKAGE .. '.gjk') -- actual collision detection +-- reset global table `common' (required by class commons) +if common_local ~= common then + common_local, common = common, common_local +end + -- -- base class -- @@ -77,7 +82,7 @@ function ConcavePolygonShape:init(poly) self._polygon = poly self._shapes = poly:splitConvex() for i,s in ipairs(self._shapes) do - self._shapes[i] = common.instance(ConvexPolygonShape, s) + self._shapes[i] = common_local.instance(ConvexPolygonShape, s) end end @@ -402,33 +407,33 @@ function PointShape:draw() end -Shape = common.class('Shape', Shape) -ConvexPolygonShape = common.class('ConvexPolygonShape', ConvexPolygonShape, Shape) -ConcavePolygonShape = common.class('ConcavePolygonShape', ConcavePolygonShape, Shape) -CircleShape = common.class('CircleShape', CircleShape, Shape) -PointShape = common.class('PointShape', PointShape, Shape) +Shape = common_local.class('Shape', Shape) +ConvexPolygonShape = common_local.class('ConvexPolygonShape', ConvexPolygonShape, Shape) +ConcavePolygonShape = common_local.class('ConcavePolygonShape', ConcavePolygonShape, Shape) +CircleShape = common_local.class('CircleShape', CircleShape, Shape) +PointShape = common_local.class('PointShape', PointShape, Shape) local function newPolygonShape(polygon, ...) -- create from coordinates if needed if type(polygon) == "number" then - polygon = common.instance(Polygon, polygon, ...) + polygon = common_local.instance(Polygon, polygon, ...) else polygon = polygon:clone() end if polygon:isConvex() then - return common.instance(ConvexPolygonShape, polygon) + return common_local.instance(ConvexPolygonShape, polygon) end - return common.instance(ConcavePolygonShape, polygon) + return common_local.instance(ConcavePolygonShape, polygon) end local function newCircleShape(...) - return common.instance(CircleShape, ...) + return common_local.instance(CircleShape, ...) end local function newPointShape(...) - return common.instance(PointShape, ...) + return common_local.instance(PointShape, ...) end return { diff --git a/spatialhash.lua b/spatialhash.lua index 0d8118a..eba3794 100644 --- a/spatialhash.lua +++ b/spatialhash.lua @@ -27,10 +27,11 @@ THE SOFTWARE. local floor = math.floor local min, max = math.min, math.max -local _PACKAGE = (...):match("^(.+)%.[^%.]+") -if not (common and common.class and common.instance) then - class_commons = true +local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common +if not (type(common) == 'table' and common.class and common.instance) then + assert(common_class ~= false, 'No class commons specification available.') require(_PACKAGE .. '.class') + common_local, common = common, common_local end local Spatialhash = {} @@ -155,4 +156,4 @@ function Spatialhash:draw(how, show_empty, print_key) end end -return common.class('Spatialhash', Spatialhash) +return common_local.class('Spatialhash', Spatialhash)