Skip to content

Instantly share code, notes, and snippets.

@sysl-dev
Created March 24, 2021 23:23
Show Gist options
  • Save sysl-dev/e4c7afe104208bd08bf1b68ff15ab70a to your computer and use it in GitHub Desktop.
Save sysl-dev/e4c7afe104208bd08bf1b68ff15ab70a to your computer and use it in GitHub Desktop.
local eb_move_adj = 1.1
local playerFilter = function(item, other)
if other.isCoin then return 'cross'
elseif other.isWall then return 'slide'
elseif other.isExit then return 'touch'
elseif other.iswater then return 'cross'
else return 'slide'
end -- else return nil
end
function m.move(self, move_x, move_y)
-- This moves the player with the controller
local final_x, final_y, collisions, collision_count = self.world:move(self.collision, self.x + move_x,self.y + move_y, playerFilter)
--[[
for i=1,collision_count do
print('collided with ' .. tostring(collisions[i].other.name))
end
]]--
-- Let's wiggle around corners so movement does not suck. This only checks if we're walking into a wall.
-- Sensors check if there's something near the player
if self.x == final_x and self.y == final_y and (move_x ~= 0 or move_y ~= 0) then
-- Anything that happens here is when we walk into collision.
if not (move_x ~= 0 and move_y ~= 0) then
-- Moving Right
if move_x > 0 then
local _, len = self.world:queryPoint(self.x+15,self.y)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, self.x,self.y - eb_move_adj)
end
--print(len)
local _, len = self.world:queryPoint(self.x+15,self.y+14)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, final_x,final_y + eb_move_adj)
end
--print(len)
end
-- Moving Left
if move_x < 0 then
local _, len = self.world:queryPoint(self.x-1,self.y)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, self.x,self.y - eb_move_adj)
end
--print(len)
local _, len = self.world:queryPoint(self.x-1,self.y+14)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, final_x,final_y + eb_move_adj)
end
--print(len)
end
-- Moving Down
if move_y > 0 then
local _, len = self.world:queryPoint(self.x,self.y+15)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, self.x - eb_move_adj,self.y)
end
--print(len)
local _, len = self.world:queryPoint(self.x+14,self.y+15)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, final_x + eb_move_adj, final_y)
end
--print(len)
end
-- Moving Up
if move_y < 0 then
local _, len = self.world:queryPoint(self.x,self.y-1)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, self.x - eb_move_adj,self.y)
end
--print(len)
local _, len = self.world:queryPoint(self.x+14,self.y-1)
if len < 1 then
final_x, final_y, collisions, len = self.world:move(self.collision, final_x + eb_move_adj, final_y)
end
--print(len)
end
end
end
-- update the player's position after processing.
self.x = final_x
self.y = final_y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment