-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemapper.lua
319 lines (292 loc) · 8.12 KB
/
tilemapper.lua
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
-- tilemapper v0.0.5
-- Depends on:
-- - json.lua (https://github.com/rxi/json.lua)
-- - classic.lua (https://github.com/rxi/classic)
-- - bump.lua (https://github.com/kikito/bump.lua)
local json = require("lib.json")
local Tilemapper = Class:extend()
local Layer = Class:extend()
function Layer:new(tiles, name)
self.tiles = tiles
self.name = name
end
local TileLayer = Layer:extend()
function TileLayer:new(tiles, name, tileset, size, spacing, padding)
TileLayer.super.new(self, tiles, name)
self.type = "tile"
self.tileset = love.graphics.newImage(tileset)
self.size = size
local quadInfo = {}
for i = 1, #tiles do
local tile = tiles[i]
if not quadInfo[tile.t] then
quadInfo[tile.t] = tile.src
end
end
local quads = {}
for k, info in pairs(quadInfo) do
quads[k] = love.graphics.newQuad(
info[1], info[2], size, size, self.tileset:getWidth(),
self.tileset:getHeight())
end
self.quads = quads
end
local IntGrid = Layer:extend()
function IntGrid:new(tiles, name, size)
IntGrid.super.new(self, tiles, name)
self.type = "int"
self.size = size
end
local AutoLayer = Layer:extend()
function AutoLayer:new(tiles, name, tileset, size, spacing, padding)
AutoLayer.super.new(self, tiles, name)
self.type = "auto"
self.tileset = love.graphics.newImage(tileset)
self.size = size
local quadInfo = {}
for i = 1, #tiles do
local tile = tiles[i]
if not quadInfo[tile.t] then
quadInfo[tile.t] = tile.src
end
end
local quads = {}
for k, info in pairs(quadInfo) do
quads[k] = love.graphics.newQuad(
info[1], info[2], size, size, self.tileset:getWidth(),
self.tileset:getHeight())
end
self.quads = quads
end
local function getIntGrid(layer, _, options)
local width = layer.__cWid
local grid = layer.intGridCsv
local collisions = options and options.collisions or {}
local tiles = {}
local size = layer.__gridSize
for i = 0, #grid - 1 do
if grid[i + 1] > 0 then
local v = grid[i + 1]
if collisions[v] then
local y = math.floor(i / width)
local x = i - y * width
tiles[#tiles + 1] = {
x = x * size,
y = y * size,
v = v,
h = size,
w = size,
}
end
end
end
return IntGrid(tiles, layer.__identifier, size)
end
local function getAutoLayer(layer, root, options, tilesets)
local tilesetPath = root .. layer.__tilesetRelPath
local tileset = tilesets[layer.__tilesetDefUid]
if options and options.aseprite then
tilesetPath = tilesetPath:gsub("aseprite", "png")
end
return AutoLayer(
layer.autoLayerTiles, layer.__identifier, tilesetPath,
layer.__gridSize, tileset.spacing, tileset.padding)
end
local function getTileLayer(layer, root, options, tilesets)
local tilesetPath = root .. layer.__tilesetRelPath
local tileset = tilesets[layer.__tilesetDefUid]
if options and options.aseprite then
tilesetPath = tilesetPath:gsub("aseprite", "png")
end
return TileLayer(
layer.gridTiles, layer.__identifier, tilesetPath, layer.__gridSize,
tileset.spacing, tileset.padding)
end
local function getEntities(layer)
local instances = layer.entityInstances
local entities = {}
for i = 1, #instances do
local entity = instances[i]
if not entities[entity.__identifier] then
entities[entity.__identifier] = {}
end
local entity_obj = {
x = entity.px[1],
y = entity.px[2],
w = entity.width,
h = entity.height,
}
local _fields = entity.fieldInstances
for j = 1, #_fields do
local field = _fields[j]
entity_obj[field.__identifier] = field.__value
end
entities[entity.__identifier][#entities[entity.__identifier] + 1] =
entity_obj
end
entities.name = layer.__identifier
entities.grid_size = layer.__gridSize
return entities
end
local layerTypes = {
AutoLayer = getAutoLayer,
IntGrid = getIntGrid,
Entities = getEntities,
Tiles = getTileLayer,
}
local function getLayer(_layer, root, options, tilesets)
local layer = {}
local getLayerByType = layerTypes[_layer.__type]
if getLayerByType then
layer = getLayerByType(_layer, root, options, tilesets)
else
layer.name = _layer.__identifier
end
return layer
end
local function getLayers(_layers, root, options, tilesets)
local layers = {}
for i = 1, #_layers do
local _layer = _layers[i]
local layer = getLayer(_layer, root, options, tilesets)
layers[layer.name] = layer
end
return layers
end
local function getNeighbours(level, levelsByUid)
local _neighbours = level.__neighbours
local neighbours = {}
for i = 1, #_neighbours do
local neighbour = _neighbours[i]
neighbours[neighbour.dir] = levelsByUid[neighbour.levelUid]
end
return neighbours
end
local function getLevel(_level, root, options, tilesets, levelsByUid)
local level = getLayers(_level.layerInstances, root, options, tilesets)
for _, field in ipairs(_level.fieldInstances or {}) do
level[field.__identifier] = field.__value
end
level.name = _level.identifier
level.width = _level.pxWid
level.height = _level.pxHei
level.next = getNeighbours(_level, levelsByUid)
return level
end
local function getLevels(_levels, root, options, tilesets, levelsByUid)
local levels = {}
for i = 1, #_levels do
local _level = _levels[i]
local level = getLevel(_level, root, options, tilesets, levelsByUid)
levels[level.name] = level
end
return levels
end
function Tilemapper:new(path, options)
local root = path:gsub("[^/]+.ldtk", "")
local data = love.filesystem.read(path)
local raw = json.decode(data)
local _levels = raw.levels
local _tilesets = raw.defs.tilesets
local tilesets = {}
for i = 1, #_tilesets do
local tileset = _tilesets[i]
tilesets[tileset.uid] = {
padding = tileset.padding,
spacing = tileset.spacing,
}
end
local levelsByUid = {}
for i = 1, #_levels do
local level = _levels[i]
levelsByUid[level.uid] = level.identifier
end
local levels = getLevels(_levels, root, options, tilesets, levelsByUid)
self.levels = levels
end
function IntGrid:addCollisions(world)
local tiles = self.tiles
for i = 1, #tiles do
local tile = tiles[i]
world:add(tile, tile.x, tile.y, tile.w, tile.h)
end
end
function Tilemapper:addCollisions()
local layers = self.active
for _, layer in pairs(layers) do
local meta = getmetatable(layer)
if meta and meta.addCollisions then
layer:addCollisions(self.world)
end
end
end
function IntGrid:removeCollisions(world)
local tiles = self.tiles
for i = 1, #tiles do
local tile = tiles[i]
world:remove(tile)
end
end
function Tilemapper:removeCollisions()
local layers = self.active
for _, layer in pairs(layers) do
local meta = getmetatable(layer)
if meta and meta.addCollisions then
layer:removeCollisions(self.world)
end
end
end
function AutoLayer:draw()
local tiles = self.tiles
for i = 1, #tiles do
local tile = tiles[i]
love.graphics.draw(self.tileset, self.quads[tile.t], tile.px[1], tile.px[2])
end
end
function TileLayer:draw()
local tiles = self.tiles
for i = 1, #tiles do
local tile = tiles[i]
love.graphics.draw(self.tileset, self.quads[tile.t], tile.px[1], tile.px[2])
end
end
function Tilemapper:loadLevel(name, world)
if not self.world and world then
self.world = world
end
if not self.levels[name] then
return false
end
self.current = name
if self.active then
self:removeCollisions()
end
self.active = self.levels[self.current]
self:addCollisions()
end
function Tilemapper:nextLevel(dir)
local nextLevel = self.active.next[dir]
if not nextLevel then
return false
end
self:loadLevel(self.active.next[dir])
end
local function get_sorted_keys(t)
local keys = {}
for k in pairs(t) do
table.insert(keys, k)
end
table.sort(keys)
return keys
end
function Tilemapper:draw()
local layers = self.active
for _, k in ipairs(get_sorted_keys(layers)) do
local layer = layers[k]
local meta = getmetatable(layer)
if meta and meta.draw then
layer:draw()
end
end
end
return Tilemapper