Change tile size + fix tile-aligned movement
Getty Ritter
7 years ago
1 | 1 | return { |
2 | 2 | speed = 2, |
3 | tileSize = 20, | |
4 | boardWidth = 39, | |
5 |
|
|
3 | tileSize = 24, | |
4 | halfTile = 12, | |
5 | quarterTile = 6, | |
6 | boardWidth = 32, | |
7 | boardHeight = 24, | |
6 | 8 | } |
18 | 18 | function Entity:checkCollision(dx, dy) |
19 | 19 | local bx = math.floor((self.x + dx) / consts.tileSize) |
20 | 20 | local by = math.floor((self.y + dy) / consts.tileSize) |
21 | local alignX = ((self.x + dx) % consts.tileSize) == 0 | |
22 | local alignY = ((self.y + dy) % consts.tileSize) == 0 | |
21 | 23 | |
22 | 24 | local a = self.board:lookup(bx, by) |
23 | 25 | local b = self.board:lookup(bx + 1, by) |
25 | 27 | local d = self.board:lookup(bx + 1, by + 1) |
26 | 28 | |
27 | 29 | if not (a and b and c and d) then return false end |
28 | return a.pass and b.pass and c.pass and d.pass | |
30 | ||
31 | if alignX and alignY then | |
32 | return a.pass | |
33 | elseif alignX then | |
34 | return a.pass and c.pass | |
35 | elseif alignY then | |
36 | return a.pass and b.pass | |
37 | else | |
38 | return a.pass and b.pass and c.pass and d.pass | |
39 | end | |
40 | ||
29 | 41 | end |
30 | 42 | |
31 | 43 | function Entity:move(board) |
46 | 58 | -- nothing |
47 | 59 | |
48 | 60 | elseif self.dy ~= 0 then |
49 | local xm = self.x % 10 | |
50 | if 0 < xm and xm < 5 and self:checkCollision(-1, 0) then | |
61 | local xm = self.x % consts.halfTile | |
62 | if 0 < xm and xm < consts.quarterTile and self:checkCollision(-1, 0) then | |
51 | 63 | self.x = self.x - 1 |
52 |
elseif xm >= |
|
64 | elseif xm >= consts.quarterTile and self:checkCollision(1, 0) then | |
53 | 65 | self.x = self.x + 1 |
54 | 66 | end |
55 | 67 | |
56 | 68 | elseif self.dx ~= 0 then |
57 | local ym = self.y % 10 | |
58 | if 0 < ym and ym < 5 and self:checkCollision(0, -1) then | |
69 | local ym = self.y % consts.halfTile | |
70 | if 0 < ym and ym < consts.quarterTile and self:checkCollision(0, -1) then | |
59 | 71 | self.y = self.y - 1 |
60 |
elseif ym >= |
|
72 | elseif ym >= consts.quarterTile and self:checkCollision(0, 1) then | |
61 | 73 | self.y = self.y + 1 |
62 | 74 | end |
63 | 75 | end |
64 | 76 | end |
77 | print(self.x, self.y) | |
65 | 78 | end |
66 | 79 | |
67 | 80 | function Entity:draw() |
10 | 10 | function love.load() |
11 | 11 | state.t = 0 |
12 | 12 | state.board = board.Board:new() |
13 |
state.char = entity.Entity:new(state.board, |
|
13 | state.char = entity.Entity:new(state.board, 3 * 24, 3 * 24, tile.getTile('character')) | |
14 | 14 | |
15 | 15 | state.board:set(2, 3, tile.getTile('water')) |
16 | 16 | state.board:set(2, 4, tile.getTile('water')) |
48 | 48 | state.board:lookup(x, y):draw(x, y) |
49 | 49 | end |
50 | 50 | end |
51 | ||
51 | 52 | state.char:draw() |
52 | 53 | end |
1 | local consts = require 'constants' | |
2 | ||
1 | 3 | local Tile = {} |
2 | 4 | Tile.__index = Tile |
3 | 5 | |
14 | 16 | end |
15 | 17 | |
16 | 18 | function Tile:draw(x, y) |
17 |
love.graphics.draw(self.sprite, |
|
19 | love.graphics.draw(self.sprite, | |
20 | x * consts.tileSize, | |
21 | y * consts.tileSize) | |
18 | 22 | end |
19 | 23 | |
20 | 24 | function Tile:drawPx(x, y) |
Binary diff not shown
Binary diff not shown
Binary diff not shown