1 | 1 |
local board = require 'board'
|
2 | 2 |
local consts = require 'constants'
|
| 3 |
local entity = require 'entity'
|
3 | 4 |
local tile = require 'tile'
|
| 5 |
local state = {}
|
4 | 6 |
|
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 | |
}
|
15 | 7 |
local sprites = {}
|
16 | 8 |
|
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'))
|
20 | 13 |
|
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'))
|
24 | 17 |
|
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'))
|
35 | 21 |
end
|
36 | 22 |
|
37 | 23 |
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()
|
70 | 25 |
end
|
71 | 26 |
|
72 | 27 |
function love.keypressed(key)
|
|
108 | 63 |
state.board:lookup(x, y):draw(x, y)
|
109 | 64 |
end
|
110 | 65 |
end
|
111 | |
tile.getTile('character'):drawPx(state.char.x, state.char.y)
|
| 66 |
state.char:draw()
|
112 | 67 |
end
|