-
Notifications
You must be signed in to change notification settings - Fork 121
/
map.lua
1408 lines (1217 loc) · 35.9 KB
/
map.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- Map object
-- @module map
local path = (...):gsub('%.[^%.]+$', '') .. "."
local pluginPath = string.gsub(path, "[.]", "/") .. "plugins/"
local Map = {}
-- https://github.com/stevedonovan/Penlight/blob/master/lua/pl/path.lua#L286
local function formatPath(path)
local np_gen1,np_gen2 = '[^SEP]+SEP%.%.SEP?','SEP+%.?SEP'
local np_pat1, np_pat2 = np_gen1:gsub('SEP','/'), np_gen2:gsub('SEP','/')
local k
repeat -- /./ -> /
path,k = path:gsub(np_pat2,'/')
until k == 0
repeat -- A/../ -> (empty)
path,k = path:gsub(np_pat1,'')
until k == 0
if path == '' then path = '.' end
return path
end
-- Compensation for scale/rotation shift
local function compensate(tile, x, y, tw, th)
local tx = x + tile.offset.x
local ty = y + tile.offset.y
local origx = tx
local origy = ty
local compx = 0
local compy = 0
if tile.sx < 0 then compx = tw end
if tile.sy < 0 then compy = th end
if tile.r > 0 then
tx = tx + th - compy
ty = ty + th - tw + compx
elseif tile.r < 0 then
tx = tx + compy
ty = ty + th - compx
else
tx = tx + compx
ty = ty + compy
end
return tx, ty
end
-- Cache images in main STI module
local function cache_image(sti, path)
local image = love.graphics.newImage(path)
image:setFilter("nearest", "nearest")
sti.cache[path] = image
end
--- Instance a new map
-- @param path Path to the map file
-- @param plugins A list of plugins to load
-- @param ox Offset of map on the X axis (in pixels)
-- @param oy Offset of map on the Y axis (in pixels)
-- @return nil
function Map:init(STI, path, plugins, ox, oy)
if type(plugins) == "table" then
self:loadPlugins(plugins)
end
self:resize()
self.objects = {}
self.tiles = {}
self.tileInstances = {}
self.drawRange = {
sx = 1,
sy = 1,
ex = self.width,
ey = self.height,
}
self.offsetx = ox or 0
self.offsety = oy or 0
self.sti = STI
-- Set tiles, images
local gid = 1
for i, tileset in ipairs(self.tilesets) do
assert(tileset.image, "STI does not support Tile Collections.\nYou need to create a Texture Atlas.")
-- Cache images
local formatted_path = formatPath(path .. tileset.image)
if not self.sti.cache[formatted_path] then
cache_image(self.sti, formatted_path)
end
-- Pull images from cache
tileset.image = self.sti.cache[formatted_path]
gid = self:setTiles(i, tileset, gid)
end
-- Set layers
for i, layer in ipairs(self.layers) do
self:setLayer(layer, path)
end
end
--- Load plugins
-- @param plugins A list of plugins to load
-- @return nil
function Map:loadPlugins(plugins)
for _, plugin in ipairs(plugins) do
local p = pluginPath .. plugin .. ".lua"
if love.filesystem.isFile(p) then
local file = love.filesystem.load(p)()
for k, func in pairs(file) do
if not self[k] then
self[k] = func
end
end
end
end
end
--- Create Tiles
-- @param index Index of the Tileset
-- @param tileset Tileset data
-- @param gid First Global ID in Tileset
-- @return number Next Tileset's first Global ID
function Map:setTiles(index, tileset, gid)
local function getTiles(i, t, m, s)
i = i - m
local n = 0
while i >= t do
i = i - t
if n ~= 0 then i = i - s end
if i >= 0 then n = n + 1 end
end
return n
end
local quad = love.graphics.newQuad
local mw = self.tilewidth
local iw = tileset.imagewidth
local ih = tileset.imageheight
local tw = tileset.tilewidth
local th = tileset.tileheight
local s = tileset.spacing
local m = tileset.margin
local w = getTiles(iw, tw, m, s)
local h = getTiles(ih, th, m, s)
for y = 1, h do
for x = 1, w do
local id = gid - tileset.firstgid
local qx = (x - 1) * tw + m + (x - 1) * s
local qy = (y - 1) * th + m + (y - 1) * s
local properties, terrain, animation, objectGroup
for _, tile in pairs(tileset.tiles) do
if tile.id == id then
properties = tile.properties
animation = tile.animation
objectGroup = tile.objectGroup
if tile.terrain then
terrain = {}
for i=1, #tile.terrain do
terrain[i] = tileset.terrains[tile.terrain[i] + 1]
end
end
end
end
local tile = {
id = id,
gid = gid,
tileset = index,
quad = quad(qx, qy, tw, th, iw, ih),
properties = properties or {},
terrain = terrain,
animation = animation,
objectGroup = objectGroup,
frame = 1,
time = 0,
width = tw,
height = th,
sx = 1,
sy = 1,
r = 0,
offset = {
x = -mw + tileset.tileoffset.x,
y = -th + tileset.tileoffset.y,
},
}
if self.orientation == "isometric" then
tile.offset.x = -mw / 2
end
self.tiles[gid] = tile
gid = gid + 1
end
end
return gid
end
--- Create Layers
-- @param layer Layer data
-- @param path (Optional) Path to an Image Layer's image
-- @return nil
function Map:setLayer(layer, path)
if layer.encoding then
if layer.encoding == "base64" then
local ffi = assert(require "ffi", "Compressed maps require LuaJIT FFI.\nPlease Switch your interperator to LuaJIT or your Tile Layer Format to \"CSV\".")
local fd = love.filesystem.newFileData(layer.data, "data", "base64"):getString()
local function getDecompressedData(data)
local d = {}
local decoded = ffi.cast("uint32_t*", data)
for i=0, data:len() / ffi.sizeof("uint32_t") do
table.insert(d, tonumber(decoded[i]))
end
return d
end
if not layer.compression then
layer.data = getDecompressedData(fd)
else
assert(love.math.decompress, "zlib and gzip compression require LOVE 0.10.0+.\nPlease set your Tile Layer Format to \"Base64 (uncompressed)\" or \"CSV\".")
if layer.compression == "zlib" then
local data = love.math.decompress(fd, "zlib")
layer.data = getDecompressedData(data)
end
if layer.compression == "gzip" then
local data = love.math.decompress(fd, "gzip")
layer.data = getDecompressedData(data)
end
end
end
end
layer.x = (layer.x or 0) + self.offsetx
layer.y = (layer.y or 0) + self.offsety
layer.update = function(dt) return end
if layer.type == "tilelayer" then
self:setTileData(layer)
self:setSpriteBatches(layer)
layer.draw = function() self:drawTileLayer(layer) end
elseif layer.type == "objectgroup" then
self:setObjectData(layer)
self:setObjectCoordinates(layer)
self:setObjectSpriteBatches(layer)
layer.draw = function() self:drawObjectLayer(layer) end
elseif layer.type == "imagelayer" then
layer.draw = function() self:drawImageLayer(layer) end
if layer.image ~= "" then
local formatted_path = formatPath(path .. layer.image)
if not self.sti.cache[formatted_path] then
cache_image(self.sti, formatted_path)
end
layer.image = self.sti.cache[formatted_path]
layer.width = layer.image:getWidth()
layer.height = layer.image:getHeight()
end
end
self.layers[layer.name] = layer
end
--- Add Tiles to Tile Layer
-- @param layer The Tile Layer
-- @return nil
function Map:setTileData(layer)
local i = 1
local map = {}
for y = 1, layer.height do
map[y] = {}
for x = 1, layer.width do
local gid = layer.data[i]
if gid > 0 then
map[y][x] = self.tiles[gid] or self:setFlippedGID(gid)
end
i = i + 1
end
end
layer.data = map
end
--- Add Objects to Layer
-- @param layer The Object Layer
-- @return nil
function Map:setObjectData(layer)
for _, object in ipairs(layer.objects) do
object.layer = layer
self.objects[object.id] = object
end
end
--- Correct position and orientation of Objects in an Object Layer
-- @param layer The Object Layer
-- @return nil
function Map:setObjectCoordinates(layer)
local function convertEllipseToPolygon(x, y, w, h, max_segments)
local function calc_segments(segments)
local function vdist(a, b)
local c = {
x = a.x - b.x,
y = a.y - b.y,
}
return c.x * c.x + c.y * c.y
end
segments = segments or 64
local vertices = {}
local v = { 1, 2, math.ceil(segments/4-1), math.ceil(segments/4) }
local m
if love.physics then
m = love.physics.getMeter()
else
m = 32
end
for _, i in ipairs(v) do
local angle = (i / segments) * math.pi * 2
local px = x + w / 2 + math.cos(angle) * w / 2
local py = y + h / 2 + math.sin(angle) * h / 2
table.insert(vertices, { x = px / m, y = py / m })
end
local dist1 = vdist(vertices[1], vertices[2])
local dist2 = vdist(vertices[3], vertices[4])
-- Box2D threshold
if dist1 < 0.0025 or dist2 < 0.0025 then
return calc_segments(segments-2)
end
return segments
end
local segments = calc_segments(max_segments)
local vertices = {}
table.insert(vertices, { x = x + w / 2, y = y + h / 2 })
for i=0, segments do
local angle = (i / segments) * math.pi * 2
local px = x + w / 2 + math.cos(angle) * w / 2
local py = y + h / 2 + math.sin(angle) * h / 2
table.insert(vertices, { x = px, y = py })
end
return vertices
end
local function rotateVertex(vertex, x, y, cos, sin)
if self.orientation == "isometric" then
x, y = self:convertIsometricToScreen(x, y)
vertex.x, vertex.y = self:convertIsometricToScreen(vertex.x, vertex.y)
end
vertex.x = vertex.x - x
vertex.y = vertex.y - y
local vx = cos * vertex.x - sin * vertex.y
local vy = sin * vertex.x + cos * vertex.y
return vx + x, vy + y
end
for _, object in ipairs(layer.objects) do
local x = layer.x + object.x
local y = layer.y + object.y
local w = object.width
local h = object.height
local r = object.rotation
local cos = math.cos(math.rad(r))
local sin = math.sin(math.rad(r))
if object.shape == "rectangle" and not object.gid then
object.rectangle = {}
local vertices = {
{ x=x, y=y },
{ x=x + w, y=y },
{ x=x + w, y=y + h },
{ x=x, y=y + h },
}
for _, vertex in ipairs(vertices) do
vertex.x, vertex.y = rotateVertex(vertex, x, y, cos, sin)
table.insert(object.rectangle, { x = vertex.x, y = vertex.y })
end
elseif object.shape == "ellipse" then
object.ellipse = {}
local vertices = convertEllipseToPolygon(x, y, w, h)
for _, vertex in ipairs(vertices) do
vertex.x, vertex.y = rotateVertex(vertex, x, y, cos, sin)
table.insert(object.ellipse, { x = vertex.x, y = vertex.y })
end
elseif object.shape == "polygon" then
for _, vertex in ipairs(object.polygon) do
vertex.x = vertex.x + x
vertex.y = vertex.y + y
vertex.x, vertex.y = rotateVertex(vertex, x, y, cos, sin)
end
elseif object.shape == "polyline" then
for _, vertex in ipairs(object.polyline) do
vertex.x = vertex.x + x
vertex.y = vertex.y + y
vertex.x, vertex.y = rotateVertex(vertex, x, y, cos, sin)
end
end
end
end
--- Batch Tiles in Tile Layer for improved draw speed
-- @param layer The Tile Layer
-- @return nil
function Map:setSpriteBatches(layer)
local newBatch = love.graphics.newSpriteBatch
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local tw = self.tilewidth
local th = self.tileheight
local bw = math.ceil(w / tw)
local bh = math.ceil(h / th)
local sx = 1
local sy = 1
local ex = layer.width
local ey = layer.height
local ix = 1
local iy = 1
-- Determine order to add tiles to sprite batch
-- Defaults to right-down
if self.renderorder == "right-up" then
sx, ex, ix = sx, ex, 1
sy, ey, iy = ey, sy, -1
elseif self.renderorder == "left-down" then
sx, ex, ix = ex, sx, -1
sy, ey, iy = sy, ey, 1
elseif self.renderorder == "left-up" then
sx, ex, ix = ex, sx, -1
sy, ey, iy = ey, sy, -1
end
-- Minimum of 400 tiles per batch
if bw < 20 then bw = 20 end
if bh < 20 then bh = 20 end
local size = bw * bh
local batches = {
width = bw,
height = bh,
data = {},
}
for y=sy, ey, iy do
local by = math.ceil(y / bh)
for x=sx, ex, ix do
local tile = layer.data[y][x]
local bx = math.ceil(x / bw)
local id
if tile then
local ts = tile.tileset
local image = self.tilesets[tile.tileset].image
batches.data[ts] = batches.data[ts] or {}
batches.data[ts][by] = batches.data[ts][by] or {}
batches.data[ts][by][bx] = batches.data[ts][by][bx] or newBatch(image, size)
local batch = batches.data[ts][by][bx]
local tx, ty
if self.orientation == "orthogonal" then
tx, ty = compensate(tile, x*tw, y*th, tw, th)
elseif self.orientation == "isometric" then
tx = (x - y) * (tw / 2) + tile.offset.x + layer.width * tw / 2
ty = (x + y) * (th / 2) + tile.offset.y
elseif self.orientation == "staggered" or self.orientation == "hexagonal" then
local hl = self.hexsidelength or 0
if self.staggeraxis == "y" then
if self.staggerindex == "odd" then
if y % 2 == 0 then
tx = x * tw + tw / 2 + hl + tile.offset.x
else
tx = x * tw + hl + tile.offset.x
end
else
if y % 2 == 0 then
tx = x * tw + hl + tile.offset.x
else
tx = x * tw + tw / 2 + hl + tile.offset.x
end
end
if self.orientation == "hexagonal" then
ty = y * (th - (th - hl) / 2) + tile.offset.y + (th - (th - hl) / 2)
else
ty = y * th / 2 + tile.offset.y + th / 2
end
else
if self.staggerindex == "odd" then
if x % 2 == 0 then
ty = y * th + th / 2 + hl + tile.offset.y
else
ty = y * th + hl + tile.offset.y
end
else
if x % 2 == 0 then
ty = y * th + hl + tile.offset.y
else
ty = y * th + th / 2 + hl + tile.offset.y
end
end
if self.orientation == "hexagonal" then
tx = x * (tw - (tw - hl) / 2) + tile.offset.x + (tw - (tw - hl) / 2)
else
tx = x * tw / 2 + tile.offset.x + tw / 2
end
end
-- I FEEL LIKE THIS IS WRONG
if self.orientation == "hexagonal" then
tx = tx - tw / 2 - 2
ty = ty - th / 2 - 2
end
end
id = batch:add(tile.quad, tx, ty, tile.r, tile.sx, tile.sy)
self.tileInstances[tile.gid] = self.tileInstances[tile.gid] or {}
table.insert(self.tileInstances[tile.gid], {
layer = layer,
batch = batch,
id = id,
gid = tile.gid,
x = tx,
y = ty,
r = tile.r,
oy = 0
})
end
end
end
layer.batches = batches
end
--- Batch Tiles in Object Layer for improved draw speed
-- @param layer The Object Layer
-- @return nil
function Map:setObjectSpriteBatches(layer)
local newBatch = love.graphics.newSpriteBatch
local tw = self.tilewidth
local th = self.tileheight
local batches = {}
for _, object in ipairs(layer.objects) do
if object.gid then
local tile = self.tiles[object.gid] or self:setFlippedGID(object.gid)
local ts = tile.tileset
local image = self.tilesets[tile.tileset].image
batches[ts] = batches[ts] or newBatch(image, 100)
local batch = batches[ts]
local tx = object.x + tw + tile.offset.x
local ty = object.y + tile.offset.y
local tr = math.rad(object.rotation)
local oy = 0
-- Compensation for scale/rotation shift
if tile.sx == 1 and tile.sy == 1 then
if tr ~= 0 then
ty = ty + th
oy = th
end
else
if tile.sx < 0 then tx = tx + tw end
if tile.sy < 0 then ty = ty + th end
if tr > 0 then tx = tx + tw end
if tr < 0 then ty = ty + th end
end
id = batch:add(tile.quad, tx, ty, tr, tile.sx, tile.sy, 0, oy)
self.tileInstances[tile.gid] = self.tileInstances[tile.gid] or {}
table.insert(self.tileInstances[tile.gid], {
layer = layer,
batch = batch,
id = id,
gid = tile.gid,
x = tx,
y = ty,
r = tr,
oy = oy
})
end
end
layer.batches = batches
end
--- Only draw what is visible on screen for improved draw speed
-- @param tx Translate X axis (in pixels)
-- @param ty Translate Y axis (in pixels)
-- @param w Width of screen (in pixels)
-- @param h Height of screen (in pixels)
-- @return nil
function Map:setDrawRange(tx, ty, w, h)
local tw, th = self.tilewidth, self.tileheight
local sx, sy, ex, ey
if self.orientation == "orthogonal" then
sx = math.ceil(tx / tw)
sy = math.ceil(ty / th)
ex = math.ceil(sx + w / tw)
ey = math.ceil(sy + h / th)
elseif self.orientation == "isometric" then
sx = math.ceil(((ty / (th / 2)) + (tx / (tw / 2))) / 2)
sy = math.ceil(((ty / (th / 2)) - (tx / (tw / 2))) / 2 - h / th)
ex = math.ceil(sx + (h / th) + (w / tw))
ey = math.ceil(sy + (h / th) * 2 + (w / tw))
elseif self.orientation == "staggered" or self.orientation == "hexagonal" then
sx = math.ceil(tx / tw - 1)
sy = math.ceil(ty / th)
ex = math.ceil(sx + w / tw + 1)
ey = math.ceil(sy + h / th * 2)
end
self.drawRange.sx = sx
self.drawRange.sy = sy
self.drawRange.ex = ex
self.drawRange.ey = ey
end
--- Create a Custom Layer to place userdata in (such as player sprites)
-- @param name Name of Custom Layer
-- @param index Draw order within Layer stack
-- @return table Custom Layer
function Map:addCustomLayer(name, index)
local index = index or #self.layers + 1
local layer = {
type = "customlayer",
name = name,
visible = true,
opacity = 1,
properties = {},
}
function layer:draw() return end
function layer:update(dt) return end
table.insert(self.layers, index, layer)
self.layers[name] = self.layers[index]
return layer
end
--- Convert another Layer into a Custom Layer
-- @param index Index or name of Layer to convert
-- @return table Custom Layer
function Map:convertToCustomLayer(index)
local layer = assert(self.layers[index], "Layer not found: " .. index)
layer.type = "customlayer"
layer.x = nil
layer.y = nil
layer.width = nil
layer.height = nil
layer.encoding = nil
layer.data = nil
layer.objects = nil
layer.image = nil
function layer:draw() return end
function layer:update(dt) return end
return layer
end
--- Remove a Layer from the Layer stack
-- @param index Index or name of Layer to convert
-- @return nil
function Map:removeLayer(index)
local layer = assert(self.layers[index], "Layer not found: " .. index)
if type(index) == "string" then
for i, layer in ipairs(self.layers) do
if layer.name == index then
table.remove(self.layers, i)
self.layers[index] = nil
break
end
end
else
local name = self.layers[index].name
table.remove(self.layers, index)
self.layers[name] = nil
end
-- Remove tile instances
if layer.batches then
for gid, tiles in pairs(self.tileInstances) do
for i=#tiles, 1, -1 do
local tile = tiles[i]
if tile.layer == layer then
table.remove(tiles, i)
end
end
end
end
-- Remove objects
if layer.objects then
for i, object in pairs(self.objects) do
if object.layer == layer then
self.objects[i] = nil
end
end
end
end
--- Animate Tiles and update every Layer
-- @param dt Delta Time
-- @return nil
function Map:update(dt)
for gid, tile in pairs(self.tiles) do
local update = false
if tile.animation then
tile.time = tile.time + dt * 1000
while tile.time > tonumber(tile.animation[tile.frame].duration) do
update = true
tile.time = tile.time - tonumber(tile.animation[tile.frame].duration)
tile.frame = tile.frame + 1
if tile.frame > #tile.animation then tile.frame = 1 end
end
if update and self.tileInstances[tile.gid] then
for _, j in pairs(self.tileInstances[tile.gid]) do
local t = self.tiles[tonumber(tile.animation[tile.frame].tileid) + self.tilesets[tile.tileset].firstgid]
j.batch:set(j.id, t.quad, j.x, j.y, j.r, tile.sx, tile.sy, 0, j.oy)
end
end
end
end
for _, layer in ipairs(self.layers) do
layer:update(dt)
end
end
--- Draw every Layer
-- @return nil
function Map:draw()
local current_canvas = love.graphics.getCanvas()
love.graphics.setCanvas(self.canvas)
if self.canvas.clear then
self.canvas:clear()
else
local r,g,b,a = love.graphics.getBackgroundColor()
love.graphics.clear(r,g,b,a,self.canvas)
end
for _, layer in ipairs(self.layers) do
if layer.visible and layer.opacity > 0 then
self:drawLayer(layer)
end
end
love.graphics.setCanvas(current_canvas)
love.graphics.push()
love.graphics.origin()
love.graphics.draw(self.canvas)
love.graphics.pop()
end
--- Draw an individual Layer
-- @param layer The Layer to draw
-- @return nil
function Map:drawLayer(layer)
love.graphics.setColor(255, 255, 255, 255 * layer.opacity)
layer:draw()
love.graphics.setColor(255, 255, 255, 255)
end
--- Default draw function for Tile Layers
-- @param layer The Tile Layer to draw
-- @return nil
function Map:drawTileLayer(layer)
if type(layer) == "string" or type(layer) == "number" then
layer = self.layers[layer]
end
assert(layer.type == "tilelayer", "Invalid layer type: " .. layer.type .. ". Layer must be of type: tilelayer")
local bw = layer.batches.width
local bh = layer.batches.height
local sx = math.ceil((self.drawRange.sx - layer.x / self.tilewidth - 1) / bw)
local sy = math.ceil((self.drawRange.sy - layer.y / self.tileheight - 1) / bh)
local ex = math.ceil((self.drawRange.ex - layer.x / self.tilewidth + 1) / bw)
local ey = math.ceil((self.drawRange.ey - layer.y / self.tileheight + 1) / bh)
local ix = 1
local iy = 1
local mx = math.ceil(self.width / bw)
local my = math.ceil(self.height / bh)
-- Determine order to draw batches
-- Defaults to right-down
if self.renderorder == "right-up" then
sx, ex, ix = sx, ex, 1
sy, ey, iy = ey, sy, -1
elseif self.renderorder == "left-down" then
sx, ex, ix = ex, sx, -1
sy, ey, iy = sy, ey, 1
elseif self.renderorder == "left-up" then
sx, ex, ix = ex, sx, -1
sy, ey, iy = ey, sy, -1
end
for by=sy, ey, iy do
for bx=sx, ex, ix do
if bx >= 1 and bx <= mx and by >= 1 and by <= my then
for _, batches in pairs(layer.batches.data) do
local batch = batches[by] and batches[by][bx]
if batch then
love.graphics.draw(batch, math.floor(layer.x), math.floor(layer.y))
end
end
end
end
end
end
--- Default draw function for Object Layers
-- @param layer The Object Layer to draw
-- @return nil
function Map:drawObjectLayer(layer)
if type(layer) == "string" or type(layer) == "number" then
layer = self.layers[layer]
end
assert(layer.type == "objectgroup", "Invalid layer type: " .. layer.type .. ". Layer must be of type: objectgroup")
local line = { 160, 160, 160, 255 * layer.opacity }
local fill = { 160, 160, 160, 255 * layer.opacity * 0.2 }
local shadow = { 0, 0, 0, 255 * layer.opacity }
local reset = { 255, 255, 255, 255 * layer.opacity }
local function sortVertices(obj)
local vertices = {{},{}}
for _, vertex in ipairs(obj) do
table.insert(vertices[1], vertex.x)
table.insert(vertices[1], vertex.y)
table.insert(vertices[2], vertex.x+1)
table.insert(vertices[2], vertex.y+1)
end
return vertices
end
local function drawShape(obj, shape)
local vertices = sortVertices(obj)
if shape == "polyline" then
love.graphics.setColor(shadow)
love.graphics.line(vertices[2])
love.graphics.setColor(line)
love.graphics.line(vertices[1])
return
elseif shape == "polygon" then
love.graphics.setColor(fill)
if not love.math.isConvex(vertices[1]) then
local triangles = love.math.triangulate(vertices[1])
for _, triangle in ipairs(triangles) do
love.graphics.polygon("fill", triangle)
end
else
love.graphics.polygon("fill", vertices[1])
end
else
love.graphics.setColor(fill)
love.graphics.polygon("fill", vertices[1])
end
love.graphics.setColor(shadow)
love.graphics.polygon("line", vertices[2])
love.graphics.setColor(line)
love.graphics.polygon("line", vertices[1])
end
for _, object in ipairs(layer.objects) do
if object.shape == "rectangle" and not object.gid then
drawShape(object.rectangle, "rectangle")
elseif object.shape == "ellipse" then
drawShape(object.ellipse, "ellipse")
elseif object.shape == "polygon" then
drawShape(object.polygon, "polygon")
elseif object.shape == "polyline" then
drawShape(object.polyline, "polyline")
end
end
love.graphics.setColor(reset)
for _, batch in pairs(layer.batches) do
love.graphics.draw(batch, 0, 0)
end
end
--- Default draw function for Image Layers
-- @param layer The Image Layer to draw
-- @return nil
function Map:drawImageLayer(layer)
if type(layer) == "string" or type(layer) == "number" then
layer = self.layers[layer]
end
assert(layer.type == "imagelayer", "Invalid layer type: " .. layer.type .. ". Layer must be of type: imagelayer")
if layer.image ~= "" then
love.graphics.draw(layer.image, layer.x, layer.y)
end
end
--- Resize the drawable area of the Map
-- @param w The new width of the drawable area (in pixels)
-- @param h The new Height of the drawable area (in pixels)
-- @return nil
function Map:resize(w, h)
w = w or love.graphics.getWidth()
h = h or love.graphics.getHeight()
self.canvas = love.graphics.newCanvas(w, h)
self.canvas:setFilter("nearest", "nearest")
end
--- Create flipped or rotated Tiles based on bitop flags
-- @param gid The flagged Global ID
-- @return table Flipped Tile
function Map:setFlippedGID(gid)
local bit31 = 2147483648
local bit30 = 1073741824
local bit29 = 536870912
local flipX = false
local flipY = false
local flipD = false
local realgid = gid
if realgid >= bit31 then
realgid = realgid - bit31
flipX = not flipX
end
if realgid >= bit30 then
realgid = realgid - bit30
flipY = not flipY
end
if realgid >= bit29 then
realgid = realgid - bit29
flipD = not flipD
end
local tile = self.tiles[realgid]
local data = {
id = tile.id,
gid = gid,