gdritter repos animaltransiro / 6080844
Splitting things apart a bit Getty Ritter 7 years ago
4 changed file(s) with 80 addition(s) and 15 deletion(s). Collapse all Expand all
1 local consts = require 'constants'
2 local tile = require 'tile'
3
4 local Board = {}
5 Board.__index = Board
6
7
8 function Board:new()
9 local t = { }
10 for x = 0, consts.boardWidth do
11 for y = 0, consts.boardHeight do
12 t[x * consts.boardWidth + y] = tile.getTile('grass')
13 end
14 end
15 return setmetatable(t, Board)
16 end
17
18 function Board:lookup(x, y)
19 return self[x * consts.boardWidth + y]
20 end
21
22 function Board:set(x, y, r)
23 self[x * consts.boardWidth + y] = r
24 end
25
26 return {
27 Board = Board
28 }
1 return {
2 speed = 2,
3 tileSize = 20,
4 boardWidth = 39,
5 boardHeight = 29
6 }
1 local board = require 'board'
2 local consts = require 'constants'
3 local tile = require 'tile'
4
15 local state = {
26 keys = {
37 w = false,
913 x = 60,
1014 y = 60,
1115 },
12 }
13 local consts = {
14 speed = 2,
15 tileSize = 20,
16 board = board.Board:new(),
1617 }
1718 local sprites = {}
1819
20 state.board:set(2, 3, tile.getTile('water'))
21 state.board:set(2, 4, tile.getTile('water'))
22 state.board:set(2, 5, tile.getTile('water'))
23
1924 function love.load()
20 sprites['character'] = love.graphics.newImage('tiles/character.png')
21 sprites['water'] = love.graphics.newImage('tiles/water.png')
22 sprites['grass'] = love.graphics.newImage('tiles/grass.png')
2325 end
2426
2527 function love.update()
7072 love.graphics.getHeight())
7173
7274 love.graphics.setColor(255, 255, 255)
73 for x = 0, 39, 1 do
74 for y = 0, 29, 1 do
75 love.graphics.draw(sprites.grass,
76 x * 20,
77 y * 20)
75
76 for x = 0, consts.boardWidth, 1 do
77 for y = 0, consts.boardHeight, 1 do
78 state.board:lookup(x, y):draw(x, y)
7879 end
7980 end
80 love.graphics.draw(sprites.character,
81 state.char.x,
82 state.char.y)
81 tile.getTile('character'):drawPx(state.char.x, state.char.y)
8382 end
1 local Tile = {}
2 Tile.__index = Tile
3
4 function Tile:new(name)
5 local t = {
6 name = name,
7 sprite = love.graphics.newImage('tiles/' .. name .. '.png'),
8 }
9 return setmetatable(t, Tile)
10 end
11
12 function Tile:action()
13 end
14
15 function Tile:draw(x, y)
16 love.graphics.draw(self.sprite, x * 20, y * 20)
17 end
18
19 function Tile:drawPx(x, y)
20 love.graphics.draw(self.sprite, x, y)
21 end
22
23 local tiles = {}
24
25 function getTile(name)
26 tiles[name] = tiles[name] or Tile:new(name)
27 return tiles[name]
28 end
29
30 return {
31 getTile = getTile
32 }