Collision and some movement experiments
Getty Ritter
7 years ago
3 | 3 | local tile = require 'tile' |
4 | 4 | |
5 | 5 | local state = { |
6 | keys = { | |
7 | w = false, | |
8 | a = false, | |
9 | s = false, | |
10 | d = false, | |
11 | }, | |
6 | t = 0, | |
12 | 7 | char = { |
13 | 8 | x = 60, |
14 | 9 | y = 60, |
10 | dx = 0, | |
11 | dy = 0, | |
15 | 12 | }, |
16 | 13 | board = board.Board:new(), |
17 | 14 | } |
21 | 18 | state.board:set(2, 4, tile.getTile('water')) |
22 | 19 | state.board:set(2, 5, tile.getTile('water')) |
23 | 20 | |
21 | state.board:set(8, 3, tile.getTile('water')) | |
22 | state.board:set(8, 4, tile.getTile('water')) | |
23 | state.board:set(8, 5, tile.getTile('water')) | |
24 | ||
24 | 25 | function love.load() |
25 | 26 | end |
26 | 27 | |
28 | function check_collision() | |
29 | local bx = math.floor((state.char.x + state.char.dx) / consts.tileSize) | |
30 | local by = math.floor((state.char.y + state.char.dy) / consts.tileSize) | |
31 | return state.board:lookup(bx, by).pass and | |
32 | state.board:lookup(bx + 1, by).pass and | |
33 | state.board:lookup(bx, by + 1).pass and | |
34 | state.board:lookup(bx + 1, by + 1).pass | |
35 | end | |
36 | ||
27 | 37 | function love.update() |
28 | if state.keys.w then | |
29 | state.char.y = state.char.y - consts.speed | |
30 | end | |
31 | if state.keys.s then | |
32 | state.char.y = state.char.y + consts.speed | |
33 | end | |
34 | if state.keys.a then | |
35 | state.char.x = state.char.x - consts.speed | |
36 | end | |
37 | if state.keys.d then | |
38 |
|
|
38 | state.t = (state.t + 1) % 10 | |
39 | if check_collision() then | |
40 | state.char.y = state.char.y + state.char.dy | |
41 | state.char.x = state.char.x + state.char.dx | |
42 | ||
43 | if state.char.dx ~= 0 and state.char.dx ~= 0 then | |
44 | -- nothing | |
45 | elseif state.char.dy ~= 0 then | |
46 | local xm = state.char.x % 10 | |
47 | if 0 < xm and xm < 5 then | |
48 | state.char.x = state.char.x - 1 | |
49 | elseif xm >= 5 then | |
50 | state.char.x = state.char.x + 1 | |
51 | end | |
52 | elseif state.char.dx ~= 0 then | |
53 | local ym = state.char.y % 10 | |
54 | if 0 < ym and ym < 5 then | |
55 | state.char.y = state.char.y - 1 | |
56 | elseif ym >= 5 then | |
57 | state.char.y = state.char.y + 1 | |
58 | end | |
59 | end | |
39 | 60 | end |
40 | 61 | end |
41 | 62 | |
43 | 64 | if key == 'q' then |
44 | 65 | love.event.quit() |
45 | 66 | elseif key == 'w' or key == 'up' then |
46 |
state. |
|
67 | state.char.dy = -consts.speed | |
47 | 68 | elseif key == 'a' or key == 'left' then |
48 |
state. |
|
69 | state.char.dx = -consts.speed | |
49 | 70 | elseif key == 's' or key == 'down' then |
50 |
state. |
|
71 | state.char.dy = consts.speed | |
51 | 72 | elseif key == 'd' or key == 'right' then |
52 |
state. |
|
73 | state.char.dx = consts.speed | |
53 | 74 | end |
54 | 75 | end |
55 | 76 | |
56 | 77 | function love.keyreleased(key) |
57 | 78 | if key == 'w' or key == 'up' then |
58 |
state. |
|
79 | state.char.dy = 0 | |
59 | 80 | elseif key == 'a' or key == 'left' then |
60 |
state. |
|
81 | state.char.dx = 0 | |
61 | 82 | elseif key == 's' or key == 'down' then |
62 |
state. |
|
83 | state.char.dy = 0 | |
63 | 84 | elseif key == 'd' or key == 'right' then |
64 |
state. |
|
85 | state.char.dx = 0 | |
65 | 86 | end |
66 | 87 | end |
67 | 88 |