-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilemap.lua
More file actions
132 lines (120 loc) · 3.77 KB
/
tilemap.lua
File metadata and controls
132 lines (120 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
TileMap = class("TileMap")
local function parseMap(self)
local map = self.mapLayout
local tiles = {}
local rows = #map
local columns = #map[1]
self.spriteBatch:bind()
for yIndex = 1, rows do
tiles[yIndex] = {}
for xIndex = 1, columns do
local tile = Tile.new(map[yIndex][xIndex])
self.spriteBatch:add(Tile.quads[map[yIndex][xIndex]], (xIndex-1)*self.tileSize, (yIndex-1)*self.tileSize) --should be addq in final version
tiles[yIndex][xIndex] = tile
end
end
self.spriteBatch:unbind()
return tiles
end
function TileMap:initialize(mapLayout, image, tileSize)
self.mapLayout = mapLayout
self.image = image
self.tileSize = tileSize or 32
end
function TileMap:load()
self.spriteBatch = love.graphics.newSpriteBatch(self.image)
self.tiles = parseMap(self)
end
--to be called only on solid tiles, or else it will add an unnecessary AABB
--might want to move this to tile class to abstract that away
local function isColliding(self, tile, xIndex, yIndex, boundingBox)
if not tile.solid then
return false
end
if tile.box == nil then
tile:addBoundingBox(Vector:new((xIndex - 1) * self.tileSize, (yIndex - 1) * self.tileSize), self.tileSize)
end
return boundingBox:isColliding(tile.box)
end
function TileMap:getTile(xIndex, yIndex)
if self.tiles[yIndex] and self.tiles[yIndex][xIndex] then
return self.tiles[yIndex][xIndex]
else
return nil
end
end
--TODO: make facingright not a bool (split function up)
function TileMap:horizontalCheck(boundingBox, facingRight)
local finished = false
local rightEdge = math.floor((boundingBox.pos.x + boundingBox.width)/self.tileSize) + 1
local rightLimit = rightEdge + 2
local topRow = math.floor((boundingBox.pos.y)/self.tileSize) + 1
local botRow = topRow + math.floor(boundingBox.height-1/self.tileSize)
local collidingTiles = {}
if facingRight then
--look right up to 3
for col = rightEdge, rightLimit do
if finished then break end
for row = topRow, botRow do
local tile = self:getTile(col, row)
if tile and isColliding(self, tile, col, row, boundingBox) then
table.insert(collidingTiles, tile)
finished = true
end
end
end
else
--We make it one less so that it doesn't treat block below player
--as a collision block
local leftEdge = math.floor((boundingBox.pos.x-1)/self.tileSize) + 1
local leftLimit = leftEdge - 2
for col = leftEdge, leftLimit, -1 do
if finished then break end
for row = topRow, botRow do
local tile = self:getTile(col, row)
if tile and isColliding(self, tile, col, row, boundingBox) then
table.insert(collidingTiles, tile)
finished = true
end
end
end
end
return collidingTiles
end
function TileMap:verticalCheck(boundingBox, goingUp)
local finished = false
local botEdge = math.floor((boundingBox.pos.y + boundingBox.height)/self.tileSize) + 1
local botLimit = botEdge + 2
local leftEdge = math.floor((boundingBox.pos.x)/self.tileSize) + 1
local rightEdge = leftEdge + math.floor(boundingBox.width/self.tileSize)
local collidingTiles = {}
if (not goingUp) then
for row = botEdge, botLimit do
if finished then break end
for col = leftEdge, rightEdge do
local tile = self:getTile(col, row)
if tile and isColliding(self, tile, col, row, boundingBox) then
table.insert(collidingTiles, tile)
finished = true
end
end
end
else
local topEdge = math.floor((boundingBox.pos.y)/self.tileSize) + 1
local topLimit = topEdge - 2
for row = topEdge, topLimit, -1 do
if finished then break end
for col = leftEdge, rightEdge do
local tile = self:getTile(col, row)
if tile and isColliding(self, tile, col, row, boundingBox) then
table.insert(collidingTiles, tile)
finished = true
end
end
end
end
return collidingTiles
end
function TileMap:draw()
love.graphics.draw(self.spriteBatch)
end