Splitting things apart a bit
Getty Ritter
7 years ago
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 | local board = require 'board' | |
2 | local consts = require 'constants' | |
3 | local tile = require 'tile' | |
4 | ||
1 | 5 | local state = { |
2 | 6 | keys = { |
3 | 7 | w = false, |
9 | 13 | x = 60, |
10 | 14 | y = 60, |
11 | 15 | }, |
12 | } | |
13 | local consts = { | |
14 | speed = 2, | |
15 | tileSize = 20, | |
16 | board = board.Board:new(), | |
16 | 17 | } |
17 | 18 | local sprites = {} |
18 | 19 | |
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 | ||
19 | 24 | 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') | |
23 | 25 | end |
24 | 26 | |
25 | 27 | function love.update() |
70 | 72 | love.graphics.getHeight()) |
71 | 73 | |
72 | 74 | 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) | |
78 | 79 | end |
79 | 80 | end |
80 | love.graphics.draw(sprites.character, | |
81 | state.char.x, | |
82 |
|
|
81 | tile.getTile('character'):drawPx(state.char.x, state.char.y) | |
83 | 82 | 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 | } |