gdritter repos animaltransiro / c6568f2
Two-layer tiles; tiles loaded from Lua; basic interaction Getty Ritter 6 years ago
12 changed file(s) with 137 addition(s) and 19 deletion(s). Collapse all Expand all
55 Board.__index = Board
66
77 function Board:new()
8 local t = { }
8 local t = {
9 base = {},
10 entity = {},
11 }
912 for x = 0, consts.boardWidth do
1013 for y = 0, consts.boardHeight do
11 t[x * consts.boardWidth + y] = tile.getTile('grass')
14 t.base[x * consts.boardWidth + y] = tile.getTile('grass')
15 t.entity[x * consts.boardWidth + y] = nil
1216 end
1317 end
1418 return setmetatable(t, Board)
1519 end
1620
1721 function Board:lookup(x, y)
18 return self[x * consts.boardWidth + y]
22 return self.base[x * consts.boardWidth + y]
1923 end
2024
2125 function Board:set(x, y, r)
22 self[x * consts.boardWidth + y] = r
26 self.base[x * consts.boardWidth + y] = r
27 end
28
29 function Board:lookupEntity(x, y)
30 return self.entity[x * consts.boardWidth + y]
31 end
32
33 function Board:setEntity(x, y, r)
34 self.entity[x * consts.boardWidth + y] = r
35 end
36
37 function Board:passable(x, y)
38 local t, e = self:lookup(x, y), self:lookupEntity(x, y)
39 if e then
40 return t and t.pass and e.pass
41 else
42 return t and t.pass
43 end
2344 end
2445
2546 return {
1111 dx = 0,
1212 dy = 0,
1313 sprite = s,
14 direction = {0, 1},
1415 }
1516 return setmetatable(t, Entity)
1617 end
2122 local alignX = ((self.x + dx) % consts.tileSize) == 0
2223 local alignY = ((self.y + dy) % consts.tileSize) == 0
2324
24 local a = self.board:lookup(bx, by)
25 local b = self.board:lookup(bx + 1, by)
26 local c = self.board:lookup(bx, by + 1)
27 local d = self.board:lookup(bx + 1, by + 1)
25 local a = self.board:passable(bx, by)
26 local b = self.board:passable(bx + 1, by)
27 local c = self.board:passable(bx, by + 1)
28 local d = self.board:passable(bx + 1, by + 1)
2829
2930 if not (a and b and c and d) then return false end
3031
3132 if alignX and alignY then
32 return a.pass
33 return a
3334 elseif alignX then
34 return a.pass and c.pass
35 return a and c
3536 elseif alignY then
36 return a.pass and b.pass
37 return a and b
3738 else
38 return a.pass and b.pass and c.pass and d.pass
39 return a and b and c and d
3940 end
4041
4142 end
4243
44 function Entity:getFocus(board)
45 local bx = math.floor(self.x / consts.tileSize)
46 local by = math.floor(self.y / consts.tileSize)
47
48 return board:lookupEntity(bx + self.direction[1], by + self.direction[2])
49 end
50
4351 function Entity:move(board)
4452 local movement = true
53
54 if self.dx > 0 then
55 self.direction[1] = 2
56 elseif self.dx < 0 then
57 self.direction[1] = -1
58 elseif self.dy ~= 0 then
59 self.direction[1] = 0
60 end
61
62 if self.dy > 0 then
63 self.direction[2] = 2
64 elseif self.dy < 0 then
65 self.direction[2] = -1
66 elseif self.dx ~= 0 then
67 self.direction[2] = 0
68 end
69
4570 if self:checkCollision(self.dx, self.dy) then
4671 self.x = self.x + self.dx
4772 self.y = self.y + self.dy
7499 end
75100 end
76101 end
77 print(self.x, self.y)
78102 end
79103
80104 function Entity:draw()
1616 function keys.released.s(s) s.char.dy = 0 end
1717 function keys.released.d(s) s.char.dx = 0 end
1818
19 function keys.pressed.e(s)
20 local tgt = s.char:getFocus(s.board)
21 if tgt then
22 tgt:action()
23 end
24 end
25
1926 return {
2027 keys = keys
2128 }
66 local state = {}
77
88 local sprites = {}
9
10 function fromScreen(x, y)
11 return math.floor(x / consts.tileSize), math.floor(y / consts.tileSize)
12 end
913
1014 function love.load()
1115 state.t = 0
1721 state.board:set(2, 5, tile.getTile('water'))
1822
1923 state.board:set(8, 3, tile.getTile('water'))
24 state.board:set(9, 3, tile.getTile('stonepath'))
2025 state.board:set(8, 4, tile.getTile('water'))
2126 state.board:set(8, 5, tile.getTile('water'))
27 state.board:setEntity(9, 6, tile.getTile('sign'))
2228 end
2329
2430 function love.update()
2531 state.char:move()
32 end
33
34 function love.mousepressed(x, y)
35 local gx, gy = fromScreen(x, y)
36 state.board:set(gx, gy, tile.getTile('stonepath'))
2637 end
2738
2839 function love.keypressed(key)
4960 end
5061 end
5162
63 for x = 0, consts.boardWidth, 1 do
64 for y = 0, consts.boardHeight, 1 do
65 local e = state.board:lookupEntity(x, y)
66 if e then e:draw(x, y) end
67 end
68 end
69
5270 state.char:draw()
5371 end
33 local Tile = {}
44 Tile.__index = Tile
55
6 local spritesheet
7
68 function Tile:new(name)
7 local t = {
8 name = name,
9 sprite = love.graphics.newImage('tiles/' .. name .. '.png'),
10 pass = name == 'grass'
11 }
9 if not spritesheet then
10 spritesheet = love.graphics.newImage('tiles/spritesheet.png')
11 end
12
13 local f = io.open('tiles/' .. name .. '.lua')
14 local t
15 if f then
16 t = loadstring('return' .. f:read('*all'))()
17 f:close()
18 t.quad = love.graphics.newQuad(
19 t.spriteX * consts.tileSize,
20 t.spriteY * consts.tileSize,
21 consts.tileSize,
22 consts.tileSize,
23 spritesheet:getDimensions())
24 else
25 t = {
26 name = name,
27 sprite = love.graphics.newImage('tiles/' .. name .. '.png'),
28 pass = name == 'grass',
29 draw = function(self, x, y)
30 love.graphics.draw(self.sprite, x * consts.tileSize, y * consts.tileSize)
31 end
32 }
33 end
1234 return setmetatable(t, Tile)
1335 end
1436
1638 end
1739
1840 function Tile:draw(x, y)
19 love.graphics.draw(self.sprite,
41 love.graphics.draw(spritesheet,
42 self.quad,
2043 x * consts.tileSize,
2144 y * consts.tileSize)
2245 end
1 {
2 pass = true,
3 name = 'grass',
4 spriteX = 0,
5 spriteY = 0,
6 }
tiles/grass.png less more
Binary diff not shown
1 {
2 pass = false,
3 name = 'sign',
4 spriteX = 3,
5 spriteY = 0,
6 action = function() print('A sign!') end,
7 }
Binary diff not shown
Binary diff not shown
1 {
2 pass = true,
3 name = 'stonepath',
4 spriteX = 1,
5 spriteY = 0,
6 }
1 {
2 pass = false,
3 name = 'water',
4 spriteX = 5,
5 spriteY = 0,
6 }