mirror of
https://github.com/vrld/HC.git
synced 2024-11-18 12:54:23 +00:00
Adhere to new class commons specs.
This commit is contained in:
parent
6c7c17ed9e
commit
5d100c703f
@ -83,7 +83,7 @@ local function new(args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
|
-- 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 = {}
|
common = {}
|
||||||
function common.class(name, prototype, parent)
|
function common.class(name, prototype, parent)
|
||||||
local init = prototype.init or (parent or {}).init
|
local init = prototype.init or (parent or {}).init
|
||||||
@ -94,7 +94,6 @@ if class_commons ~= false and not common then
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- the module
|
-- the module
|
||||||
return setmetatable({new = new, inherit = inherit},
|
return setmetatable({new = new, inherit = inherit},
|
||||||
{__call = function(_,...) return new(...) end})
|
{__call = function(_,...) return new(...) end})
|
||||||
|
19
init.lua
19
init.lua
@ -24,14 +24,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local _NAME = (...)
|
local _NAME, common_local = ..., common
|
||||||
if not (common and common.class and common.instance) then
|
if not (type(common) == 'table' and common.class and common.instance) then
|
||||||
class_commons = true
|
assert(common_class ~= false, 'No class commons specification available.')
|
||||||
require(_NAME .. '.class')
|
require(_NAME .. '.class')
|
||||||
end
|
end
|
||||||
local Shapes = require(_NAME .. '.shapes')
|
local Shapes = require(_NAME .. '.shapes')
|
||||||
local Spatialhash = require(_NAME .. '.spatialhash')
|
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 newPolygonShape = Shapes.newPolygonShape
|
||||||
local newCircleShape = Shapes.newCircleShape
|
local newCircleShape = Shapes.newCircleShape
|
||||||
local newPointShape = Shapes.newPointShape
|
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_collide = callback_collide or __NULL__
|
||||||
self.on_stop = callback_stop 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
|
end
|
||||||
|
|
||||||
function HC:clear()
|
function HC:clear()
|
||||||
@ -57,7 +62,7 @@ function HC:clear()
|
|||||||
self._ghost_shapes = {}
|
self._ghost_shapes = {}
|
||||||
self.groups = {}
|
self.groups = {}
|
||||||
self._colliding_only_last_frame = {}
|
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
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -296,9 +301,9 @@ function HC:setSolid(shape, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- the module
|
-- the module
|
||||||
HC = common.class("HardonCollider", HC)
|
HC = common_local.class("HardonCollider", HC)
|
||||||
local function new(...)
|
local function new(...)
|
||||||
return common.instance(HC, ...)
|
return common_local.instance(HC, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable({HardonCollider = HC, new = new},
|
return setmetatable({HardonCollider = HC, new = new},
|
||||||
|
11
polygon.lua
11
polygon.lua
@ -24,10 +24,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local _PACKAGE = (...):match("^(.+)%.[^%.]+")
|
local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common
|
||||||
if not (common and common.class and common.instance) then
|
if not (type(common) == 'table' and common.class and common.instance) then
|
||||||
class_commons = true
|
assert(common_class ~= false, 'No class commons specification available.')
|
||||||
require(_PACKAGE .. '.class')
|
require(_PACKAGE .. '.class')
|
||||||
|
common_local, common = common, common_local
|
||||||
end
|
end
|
||||||
local vector = require(_PACKAGE .. '.vector-light')
|
local vector = require(_PACKAGE .. '.vector-light')
|
||||||
|
|
||||||
@ -454,6 +455,6 @@ function Polygon:intersectsRay(x,y, dx,dy)
|
|||||||
return tmin ~= math.huge, tmin
|
return tmin ~= math.huge, tmin
|
||||||
end
|
end
|
||||||
|
|
||||||
Polygon = common.class('Polygon', Polygon)
|
Polygon = common_local.class('Polygon', Polygon)
|
||||||
newPolygon = function(...) return common.instance(Polygon, ...) end
|
newPolygon = function(...) return common_local.instance(Polygon, ...) end
|
||||||
return Polygon
|
return Polygon
|
||||||
|
33
shapes.lua
33
shapes.lua
@ -26,15 +26,20 @@ THE SOFTWARE.
|
|||||||
|
|
||||||
local math_min, math_sqrt, math_huge = math.min, math.sqrt, math.huge
|
local math_min, math_sqrt, math_huge = math.min, math.sqrt, math.huge
|
||||||
|
|
||||||
local _PACKAGE = (...):match("^(.+)%.[^%.]+")
|
local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common
|
||||||
if not (common and common.class and common.instance) then
|
if not (type(common) == 'table' and common.class and common.instance) then
|
||||||
class_commons = true
|
assert(common_class ~= false, 'No class commons specification available.')
|
||||||
require(_PACKAGE .. '.class')
|
require(_PACKAGE .. '.class')
|
||||||
end
|
end
|
||||||
local vector = require(_PACKAGE .. '.vector-light')
|
local vector = require(_PACKAGE .. '.vector-light')
|
||||||
local Polygon = require(_PACKAGE .. '.polygon')
|
local Polygon = require(_PACKAGE .. '.polygon')
|
||||||
local GJK = require(_PACKAGE .. '.gjk') -- actual collision detection
|
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
|
-- base class
|
||||||
--
|
--
|
||||||
@ -77,7 +82,7 @@ function ConcavePolygonShape:init(poly)
|
|||||||
self._polygon = poly
|
self._polygon = poly
|
||||||
self._shapes = poly:splitConvex()
|
self._shapes = poly:splitConvex()
|
||||||
for i,s in ipairs(self._shapes) do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -402,33 +407,33 @@ function PointShape:draw()
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
Shape = common.class('Shape', Shape)
|
Shape = common_local.class('Shape', Shape)
|
||||||
ConvexPolygonShape = common.class('ConvexPolygonShape', ConvexPolygonShape, Shape)
|
ConvexPolygonShape = common_local.class('ConvexPolygonShape', ConvexPolygonShape, Shape)
|
||||||
ConcavePolygonShape = common.class('ConcavePolygonShape', ConcavePolygonShape, Shape)
|
ConcavePolygonShape = common_local.class('ConcavePolygonShape', ConcavePolygonShape, Shape)
|
||||||
CircleShape = common.class('CircleShape', CircleShape, Shape)
|
CircleShape = common_local.class('CircleShape', CircleShape, Shape)
|
||||||
PointShape = common.class('PointShape', PointShape, Shape)
|
PointShape = common_local.class('PointShape', PointShape, Shape)
|
||||||
|
|
||||||
local function newPolygonShape(polygon, ...)
|
local function newPolygonShape(polygon, ...)
|
||||||
-- create from coordinates if needed
|
-- create from coordinates if needed
|
||||||
if type(polygon) == "number" then
|
if type(polygon) == "number" then
|
||||||
polygon = common.instance(Polygon, polygon, ...)
|
polygon = common_local.instance(Polygon, polygon, ...)
|
||||||
else
|
else
|
||||||
polygon = polygon:clone()
|
polygon = polygon:clone()
|
||||||
end
|
end
|
||||||
|
|
||||||
if polygon:isConvex() then
|
if polygon:isConvex() then
|
||||||
return common.instance(ConvexPolygonShape, polygon)
|
return common_local.instance(ConvexPolygonShape, polygon)
|
||||||
end
|
end
|
||||||
|
|
||||||
return common.instance(ConcavePolygonShape, polygon)
|
return common_local.instance(ConcavePolygonShape, polygon)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function newCircleShape(...)
|
local function newCircleShape(...)
|
||||||
return common.instance(CircleShape, ...)
|
return common_local.instance(CircleShape, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function newPointShape(...)
|
local function newPointShape(...)
|
||||||
return common.instance(PointShape, ...)
|
return common_local.instance(PointShape, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -27,10 +27,11 @@ THE SOFTWARE.
|
|||||||
local floor = math.floor
|
local floor = math.floor
|
||||||
local min, max = math.min, math.max
|
local min, max = math.min, math.max
|
||||||
|
|
||||||
local _PACKAGE = (...):match("^(.+)%.[^%.]+")
|
local _PACKAGE, common_local = (...):match("^(.+)%.[^%.]+"), common
|
||||||
if not (common and common.class and common.instance) then
|
if not (type(common) == 'table' and common.class and common.instance) then
|
||||||
class_commons = true
|
assert(common_class ~= false, 'No class commons specification available.')
|
||||||
require(_PACKAGE .. '.class')
|
require(_PACKAGE .. '.class')
|
||||||
|
common_local, common = common, common_local
|
||||||
end
|
end
|
||||||
|
|
||||||
local Spatialhash = {}
|
local Spatialhash = {}
|
||||||
@ -155,4 +156,4 @@ function Spatialhash:draw(how, show_empty, print_key)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return common.class('Spatialhash', Spatialhash)
|
return common_local.class('Spatialhash', Spatialhash)
|
||||||
|
Loading…
Reference in New Issue
Block a user