From a7c65630ef2d4b858a2e74f7c68e626045469e29 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Mon, 5 Mar 2012 10:26:14 +0100 Subject: [PATCH] Iterators for active and neighboring shapes. --- init.lua | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 2f7bb3c..01de7c8 100644 --- a/init.lua +++ b/init.lua @@ -112,9 +112,9 @@ local function new_shape(self, shape) hash:update(self, {x=x1,y=y1}, {x=x2,y=y2}, {x=x3,y=y3}, {x=x4,y=y4}) end - function shape:_getNeighbors() + function shape:neighbors() local x1,y1, x2,y2 = self:bbox() - return hash:getNeighbors(self, {x=x1,y=y1}, {x=x2,y=y2}) + return pairs(hash:getNeighbors(self, {x=x1,y=y1}, {x=x2,y=y2})) end function shape:_removeFromHash() @@ -125,6 +125,14 @@ local function new_shape(self, shape) return shape end +function HC:activeShapes() + local next, t, k, v = next, self._active_shapes + return function() + k, v = next(t, k) + return v + end +end + function HC:addPolygon(...) return new_shape(self, newPolygonShape(...)) end @@ -148,7 +156,6 @@ function HC:share_group(shape, other) return false end - -- get unique indentifier for an unordered pair of shapes, i.e.: -- collision_id(s,t) = collision_id(t,s) local function collision_id(self,s,t) @@ -161,9 +168,8 @@ end function HC:update(dt) -- collect colliding shapes local tested, colliding = {}, {} - for _,shape in pairs(self._active_shapes) do - local neighbors = shape:_getNeighbors() - for _,other in pairs(neighbors) do + for shape in self:activeShapes() do + for other in shape:neighbors() do local id = collision_id(self, shape,other) if not tested[id] then if not (self._ghost_shapes[other] or self:share_group(shape, other)) then