Avoid getting stuck when moving along a wall
Getty Ritter
8 years ago
| 18 | 18 | function Entity:checkCollision(dx, dy) |
| 19 | 19 | local bx = math.floor((self.x + dx) / consts.tileSize) |
| 20 | 20 | 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 | |
| 25 | 29 | end |
| 26 | 30 | |
| 27 | 31 | function Entity:move(board) |
| 40 | 44 | if movement then |
| 41 | 45 | if self.dx ~= 0 and self.dy ~= 0 then |
| 42 | 46 | -- nothing |
| 47 | ||
| 43 | 48 | elseif self.dy ~= 0 then |
| 44 | 49 | local xm = self.x % 10 |
| 45 |
if 0 < xm and xm < 5 |
|
| 50 | if 0 < xm and xm < 5 and self:checkCollision(-1, 0) then | |
| 46 | 51 | self.x = self.x - 1 |
| 47 |
elseif xm >= 5 |
|
| 52 | elseif xm >= 5 and self:checkCollision(1, 0) then | |
| 48 | 53 | self.x = self.x + 1 |
| 49 | 54 | end |
| 55 | ||
| 50 | 56 | elseif self.dx ~= 0 then |
| 51 | 57 | local ym = self.y % 10 |
| 52 |
if 0 < ym and ym < 5 |
|
| 58 | if 0 < ym and ym < 5 and self:checkCollision(0, -1) then | |
| 53 | 59 | self.y = self.y - 1 |
| 54 |
elseif ym >= 5 |
|
| 60 | elseif ym >= 5 and self:checkCollision(0, 1) then | |
| 55 | 61 | self.y = self.y + 1 |
| 56 | 62 | end |
| 57 | 63 | end |