-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlime-tileLayer.lua
853 lines (615 loc) · 22.2 KB
/
lime-tileLayer.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
-- Project: Lime
--
-- Date: 09-Feb-2011
--
-- Version: 3.4
--
-- File name: lime-tileLayer.lua
--
-- Author: Graham Ranson
--
-- Support: www.justaddli.me
--
-- Copyright (C) 2011 MonkeyDead Studios Limited. All Rights Reserved.
----------------------------------------------------------------------------------------------------
---- POLITE NOTICE / PSA ----
----------------------------------------------------------------------------------------------------
---- ----
---- I have put a lot of work into this library and plan to support it for a very, very ----
---- long time so please don't give the code to anyone else. I doubt that anyone would as ----
---- we are all developers in the same boat but I just thougt I would put this here in ----
---- case any one wondered if it was ok to share. ----
---- ----
---- If you did get this code through less than legitimate means then please consider ----
---- buying it legally, it is (I think) affordably priced and you will get free updates ----
---- and support for life. ----
---- ----
---- I hope you enjoy using it as much as I have enjoyed writing it and I also hope that ----
---- you will support me and the development of Lime by telling your friends about it etc ----
---- although naturally I don't require any link backs of any kind, it is completely up ----
---- to you. ----
---- ----
---- If you have any additions or fixes that you would like included in the main releases ----
---- please contact me via the forums or email - [email protected] ----
---- ----
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- REQUIRED MODULES ----
----------------------------------------------------------------------------------------------------
local base64 = require("lime-external-base64")
local decompress = require("lime-external-deflatelua")
----------------------------------------------------------------------------------------------------
---- CLASS METATABLE ----
----------------------------------------------------------------------------------------------------
TileLayer = {}
TileLayer_mt = { __index = TileLayer }
----------------------------------------------------------------------------------------------------
---- CLASS VARIABLES ----
----------------------------------------------------------------------------------------------------
TileLayer.version = 3.4
----------------------------------------------------------------------------------------------------
---- LOCALISED VARIABLES ----
----------------------------------------------------------------------------------------------------
local utils = lime.utils
local moveObject = utils.moveObject
local dragObject = utils.dragObject
local calculateViewpoint = utils.calculateViewpoint
local isometricToWorldPosition = utils.isometricToWorldPosition
local worldToIsometricPosition = utils.worldToIsometricPosition
local round = utils.round
local ceil = math.ceil
local floor = math.floor
local abs = math.abs
local clampPosition = utils.clampPosition
local worldToGridPosition = utils.worldToGridPosition
local addObjectToGroup = utils.addObjectToGroup
local readInConfigFile = utils.readInConfigFile
local splitString = utils.splitString
local convertStringToNumberSafely = utils.convertStringToNumberSafely
----------------------------------------------------------------------------------------------------
---- PRIVATE METHODS ----
----------------------------------------------------------------------------------------------------
--- Parse layer data (GIDs) from a CSV string.
-- @params data The CSV string.
-- @returns A list of parsed GIDs.
local getTileIDsFromCSV = function(data)
local IDs = {}
if(data) then
local tileIDs = splitString(data, ",")
for i=1, #tileIDs, 1 do
tileIDs[i] = convertStringToNumberSafely( tileIDs[i] )
end
return tileIDs
end
end
--- Parse layer data (GIDs) from a Base64 string.
-- @params layer The TileLayer object.
-- @params data The compressed string.
local getTileIDsFromBase64 = function(layer, data)
if(data) then
local tileIDs = {}
if(layer.compression == "uncompressed") then
tileIDs = base64.decode("int", data)
elseif(layer.compression == "gzip" or layer.compression == "zlib") then
local decompressionFunction = nil
if( layer.compression == "gzip" ) then
decompressionFunction = decompress.gunzip
else
decompressionFunction = decompress.inflate_zlib
end
if decompressionFunction then
local bytes = {}
decompressionFunction( {input = base64.decode("string", data), output = function (b) bytes[ #bytes + 1 ] = b end} )
for i=1, #bytes, 4 do
tileIDs[ #tileIDs + 1 ] = base64.glueInt( bytes[ i ], bytes[ i + 1 ], bytes[ i + 2 ], bytes[ i + 3 ])
end
end
end
return tileIDs
end
end
--- Gets a tile that is at a grid position on this layer.
-- @params tileList The list of tiles. 2D array of grid positions.
-- @params position The grid position to look for.
-- @params full If full then the tileList must be a 1D list rather than 2D and the search will use the individual tiles row/col positions incase they have been updated.
-- @return The found tile or nil if none found.
local getTileAt = function(tileList, position, full)
local _tileList = tileList
local _position = position
local _full = full
if _tileList then
if _full then
for i = 1, #_tileList, 1 do
if _tileList[i].row == _position.row and _tileList[i].column == _position.column then
return _tileList[i]
end
end
else
if _tileList[_position.column] then
if _tileList[_position.column][_position.row] then
return _tileList[_position.column][_position.row]
end
end
end
end
end
----------------------------------------------------------------------------------------------------
---- PUBLIC METHODS ----
----------------------------------------------------------------------------------------------------
--- Create a new instance of a TileLayer object.
-- @param data The XML data.
-- @param map The current Map object.
-- @return The newly created tileLayer.
function TileLayer:new(data, map)
local self = {} -- the new instance
setmetatable( self, TileLayer_mt ) -- all instances share the same metatable
self.properties = {}
self.tiles = {}
self.map = map
-- Extract the header info, name, height and width
for key, value in pairs(data['Attributes']) do
self:setProperty(key, value)
end
local node = nil
local attributes = nil
local childNode = nil
-- Loop through all the child nodes
for i=1, #data["ChildNodes"], 1 do
node = data["ChildNodes"][i]
if node.Name == "data" then
if node['Attributes'] then
local encoding = node['Attributes'].encoding or "xml"
self:setProperty("encoding", encoding)
local compression = node['Attributes'].compression or "uncompressed"
self:setProperty("compression", compression)
end
local tileIDs = {}
if(self.encoding == "xml") then
-- Loop through all the child nodes
for j=1, #node["ChildNodes"], 1 do
childNode = node["ChildNodes"][j]
-- childNode["Attributes"].index = j
self.tiles[#self.tiles + 1] = Tile:new(childNode, self.map, self)
tileIDs = nil
end
else
if(self.encoding == "csv") then
tileIDs = getTileIDsFromCSV(node.Value)
elseif(self.encoding == "base64") then
tileIDs = getTileIDsFromBase64(self, node.Value)
end
if(tileIDs) then -- Now create the tiles
for i=1, #tileIDs, 1 do
local data = {}
data["Attributes"] = {}
data["Attributes"].gid = tileIDs[i]
data["Attributes"].index = i
if(data["Attributes"].gid) then
data["Attributes"].gid = tonumber(data["Attributes"].gid)
self.tiles[#self.tiles + 1] = Tile:new(data, self.map, self)
end
end
end
end
elseif node.Name == "properties" then
-- Loop through all the child nodes
for j=1, #node["ChildNodes"], 1 do
-- Each child node is a property, the attributes are the name and value
attributes = node["ChildNodes"][j]["Attributes"]
if attributes then
if attributes.name == "configFile" then
readInConfigFile(attributes.value, self)
else
property = self:setProperty(attributes.name, attributes.value)
end
end
end
end
end
return self
end
--- Sets the value of a Property of the TileLayer.
-- Will create a new Property if none found.
-- @param name The name of the Property.
-- @param value The new value.
-- @return The new value.
function TileLayer:setProperty(name, value)
local property = self:getProperty(name)
if property then
return property:setValue(value)
else
self:addProperty(Property:new(name, value))
end
self[name] = self:getPropertyValue(name)
end
--- Gets a Property of the TileLayer.
-- @param name The name of the Property.
-- @return The Property. nil if no Property found.
function TileLayer:getProperty(name)
return self.properties[name]
end
--- Gets the value of a Property of the TileLayer.
-- @param name The name of the Property.
-- @return The Property value. Nil if no Property found.
function TileLayer:getPropertyValue(name)
local property = self:getProperty(name)
if property then
return property:getValue()
end
end
--- Gets a list of all Properties of the TileLayer.
-- @return The list of Properties.
function TileLayer:getProperties()
return self.properties
end
--- Gets a count of how many properties the Tile Layer has.
-- @return The Property count.
function TileLayer:getPropertyCount()
local count = 0
for _k, _v in pairs(self.properties) do
count = count + 1
end
return count
end
---Checks whether the TileLayer has a certain Property.
-- @param name The name of the property to check for.
-- @return True if the TileLayer has the Property, false if not.
function TileLayer:hasProperty(name)
return self:getProperty(name) ~= nil
end
--- Adds a Property to the TileLayer.
-- @param property The Property to add.
-- @return The added Property.
function TileLayer:addProperty(property)
self.properties[property:getName()] = property
self[property:getName()] = property:getValue()
return property
end
--- Removes a Property from the TileLayer.
-- @param: name The name of the Property to remove.
function TileLayer:removeProperty(name)
self.properties[name] = nil
end
--- Shows the TileLayer.
function TileLayer:show()
if self.visible then
local tiles = self.tiles
if not lime.isScreenCullingEnabled() then
for i=1, #tiles, 1 do
tiles[i]:show()
end
end
local visual = self:getVisual()
if visual then
visual.isVisible = true
end
end
self:updateTileVisibility()
end
--- Hides the TileLayer.
function TileLayer:hide()
local tiles = self.tiles
if not lime.isScreenCullingEnabled() then
for i=1, #tiles, 1 do
tiles[i]:hide()
end
end
local visual = self:getVisual()
if visual then
visual.isVisible = false
end
end
--- Gets the TileLayers visual.
function TileLayer:getVisual()
return self.group
end
--- Gets a Tile on this TileLayer at a specified position.
-- @param position The position of the Tile. A table containing either x & y or row & column.
-- @params full If true the search will check all tiles using their updated rather than original grid position. Might be slower then non-full search so use with caution.
-- @return The found Tile. nil if none found.
function TileLayer:getTileAt(position, full)
if(position.row and position.column) then
if full then
return getTileAt(self.tiles, position, true)
else
return getTileAt(self.tileGrid, position)
end
elseif(position.x and position.y) then
local gridPos = worldToGridPosition(self.map, position)
return self:getTileAt(gridPos)
end
end
--- Gets a list of Tiles on this TileLayer that have a specified property.
-- @param name The name of the Property to look for.
-- @return A list of found Tiles. Empty if none found.
function TileLayer:getTilesWithProperty(name)
local allTiles = self.tiles
local tiles = {}
for i = 1, #allTiles, 1 do
if allTiles[i]:hasProperty(name) then
tiles[#tiles + 1] = allTiles[i]
end
end
return tiles
end
--- Swaps two tiles around.
-- @param tile1 The first tile.
-- @param tile2 The second tile.
-- @usage Originally created by Michał Kołodziejski - http://developer.anscamobile.com/forum/2011/02/12/how-swap-two-tiles-layer
function TileLayer:swapTiles(tile1, tile2)
-- Make sure we actually have tiles
if tile1 and tile2 then
-- Make sure the tiles have sprites attached
if tile1.sprite and tile2.sprite then
-- Swap the tile world positions
tile1.sprite.x, tile2.sprite.x = tile2.sprite.x, tile1.sprite.x
tile1.sprite.y, tile2.sprite.y = tile2.sprite.y, tile1.sprite.y
-- Swap the tiles in the tileGrid
self.tileGrid[tile1.column][tile1.row], self.tileGrid[tile2.column][tile2.row] = self.tileGrid[tile2.column][tile2.row], self.tileGrid[tile1.column][tile1.row]
-- Swap the tile grid positions
tile1.column, tile2.column = tile2.column, tile1.column
tile1.row, tile2.row = tile2.row, tile1.row
end
end
end
--- Swaps two tiles around based on their positions.
-- @param position1 The position of the first tile.
-- @param position2 The position of the second tile.
-- @usage Originally created by Michał Kołodziejski - http://developer.anscamobile.com/forum/2011/02/12/how-swap-two-tiles-layer
function TileLayer:swapTilesAtPositions(position1, position2)
-- Get both tiles
local tile1 = self:getTileAt(position1)
local tile2 = self:getTileAt(position2)
-- Swap the tiles
self:swapTiles(tile1, tile2)
end
--- Destroys a tile at a certain position.
-- @param position The position of the tile, world or grid.
function TileLayer:removeTileAt(position)
local tile = self:getTileAt(position)
if tile then
tile:destroy()
end
end
--- Set a tile at a position.
-- @param gid The gid of the tile.
-- @param position The position of the tile, World or Grid.
-- @usage Originally created by Mattguest - http://developer.anscamobile.com/forum/2011/02/02/settile-function
function TileLayer:setTileAt(gid, position)
local tile = self:getTileAt(position)
-- First make sure there is a tile
if tile then
tile:setImage(gid)
end
end
--- Creates a new tile from a passed in GID. This will include all properties however it will not Build it ( by design ). If you wish to build it then simply call ":build()" on the returned tile.
-- @param gid The gid of the tile to create.
-- @return The created Tile.
function TileLayer:createTile(gid)
local tileSet = self.map:getTileSetFromGID( gid )
if tileSet then
local data = {}
data.isGenerated = true
data.properties = tileSet:getPropertiesForTile( gid )
local tile = Tile:new(data, self.map, self)
if tile then
local index = #self.tiles + 1
tile.gid = gid
tile:create( index )
self.tiles[ index ] = tile
tile:updateGridPosition()
end
return tile
end
end
--- Creates and builds a new tile from a passed in GID. This will include all properties.
-- @param gid The gid of the tile to create.
-- @return The created Tile.
function TileLayer:createAndBuildTile(gid)
local tile = self:createTile(gid)
if tile then
tile:build()
end
return tile
end
--- Creates a new tile from a passed in GID and sets its position. This will include all properties however it will not Build it ( by design ). If you wish to build it then simply call ":build()" on the returned tile.
-- @param gid The gid of the tile to create.
-- @param position The world position for the Tile.
-- @return The created Tile.
function TileLayer:createTileAt(gid, position)
local tile = self:createTile(gid)
if tile then
if position.row and position.column then
position = lime.utils.gridToWorldPosition( self.map, position )
end
tile:setPosition(position.x, position.y)
end
return tile
end
--- Creates and builds a new tile from a passed in GID and sets its position. This will include all properties.
-- @param gid The gid of the tile to create.
-- @param position The world position for the Tile.
-- @return The created Tile.
function TileLayer:createAndBuildTileAt(gid, position)
local tile = self:createTileAt(gid, position)
if tile then
tile:build()
end
return tile
end
--- Adds a displayObject to the layer.
-- @param displayObject The displayObject to add.
-- @return The added displayObject.
function TileLayer:addObject(displayObject)
return addObjectToGroup(displayObject, self.group)
end
--- Sets the position of the TileLayer.
-- @param x The new X position of the TileLayer.
-- @param y The new Y position of the TileLayer.
-- @param force If true then the layer will not be clamped or use the viewpoint calculator. Default is false. Optional.
function TileLayer:setPosition(x, y, force)
if self.group then
if force then
self.group.x = round(x)
self.group.y = round(y)
else
local viewPoint = calculateViewpoint(self.group, x, y)
self.group.x = round(viewPoint.x)
self.group.y = round(viewPoint.y)
if self.map.orientation ~= "isometric" then
self.group.x, self.group.y = clampPosition(self.group.x, self.group.y, self.map.bounds)
end
end
end
end
--- Moves the TileLayer.
-- @param x The amount to move the TileLayer along the X axis.
-- @param y The amount to move the TileLayer along the Y axis.
function TileLayer:move(x, y)
moveObject(self.group, x, y)
end
--- Drags the TileLayer.
-- @param event The Touch event.
function TileLayer:drag(event)
dragObject(self.group, event)
end
--- Sets the rotation of the TileLayer.
-- @param angle The new rotation.
function TileLayer:setRotation(angle)
self.group.rotation = angle
end
--- Rotates the TileLayer.
-- @param angle The angle to rotate by.
function TileLayer:rotate(angle)
self.group.rotation = self.group.rotation + angle
end
--- Updates the TileLayer.
-- @param event The enterFrame event.
function TileLayer:update( event )
end
function TileLayer:updateTileVisibility()
if self.visible then
if self.visibleTiles then
for i=1, #self.visibleTiles, 1 do
if self.visibleTiles[i].isVisible then
self.visibleTiles[i].isVisible = false
end
end
end
self.visibleTiles = nil
self.visibleTiles = {}
local xScale, yScale = self.map:getScale()
local tileWidth = self.map.tilewidth * xScale
local tileHeight = self.map.tileheight * yScale
local cam = {}
cam.x = abs( self.map.world.x - tileWidth)
cam.y = abs( self.map.world.y - tileHeight)
cam.width = ( display.contentWidth ) * 1 --xScale
cam.height = ( display.contentHeight ) * 1 --yScale
local buffer = {}
buffer.left = tileWidth
buffer.right = tileWidth
buffer.top = tileHeight
buffer.bottom = tileHeight
local view = {}
view.xMin = floor( ( ( cam.x - buffer.left ) / tileWidth * xScale ) )
view.xMax = floor( ( ( cam.x + ( cam.width + buffer.right ) ) / tileWidth / xScale ) )
view.yMin = floor( ( ( cam.y - buffer.top ) / tileHeight * yScale ) )
view.yMax = floor( ( ( cam.y + ( cam.height + buffer.bottom ) ) / tileHeight / yScale ) )
if view.xMin < 0 then
view.xMin = 0
end
if view.xMax > self.map.width then
view.xMax = self.map.width
end
if view.yMin < 0 then
view.yMin = 0
end
if view.yMax > self.map.height then
view.yMax = self.map.height
end
for x = view.xMin, view.xMax, 1 do
for y = view.yMin, view.yMax, 1 do
if not self.tileGrid then
self.tileGrid = {}
end
if self.tileGrid[x] then
if self.tileGrid[x][y] then
local tile = self.tileGrid[x][y]
if not tile.ignoreCulling then
if tile and tile.sprite and tile.gid ~= 0 then
if not tile.sprite.isVisible then
tile.sprite.isVisible = true
self.visibleTiles[ #self.visibleTiles + 1 ] = tile.sprite
end
end
end
end
end
end
end
end
end
--- Creates the visual representation of the layer.
-- @return The group containing the newly created layer.
function TileLayer:create()
if not self.map.world then
self.map.world = display.newGroup()
end
self.group = display.newGroup()
if(lime.isDebugModeEnabled()) then
print("Lime-Coconut: Creating layer - " .. self.name)
end
local tile = nil
for i=1, #self.tiles, 1 do
tile = self.tiles[i]
tile:create(i)
if(tile.sprite) then
else
-- If no sprite was created then chances are this was a blank section, delete the tile to save some memory (maybe). If problems start appearing simply comment out the next lines.
--tile:destroy()
--tile = nil
end
end
for key, value in pairs(self.properties) do
self.map:firePropertyListener(self.properties[key], "layer", self)
end
-- Override tile properties if this layer has been marked as static - NOTE: THIS IS A TEMPORARY FEATURE UNTIL A MORE FLEXIBLE / SECURE FEATURE SET HAS BEEN DECIDED ON!
if self:getPropertyValue("IsStatic") then
local tile = nil
for i=1, #self.tiles, 1 do
tile = self.tiles[i]
if tile.gid ~= 0 then
tile.HasBody = "true"
tile.bodyType = "static"
end
end
end
if self.opacity then
self.group.alpha = self.opacity
end
self.pixelwidth = self.width * self.map.tilewidth
self.pixelheight = self.height * self.map.tileheight
self.visible = ( self.visible == 1 or self.visible == nil )
return self.group
end
--- Builds the physical representation of the TileLayer.
function TileLayer:build()
if(lime.isDebugModeEnabled()) then
print("Lime-Banana: Building Tile Layer - " .. self.name)
end
for i=1, #self.tiles, 1 do
self.tiles[i]:build()
end
end
--- Completely removes all visual and physical objects associated with the TileLayer.
function TileLayer:destroy()
if self.group and self.tiles then
for i=1, #self.tiles, 1 do
self.tiles[i]:destroy()
end
self.tiles = nil
self.group:removeSelf()
self.group = nil
end
end