gdritter repos animaltransiro / f9b018e
Collision and some movement experiments Getty Ritter 6 years ago
3 changed file(s) with 47 addition(s) and 26 deletion(s). Collapse all Expand all
33
44 local Board = {}
55 Board.__index = Board
6
76
87 function Board:new()
98 local t = { }
33 local tile = require 'tile'
44
55 local state = {
6 keys = {
7 w = false,
8 a = false,
9 s = false,
10 d = false,
11 },
6 t = 0,
127 char = {
138 x = 60,
149 y = 60,
10 dx = 0,
11 dy = 0,
1512 },
1613 board = board.Board:new(),
1714 }
2118 state.board:set(2, 4, tile.getTile('water'))
2219 state.board:set(2, 5, tile.getTile('water'))
2320
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
2425 function love.load()
2526 end
2627
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
2737 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 state.char.x = state.char.x + consts.speed
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
3960 end
4061 end
4162
4364 if key == 'q' then
4465 love.event.quit()
4566 elseif key == 'w' or key == 'up' then
46 state.keys.w = true
67 state.char.dy = -consts.speed
4768 elseif key == 'a' or key == 'left' then
48 state.keys.a = true
69 state.char.dx = -consts.speed
4970 elseif key == 's' or key == 'down' then
50 state.keys.s = true
71 state.char.dy = consts.speed
5172 elseif key == 'd' or key == 'right' then
52 state.keys.d = true
73 state.char.dx = consts.speed
5374 end
5475 end
5576
5677 function love.keyreleased(key)
5778 if key == 'w' or key == 'up' then
58 state.keys.w = false
79 state.char.dy = 0
5980 elseif key == 'a' or key == 'left' then
60 state.keys.a = false
81 state.char.dx = 0
6182 elseif key == 's' or key == 'down' then
62 state.keys.s = false
83 state.char.dy = 0
6384 elseif key == 'd' or key == 'right' then
64 state.keys.d = false
85 state.char.dx = 0
6586 end
6687 end
6788
55 local t = {
66 name = name,
77 sprite = love.graphics.newImage('tiles/' .. name .. '.png'),
8 pass = name == 'grass'
89 }
910 return setmetatable(t, Tile)
1011 end