Refine movement strategies
Getty Ritter
8 years ago
| 25 | 25 | function love.load() |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | function check_collision() | |
| 29 | local bx = math.floor((state.char.x + state.char.dx) / consts.tileSize) | |
| 30 |
|
|
| 28 | function check_collision(thing, dx, dy) | |
| 29 | local bx = math.floor((thing.x + dx) / consts.tileSize) | |
| 30 | local by = math.floor((thing.y + dy) / consts.tileSize) | |
| 31 | 31 | return state.board:lookup(bx, by).pass and |
| 32 | 32 | state.board:lookup(bx + 1, by).pass and |
| 33 | 33 | state.board:lookup(bx, by + 1).pass and |
| 35 | 35 | end |
| 36 | 36 | |
| 37 | 37 | function love.update() |
| 38 | local movement = true | |
| 38 | 39 | state.t = (state.t + 1) % 10 |
| 39 |
if check_collision( |
|
| 40 | if check_collision(state.char, state.char.dx, state.char.dy) then | |
| 41 | state.char.x = state.char.x + state.char.dx | |
| 40 | 42 | state.char.y = state.char.y + state.char.dy |
| 43 | elseif check_collision(state.char, state.char.dx, 0) then | |
| 41 | 44 | state.char.x = state.char.x + state.char.dx |
| 45 | elseif check_collision(state.char, 0, state.char.dy) then | |
| 46 | state.char.y = state.char.y + state.char.dy | |
| 47 | else | |
| 48 | movement = false | |
| 49 | end | |
| 42 | 50 | |
| 43 |
|
|
| 51 | if movement then | |
| 52 | if state.char.dx ~= 0 and state.char.dy ~= 0 then | |
| 44 | 53 | -- nothing |
| 45 | 54 | elseif state.char.dy ~= 0 then |
| 46 | 55 | local xm = state.char.x % 10 |