-- ================ -- Private helpers -- ================ local setmetatable = setmetatable -- Internal class constructor local class = function(...) local klass = {} klass.__index = klass klass.__call = function(_,...) return klass:new(...) end function klass:new(...) local instance = setmetatable({}, klass) klass.__init(instance, ...) return instance end return setmetatable(klass,{__call = klass.__call}) end local Room = class() Room.__tostring = function(r) return ('Room x=%f y=%f w=%d h=%d'):format(r.x, r.y, r.width, r.height) end function Room:__init(x, y, width, height) self.x, self.y, self.width, self.height = x, y, width, height self.vx, self.vy = 0,0 end function Room:intersects(r) local a = { x=self.x+self.width/2, y=self.y+self.height/2, wh = self.width/2, hh = self.height/2 } local b = { x=r.x+r.width/2, y=r.y+r.height/2, wh = r.width/2+1, hh = r.height/2+1 } return not (math.abs(a.x - b.x) > (a.wh + b.wh) or math.abs(a.y - b.y) > (a.hh + b.hh)) end return Room