function collision(curValue as integer, amount as integer, direction as integer)
if direction = COLLISION_RIGHT
rem the character's bounding collision box
rem can lie within more than a single tile
rem unless completely aligned with the natural
rem grid of the map. This calculates the range
rem of tiles necessary to check for collision
x2 = min(((_Player.x - MAP_OFFSET_X) + amount + _Player.cx2) / _TileSize + 1, 16)
y1 = max(((_Player.y - MAP_OFFSET_Y) - _Player.cy2) / _TileSize + 1, 1)
y2 = min(((_Player.y - MAP_OFFSET_Y) - _Player.cy1) / _TileSize + 1, 11)
for iy = y1 to y2
rem check if this nearby tile is passable or not
if collisionMap(x2, iy) = STATE_IMPASSABLE OR (collisionMap(x2, iy) = STATE_WATER AND _ShowLadder = 1 AND (_WaterX <> x2 OR _WaterY <> iy))
x = (x2-1)*_TileSize + MAP_OFFSET_X
y = (iy-1)*_TileSize + MAP_OFFSET_Y
if _Player.y - _Player.cy2 < y+_TileSize and _Player.y - _Player.cy1 > y
rem there was a collision
if curValue+amount + _Player.cx2 > x
rem restore x coordinate
_Player.x = x - _Player.cx2
rem provides a small level of sliding when colliding
rem with an object. When the character's path becomes
rem obstructed because it only clipped the edge of an
rem impassable tile, it will gradually align to the
rem natural grid of the map, often allowing the character
rem to slide on past the obstruction.
i = (_Player.y - MAP_OFFSET_Y-8) / 32
r = (_Player.y - MAP_OFFSET_Y-8) - (i*32)
if r > 16 then _Player.y = _Player.y - 1
if r < 16 then _Player.y = _Player.y + 1
exitfunction 1
endif
endif
else
if collisionMap(x2, iy) = STATE_WATER
if _ShowLadder = 0
_ShowLadder = 1
_WaterX = x2
_WaterY = iy
endif
else
_ShowLadder = 0
endif
endif
next iy
rem there was no collision, increment position as normal
_Player.x = curValue + amount
exitfunction 0
endif
if direction = COLLISION_LEFT
x1 = min(((_Player.x - MAP_OFFSET_X) + amount + _Player.cx1) / _TileSize + 1, 16)
y1 = max(((_Player.y - MAP_OFFSET_Y) - _Player.cy2) / _TileSize + 1, 1)
y2 = min(((_Player.y - MAP_OFFSET_Y) - _Player.cy1) / _TileSize + 1, 11)
for iy = y1 to y2
if collisionMap(x1, iy) = STATE_IMPASSABLE OR (collisionMap(x1, iy) = STATE_WATER AND _ShowLadder = 1 AND (_WaterX <> x1 OR _WaterY <> iy))
x = (x1-1)*_TileSize + MAP_OFFSET_X
y = (iy-1)*_TileSize + MAP_OFFSET_Y
if _Player.y - _Player.cy2 < y+_TileSize and _Player.y - _Player.cy1 > y
if curValue+amount + _Player.cx1 < x + _TileSize
_Player.x = x + _TileSize - _Player.cx1
i = (_Player.y - MAP_OFFSET_Y-8) / 32
r = (_Player.y - MAP_OFFSET_Y-8) - (i*32)
if r > 16 then _Player.y = _Player.y - 1
if r < 16 then _Player.y = _Player.y + 1
exitfunction 1
endif
endif
BM Wakeupnow IBO