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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
local Entity = require("entity")
local Liquid = require("liquid")
local LiquidSurface = require("liquidSurface")
local Tilemap = {}
Tilemap.__index = Tilemap
local function loadMapData(mapPath)
if type(mapPath) ~= "string" then
return mapPath
end
if love and love.filesystem and love.filesystem.load then
local chunk, err = love.filesystem.load(mapPath)
if not chunk then
error("Tilemap: failed to load '" .. tostring(mapPath) .. "': " .. tostring(err))
end
return chunk()
end
local mod = mapPath:gsub("%.lua$", ""):gsub("/", ".")
local ok, data = pcall(require, mod)
if not ok then
error("Tilemap: failed to load '" .. tostring(mapPath) .. "': " .. tostring(data))
end
return data
end
local function objectToEntity(obj)
local x = obj.x or 0
local y = obj.y or 0
local w = obj.width or 0
local h = obj.height or 0
local entity = Entity:new(x, y, w, h)
entity:setPropertiesFromOptions({
properties = obj.properties or {},
name = obj.name,
type = obj.type,
id = obj.id,
rotation = obj.rotation,
visible = obj.visible,
shape = obj.shape
})
entity.polygon = obj.polygon
entity.polyline = obj.polyline
return entity
end
function Tilemap:new(mapPath, tilesets)
local self = setmetatable({}, Tilemap)
self.entitiesTiles = {}
self.entitiesSpawns = {}
self.entitiesCameraBorders = {}
self.entitiesLiquidPolygons = {}
self.entitiesLiquidSurfaces = {}
self.layerBackground = nil
self.layerGround = nil
self.layerForeground = nil
self.tileWidth = 8
self.tileHeight = 8
self.mapWidth = 0
self.mapHeight = 0
self.tileGidInfo = {}
self.layerTileDimensions = {}
local mapData = loadMapData(mapPath)
self.mapData = mapData
self.tileWidth = mapData and (mapData.tilewidth or 8) or 8
self.tileHeight = mapData and (mapData.tileheight or 8) or 8
self.mapWidth = (mapData and mapData.width or 0) * self.tileWidth
self.mapHeight = (mapData and mapData.height or 0) * self.tileHeight
if mapData and mapData.tilesets then
for _, ts in ipairs(mapData.tilesets) do
local info = tilesets[ts.name]
if info then
local ok, img = pcall(love.graphics.newImage, info.path)
if ok and img then
local tw = info.tilewidth or self.tileWidth
local th = info.tileheight or self.tileHeight
local imgW, imgH = img:getDimensions()
local cols = math.floor(imgW / tw)
local rows = math.floor(imgH / th)
for index = 0, (cols * rows) - 1 do
local qx = (index % cols) * tw
local qy = math.floor(index / cols) * th
local gid = ts.firstgid + index
local quad = love.graphics.newQuad(qx, qy, tw, th, imgW, imgH)
self.tileGidInfo[gid] = { image = img, quad = quad, tilewidth = tw, tileheight = th }
end
end
end
end
end
if mapData and mapData.layers then
for _, layer in ipairs(mapData.layers) do
if layer.type == "tilelayer" and layer.name then
local n = layer.name:lower()
if n == "background" then self.layerBackground = layer
elseif n == "ground" then self.layerGround = layer
elseif n == "foreground" then self.layerForeground = layer
end
local dims = nil
if layer.data then
for _, gid in ipairs(layer.data) do
if gid and gid ~= 0 then
local gi = self.tileGidInfo[gid]
if gi then
dims = { tilewidth = gi.tilewidth, tileheight = gi.tileheight }
break
end
end
end
end
self.layerTileDimensions[n] = dims or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
elseif layer.type == "objectgroup" and layer.objects then
local name = (layer.name or ""):gsub("%s+", "_"):lower()
for _, obj in ipairs(layer.objects) do
local entity = objectToEntity(obj)
if name == "tiles" or name == "tile" then
table.insert(self.entitiesTiles, entity)
elseif name == "spawn" then
table.insert(self.entitiesSpawns, entity)
elseif name == "camera_border" then
table.insert(self.entitiesCameraBorders, entity)
elseif name == "liquid" then
table.insert(self.entitiesLiquidPolygons, Liquid:new(entity))
elseif name == "liquid_surface" then
table.insert(self.entitiesLiquidSurfaces, LiquidSurface:new(entity))
end
end
end
end
end
return self
end
function Tilemap:getEntitiesLiquidPolygons()
return self.entitiesLiquidPolygons
end
function Tilemap:getEntitiesLiquidSurfaces()
return self.entitiesLiquidSurfaces
end
function Tilemap:getEntitiesTiles()
return self.entitiesTiles
end
function Tilemap:getEntitiesSpawns()
return self.entitiesSpawns
end
function Tilemap:getEntitiesCameraBorders()
return self.entitiesCameraBorders
end
function Tilemap:getCameraLimits()
local borders = self.entitiesCameraBorders
if not borders or #borders == 0 then
return nil, nil, nil, nil
end
local minX, maxX = math.huge, -math.huge
local minY, maxY = math.huge, -math.huge
for _, e in ipairs(borders) do
local x, y = e.x or 0, e.y or 0
local w, h = e.width or 0, e.height or 0
minX = math.min(minX, x)
maxX = math.max(maxX, x + w)
minY = math.min(minY, y)
maxY = math.max(maxY, y + h)
end
return minX, maxX, minY, maxY
end
function Tilemap:getMapData()
return self.mapData
end
function Tilemap:getBackgroundLayer()
return self.layerBackground
end
function Tilemap:getGroundLayer()
return self.layerGround
end
function Tilemap:getForegroundLayer()
return self.layerForeground
end
function Tilemap:getLayers()
return {
background = self.layerBackground,
ground = self.layerGround,
foreground = self.layerForeground
}
end
function Tilemap:getTileWidth()
return self.tileWidth
end
function Tilemap:getTileHeight()
return self.tileHeight
end
function Tilemap:getLayerTileDimensions(layerName)
return self.layerTileDimensions[layerName:lower()]
or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
end
function Tilemap:getMapWidth()
return self.mapWidth
end
function Tilemap:getMapHeight()
return self.mapHeight
end
function Tilemap:getTileGidInfo()
return self.tileGidInfo
end
return Tilemap
|