Skip to content

Commit

Permalink
Player has to duck under overhang now
Browse files Browse the repository at this point in the history
	If stand while on platform, will be ejected
	If stand while on floor, will slide out
Moving platform fixes
HardonCollider updates ( not needed though )
  • Loading branch information
Jordan Hoff committed Sep 10, 2012
1 parent b3cb854 commit 84463db
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 183 deletions.
2 changes: 1 addition & 1 deletion src/maps/black-caverns.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<object x="1704" y="216" width="72" height="24"/>
</objectgroup>
<objectgroup name="wall" width="125" height="36">
<object x="504" y="0" width="216" height="120"/>
<object x="504" y="0" width="216" height="144"/>
<object x="888" y="0" width="576" height="24"/>
<object x="1464" y="0" width="1536" height="144"/>
<object x="1776" y="144" width="192" height="96"/>
Expand Down
13 changes: 11 additions & 2 deletions src/nodes/movingplatform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,21 @@ function MovingPlatform.new(node, collider, map, isChain)
return mp
end

function MovingPlatform:collide()
function MovingPlatform:collide(player, dt, mtv_x, mtv_y)
if not player.currentplatform then
player.currentplatform = self
end
if not self.moving and self.pos <= 1 then
self.moving = true
end
end

function MovingPlatform:collide_end(player,dt)
if player.currentplatform == self then
player.currentplatform = nil
end
end

function MovingPlatform:update(dt,player)
local pre = { x = self.x, y = self.y }

Expand Down Expand Up @@ -143,7 +152,7 @@ function MovingPlatform:update(dt,player)
end

-- move the player along with the bounding box
if self.platform.player_touched then
if player.currentplatform == self then
player.position.x = player.position.x + ( self.x - pre.x )
player.position.y = player.position.y + ( self.y - pre.y )
player:moveBoundingBox()
Expand Down
20 changes: 18 additions & 2 deletions src/nodes/wall.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ function Wall.new(node, collider)
end

function Wall:collide(player, dt, mtv_x, mtv_y)
player.wall_collision = true
if player.state == player.crouch_state and mtv_y < player.bbox_height / 2 then
return
end

if mtv_x ~= 0 then
player.velocity.x = 0
player.position.x = player.position.x + mtv_x
end
end

if mtv_y > player.bbox_height / 2 - 5 then
--player standing up from crouch
player.state = player.crouch_state
player.position.x = player.position.x + ( 5 * ( player.direction == 'right' and 1 or -1 ) )
return
end

if mtv_y > 0 then
if mtv_y > 0 and mtv_y < player.bbox_height / 2 - 5 then
player.velocity.y = 0
player.position.y = player.position.y + mtv_y
end
Expand All @@ -30,5 +42,9 @@ function Wall:collide(player, dt, mtv_x, mtv_y)
end
end

function Wall:collide_end(player,dt)
player.wall_collision = false
end

return Wall

4 changes: 3 additions & 1 deletion src/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function Player.new(collider)
plyr.flash = false
plyr.width = 48
plyr.height = 48
plyr.bbox_width = 18
plyr.bbox_height = 44
plyr.sheet = nil
plyr.actions = {}
plyr.position = {x=0, y=0}
Expand All @@ -58,7 +60,7 @@ function Player.new(collider)
plyr.holdable = nil

plyr.collider = collider
plyr.bb = collider:addRectangle(0,0,18,44)
plyr.bb = collider:addRectangle(0,0,plyr.bbox_width,plyr.bbox_height)
plyr:moveBoundingBox()
plyr.bb.player = plyr -- wat

Expand Down
100 changes: 53 additions & 47 deletions src/vendor/hardoncollider/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,36 @@ function HC:setCallbacks(collide, stop)
return self
end

local function new_shape(self, shape)
local x1,y1,x2,y2 = shape:bbox()
function HC:addShape(shape)
assert(shape.bbox and shape.collidesWith,
"Cannot add custom shape: Incompatible shape.")

self._current_shape_id = self._current_shape_id + 1
self._active_shapes[self._current_shape_id] = shape
self._shape_ids[shape] = self._current_shape_id
self._hash:insert(shape, {x=x1,y=y1}, {x=x2,y=y2})
self._hash:insert(shape, shape:bbox())
shape._groups = {}

local hash = self._hash
local move, rotate = shape.move, shape.rotate
function shape:move(...)
local x1,y1,x2,y2 = self:bbox()
move(self, ...)
local x3,y3,x4,y4 = self:bbox()
hash:update(self, {x=x1,y=y1}, {x=x2,y=y2}, {x=x3,y=y3}, {x=x4,y=y4})
end

function shape:rotate(...)
local x1,y1,x2,y2 = self:bbox()
rotate(self, ...)
local x3,y3,x4,y4 = self:bbox()
hash:update(self, {x=x1,y=y1}, {x=x2,y=y2}, {x=x3,y=y3}, {x=x4,y=y4})
local move, rotate,scale = shape.move, shape.rotate, shape.scale
for _, func in ipairs{'move', 'rotate', 'scale'} do
local old_func = shape[func]
shape[func] = function(self, ...)
local x1,y1,x2,y2 = self:bbox()
old_func(self, ...)
local x3,y3,x4,y4 = self:bbox()
hash:update(self, x1,y1, x2,y2, x3,y3, x4,y4)
end
end

function shape:neighbors()
local x1,y1, x2,y2 = self:bbox()
return pairs(hash:getNeighbors(self, {x=x1,y=y1}, {x=x2,y=y2}))
local neighbors = hash:inRange(self:bbox())
rawset(neighbors, self, nil)
return neighbors
end

function shape:_removeFromHash()
local x1,y1, x2,y2 = self:bbox()
hash:remove(shape, {x=x1,y=y1}, {x=x2,y=y2})
return hash:remove(shape, self:bbox())
end

return shape
Expand All @@ -132,20 +129,24 @@ function HC:activeShapes()
end
end

function HC:shapesInRange(x1,y1, x2,y2)
return self._hash:inRange(x1,y1, x2,y2)
end

function HC:addPolygon(...)
return new_shape(self, newPolygonShape(...))
return self:addShape(newPolygonShape(...))
end

function HC:addRectangle(x,y,w,h)
return self:addPolygon(x,y, x+w,y, x+w,y+h, x,y+h)
end

function HC:addCircle(cx, cy, radius)
return new_shape(self, newCircleShape(cx,cy, radius))
return self:addShape(newCircleShape(cx,cy, radius))
end

function HC:addPoint(x,y)
return new_shape(self, newPointShape(x,y))
return self:addShape(newPointShape(x,y))
end

function HC:share_group(shape, other)
Expand All @@ -155,42 +156,47 @@ 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)
local i,k = self._shape_ids[s], self._shape_ids[t]
if i < k then i,k = k,i end
return string.format("%d,%d", i,k)
end

-- check for collisions
function HC:update(dt)
-- collect colliding shapes
-- cache for tested/colliding shapes
local tested, colliding = {}, {}
local function may_skip_test(shape, other)
return (shape == other)
or (tested[other] and tested[other][shape])
or self._ghost_shapes[other]
or self:share_group(shape, other)
end

-- collect colliding shapes
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
local collide, sx,sy = shape:collidesWith(other)
if collide then
colliding[id] = {shape, other, sx, sy}
end
tested[id] = true
tested[shape] = {}
for other in self._hash:rangeIter(shape:bbox()) do
if not may_skip_test(shape, other) then
local collide, sx,sy = shape:collidesWith(other)
if collide then
if not colliding[shape] then colliding[shape] = {} end
colliding[shape][other] = {sx, sy}
end
tested[shape][other] = true
end
end
end

-- call colliding callbacks on colliding shapes
for id,info in pairs(colliding) do
self._colliding_last_frame[id] = nil
self.on_collide( dt, unpack(info) )
for a, reg in pairs(colliding) do
for b, info in pairs(reg) do
if self._colliding_last_frame[a] then
self._colliding_last_frame[a][b] = nil
end
self.on_collide(dt, a, b, info[1], info[2])
end
end

-- call stop callback on shapes that do not collide anymore
for _,info in pairs(self._colliding_last_frame) do
self.on_stop( dt, unpack(info) )
for a,reg in pairs(self._colliding_last_frame) do
for b, info in pairs(reg) do
self.on_stop(dt, a, b, info[1], info[2])
end
end

self._colliding_last_frame = colliding
Expand All @@ -199,7 +205,7 @@ end
-- get list of shapes at point (x,y)
function HC:shapesAt(x, y)
local shapes = {}
for s in pairs(self._hash:cell{x=x,y=y}) do
for s in pairs(self._hash:cellAt(x,y)) do
if s:contains(x,y) then
shapes[#shapes+1] = s
end
Expand Down
Loading

0 comments on commit 84463db

Please sign in to comment.