-- monkey patches for zoetrope Sprite -- displace on a specific axis function Sprite:displaceDir(other, dir) if not self.solid or self == other or not other.solid then return end if STRICT then assert(other:instanceOf(Sprite), 'asked to displace a non-sprite') end if other.sprites then -- handle groups for _, spr in pairs(other.sprites) do self:displace(spr, dir) end else -- handle sprites local dim = util.dim(dir) local negMove = (other[dir] - self[dir]) + other[dim] local posMove = (self[dir] + self[dim]) - other[dir] -- TODO: re-add hinting? if negMove < posMove then chg = - negMove else chg = posMove end end other[dir] = other[dir] + chg end -- don't use zoetrope physics function Sprite:update(elapsed) if self.onUpdate then self:onUpdate(elapsed) end end -- directional physics function Sprite:doPhysics(dir, elapsed) local vel = self.velocity local acc = self.acceleration local drag = self.drag local minVel = self.minVelocity local maxVel = self.maxVelocity -- check existence of properties if STRICT then assert(vel, 'active sprite has no velocity property') assert(acc, 'active sprite has no acceleration property') assert(drag, 'active sprite has no drag property') assert(minVel, 'active sprite has no minVelocity property') assert(maxVel, 'active sprite has no maxVelocity property') assert(dir=='x' or dir=='y' or dir=='rotation', 'direction should be x, y, or rotation') end vel.x = vel.x or 0 vel.y = vel.y or 0 vel.rotation = vel.rotation or 0 -- physics if acc[dir] and acc[dir] ~= 0 then vel[dir] = vel[dir] + acc[dir] * elapsed else if drag[dir] then if vel[dir] > 0 then vel[dir] = vel[dir] - drag[dir] * elapsed if vel[dir] < 0 then vel[dir] = 0 end elseif vel[dir] < 0 then vel[dir] = vel[dir] + drag[dir] * elapsed if vel[dir] > 0 then vel[dir] = 0 end end end end if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end -- ugly hack for falling through floor on really slow frames if math.abs(vel[dir] * elapsed) > 32 then print('skip') return end if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end if self[dir] < 0 then self[dir] = 0 end local edge = the.view.map[util.dim(dir)] - the.player[util.dim(dir)] -- TODO: take map position into account if self[dir] > edge then self[dir] = edge end end