363
loadLayers = function (self, file, tileClass)
364
local ok, data = pcall(loadstring(Cached:text(file)))
365
local _, _, directory = string.find(file, '^(.*[/\\])')
366
directory = directory or ''
369
-- store tile properties by gid
371
local tileProtos = {}
373
for _, tileset in pairs(data.tilesets) do
374
for _, tile in pairs(tileset.tiles) do
375
local id = tileset.firstgid + tile.id
377
for key, value in pairs(tile.properties) do
378
tile.properties[key] = tovalue(value)
381
tileProtos[id] = tile
382
tileProtos[id].width = tileset.tilewidth
383
tileProtos[id].height = tileset.tileheight
387
for _, layer in pairs(data.layers) do
388
if self.prototype[layer.name] then
389
error('The class you are loading layers into reserves the ' .. layer.name .. ' property for its own use; you cannot load a layer with that name')
392
if STRICT and self[layer.name] then
393
local info = debug.getinfo(2, 'Sl')
394
print('Warning: a property named ' .. layer.name .. ' already exists in this group (' ..
395
info.short_src .. ', line ' .. info.currentline .. ')')
398
if layer.type == 'tilelayer' then
399
local map = Map:new{ spriteWidth = data.tilewidth, spriteHeight = data.tileheight }
400
map:empty(layer.width, layer.height)
404
for _, tiles in pairs(data.tilesets) do
405
map:loadTiles(directory .. tiles.image, tileClass or Tile, tiles.firstgid)
407
-- and mix in properties where applicable
409
for id, tile in pairs(tileProtos) do
410
if map.sprites[id] then
411
map.sprites[id]:mixin(tile.properties)
421
for _, val in ipairs(layer.data) do
425
if x > layer.width then
431
self[layer.name] = map
433
elseif layer.type == 'objectgroup' then
434
local group = Group:new()
436
for _, obj in pairs(layer.objects) do
437
-- roll in tile properties if based on a tile
439
if obj.gid and tileProtos[obj.gid] then
440
local tile = tileProtos[obj.gid]
442
obj.name = tile.properties.name
443
obj.width = tile.width
444
obj.height = tile.height
446
for key, value in pairs(tile.properties) do
447
obj.properties[key] = tovalue(value)
450
-- Tiled tile-based objects measure their y
451
-- position at their lower-left corner, instead
452
-- of their upper-left corner as usual
454
obj.y = obj.y - obj.height
457
-- create a new object if the class does exist
462
obj.properties.x = obj.x
463
obj.properties.y = obj.y
464
obj.properties.width = obj.width
465
obj.properties.height = obj.height
467
spr = _G[obj.name]:new(obj.properties)
469
spr = Fill:new{ x = obj.x, y = obj.y, width = obj.width, height = obj.height, fill = { 128, 128, 128 } }
472
if obj.properties._the then
473
the[obj.properties._the] = spr
479
self[layer.name] = group
482
error("don't know how to create a " .. layer.type .. " layer from file data")
486
error('could not load layers from file: ' .. data)
334
loadLayers = function (self, file, tileClass)
335
local ok, data = pcall(loadstring(Cached:text(file)))
336
local _, _, directory = string.find(file, '^(.*[/\\])')
337
directory = directory or ''
340
-- store tile properties by gid
342
local tileProtos = {}
344
for _, tileset in pairs(data.tilesets) do
345
for _, tile in pairs(tileset.tiles) do
346
local id = tileset.firstgid + tile.id
348
for key, value in pairs(tile.properties) do
349
tile.properties[key] = tovalue(value)
352
tileProtos[id] = tile
353
tileProtos[id].width = tileset.tilewidth
354
tileProtos[id].height = tileset.tileheight
358
for _, layer in pairs(data.layers) do
359
if self.prototype[layer.name] then
360
error('The class you are loading layers into reserves the ' .. layer.name .. ' property for its own use; you cannot load a layer with that name')
363
if STRICT and self[layer.name] then
364
local info = debug.getinfo(2, 'Sl')
365
print('Warning: a property named ' .. layer.name .. ' already exists in this group (' ..
366
info.short_src .. ', line ' .. info.currentline .. ')')
369
if layer.type == 'tilelayer' then
370
local map = Map:new{ spriteWidth = data.tilewidth, spriteHeight = data.tileheight }
371
map:empty(layer.width, layer.height)
375
for _, tiles in pairs(data.tilesets) do
376
map:loadTiles(directory .. tiles.image, tileClass or Tile, tiles.firstgid)
378
-- and mix in properties where applicable
380
for id, tile in pairs(tileProtos) do
381
if map.sprites[id] then
382
map.sprites[id]:mixin(tile.properties)
392
for _, val in ipairs(layer.data) do
396
if x > layer.width then
402
self[layer.name] = map
404
elseif layer.type == 'objectgroup' then
405
local group = Group:new()
407
for _, obj in pairs(layer.objects) do
408
-- roll in tile properties if based on a tile
410
if obj.gid and tileProtos[obj.gid] then
411
local tile = tileProtos[obj.gid]
413
obj.name = tile.properties.name
414
obj.width = tile.width
415
obj.height = tile.height
417
for key, value in pairs(tile.properties) do
418
obj.properties[key] = tovalue(value)
421
-- Tiled tile-based objects measure their y
422
-- position at their lower-left corner, instead
423
-- of their upper-left corner as usual
425
obj.y = obj.y - obj.height
428
-- create a new object if the class does exist
433
obj.properties.x = obj.x
434
obj.properties.y = obj.y
435
obj.properties.width = obj.width
436
obj.properties.height = obj.height
438
spr = _G[obj.name]:new(obj.properties)
440
spr = Fill:new{ x = obj.x, y = obj.y, width = obj.width, height = obj.height, fill = { 128, 128, 128 } }
443
if obj.properties._the then
444
the[obj.properties._the] = spr
450
self[layer.name] = group
453
error("don't know how to create a " .. layer.type .. " layer from file data")
457
error('could not load layers from file: ' .. data)
491
461
-- passes startFrame events to member sprites
493
463
startFrame = function (self, elapsed)
494
464
if not self.active then return end
495
465
elapsed = elapsed * self.timeScale
497
for _, spr in self:members() do
467
for _, spr in pairs(self.sprites) do
498
468
if spr.active then spr:startFrame(elapsed) end