gdritter repos animaltransiro / b682204
Moved movement logic out of main into entity Getty Ritter 6 years ago
2 changed file(s) with 81 addition(s) and 59 deletion(s). Collapse all Expand all
1 local consts = require 'constants'
2
3 local Entity = {}
4 Entity.__index = Entity
5
6 function Entity:new(board, x, y, s)
7 local t = {
8 board = board,
9 x = x,
10 y = y,
11 dx = 0,
12 dy = 0,
13 sprite = s,
14 }
15 return setmetatable(t, Entity)
16 end
17
18 function Entity:checkCollision(dx, dy)
19 local bx = math.floor((self.x + dx) / consts.tileSize)
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
25 end
26
27 function Entity:move(board)
28 local movement = true
29 if self:checkCollision(self.dx, self.dy) then
30 self.x = self.x + self.dx
31 self.y = self.y + self.dy
32 elseif self:checkCollision(self.dx, 0) then
33 self.x = self.x + self.dx
34 elseif self:checkCollision(0, self.dy) then
35 self.y = self.y + self.dy
36 else
37 movement = false
38 end
39
40 if movement then
41 if self.dx ~= 0 and self.dy ~= 0 then
42 -- nothing
43 elseif self.dy ~= 0 then
44 local xm = self.x % 10
45 if 0 < xm and xm < 5 then
46 self.x = self.x - 1
47 elseif xm >= 5 then
48 self.x = self.x + 1
49 end
50 elseif self.dx ~= 0 then
51 local ym = self.y % 10
52 if 0 < ym and ym < 5 then
53 self.y = self.y - 1
54 elseif ym >= 5 then
55 self.y = self.y + 1
56 end
57 end
58 end
59 end
60
61 function Entity:draw()
62 self.sprite:drawPx(self.x, self.y)
63 end
64
65 return {
66 Entity = Entity
67 }
11 local board = require 'board'
22 local consts = require 'constants'
3 local entity = require 'entity'
34 local tile = require 'tile'
5 local state = {}
46
5 local state = {
6 t = 0,
7 char = {
8 x = 60,
9 y = 60,
10 dx = 0,
11 dy = 0,
12 },
13 board = board.Board:new(),
14 }
157 local sprites = {}
168
17 state.board:set(2, 3, tile.getTile('water'))
18 state.board:set(2, 4, tile.getTile('water'))
19 state.board:set(2, 5, tile.getTile('water'))
9 function love.load()
10 state.t = 0
11 state.board = board.Board:new()
12 state.char = entity.Entity:new(state.board, 60, 60, tile.getTile('character'))
2013
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'))
14 state.board:set(2, 3, tile.getTile('water'))
15 state.board:set(2, 4, tile.getTile('water'))
16 state.board:set(2, 5, tile.getTile('water'))
2417
25 function love.load()
26 end
27
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 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
18 state.board:set(8, 3, tile.getTile('water'))
19 state.board:set(8, 4, tile.getTile('water'))
20 state.board:set(8, 5, tile.getTile('water'))
3521 end
3622
3723 function love.update()
38 local movement = true
39 state.t = (state.t + 1) % 10
40 if check_collision(state.char, state.char.dx, state.char.dy) then
41 state.char.x = state.char.x + state.char.dx
42 state.char.y = state.char.y + state.char.dy
43 elseif check_collision(state.char, state.char.dx, 0) then
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
50
51 if movement then
52 if state.char.dx ~= 0 and state.char.dy ~= 0 then
53 -- nothing
54 elseif state.char.dy ~= 0 then
55 local xm = state.char.x % 10
56 if 0 < xm and xm < 5 then
57 state.char.x = state.char.x - 1
58 elseif xm >= 5 then
59 state.char.x = state.char.x + 1
60 end
61 elseif state.char.dx ~= 0 then
62 local ym = state.char.y % 10
63 if 0 < ym and ym < 5 then
64 state.char.y = state.char.y - 1
65 elseif ym >= 5 then
66 state.char.y = state.char.y + 1
67 end
68 end
69 end
24 state.char:move()
7025 end
7126
7227 function love.keypressed(key)
10863 state.board:lookup(x, y):draw(x, y)
10964 end
11065 end
111 tile.getTile('character'):drawPx(state.char.x, state.char.y)
66 state.char:draw()
11267 end