gdritter repos animaltransiro / 2a25787
Avoid getting stuck when moving along a wall Getty Ritter 6 years ago
1 changed file(s) with 14 addition(s) and 8 deletion(s). Collapse all Expand all
1818 function Entity:checkCollision(dx, dy)
1919 local bx = math.floor((self.x + dx) / consts.tileSize)
2020 local by = math.floor((self.y + dy) / consts.tileSize)
21 return self.board:lookup(bx, by).pass and
22 self.board:lookup(bx + 1, by).pass and
23 self.board:lookup(bx, by + 1).pass and
24 self.board:lookup(bx + 1, by + 1).pass
21
22 local a = self.board:lookup(bx, by)
23 local b = self.board:lookup(bx + 1, by)
24 local c = self.board:lookup(bx, by + 1)
25 local d = self.board:lookup(bx + 1, by + 1)
26
27 if not (a and b and c and d) then return false end
28 return a.pass and b.pass and c.pass and d.pass
2529 end
2630
2731 function Entity:move(board)
4044 if movement then
4145 if self.dx ~= 0 and self.dy ~= 0 then
4246 -- nothing
47
4348 elseif self.dy ~= 0 then
4449 local xm = self.x % 10
45 if 0 < xm and xm < 5 then
50 if 0 < xm and xm < 5 and self:checkCollision(-1, 0) then
4651 self.x = self.x - 1
47 elseif xm >= 5 then
52 elseif xm >= 5 and self:checkCollision(1, 0) then
4853 self.x = self.x + 1
4954 end
55
5056 elseif self.dx ~= 0 then
5157 local ym = self.y % 10
52 if 0 < ym and ym < 5 then
58 if 0 < ym and ym < 5 and self:checkCollision(0, -1) then
5359 self.y = self.y - 1
54 elseif ym >= 5 then
60 elseif ym >= 5 and self:checkCollision(0, 1) then
5561 self.y = self.y + 1
5662 end
5763 end