-
Notifications
You must be signed in to change notification settings - Fork 16
/
p5.tiledmap.js
executable file
·987 lines (892 loc) · 33.6 KB
/
p5.tiledmap.js
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
(function (root, factory) {
if (typeof define === 'function' && define.amd)
define('p5.tiledmap', ['p5'], function (p5) { (factory(p5));});
else if (typeof exports === 'object')
factory(require('../p5'));
else
factory(root['p5']);
}(this, function (p5) {
// =============================================================================
// p5.tiledmap Library
// =============================================================================
/**
* p5.tiledmap
* Add Tiled Maps to your sketch.
* What's not working (and may never will):
* Loading TMX files - p5.tiledMap uses Tiled JavaScript export, which must be loaded previously.
* External TileSets - must be imported to be part of JavaScript export.
* Tiles renderorder is not considered - always renders "right-down".
* Objects draworder is not considered.
* It's advisable to export Tiled maps to the same folder - pay attention to images path on js!
* The p5.tiledmap.js library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1.
* Last p5.tiledmap available on https://github.com/linux-man/p5.tiledmap
*/
p5.TiledMap = function(mapname, imagepath, transparentoffset) {
'use strict';
this.name = mapname;
this.path = imagepath;
this.drawmode = CORNER;
this.positionmode = "CANVAS";
this.transparentoffset = transparentoffset;
this.drawmargin = 2;
this.tile = [];
this.layerimage = [];
this.orientation = TileMaps[this.name].orientation;
this.tilewidth = TileMaps[this.name].tilewidth;
this.tileheight = TileMaps[this.name].tileheight;
this.mapwidth = TileMaps[this.name].width;
this.mapheight = TileMaps[this.name].height;
this.camleft = 0;
this.camtop = 0;
this.camwidth = parent.width;
this.camheight = parent.height;
this.staggeraxis = TileMaps[this.name].staggeraxis || "x";
this.staggerindex = Number(TileMaps[this.name].staggerindex == "even");
this.hexsidelength = TileMaps[this.name].hexsidelength || 0;
// Rewrite drawTileLayer, mapToCanvas and canvasToMap for the several geometries
switch(TileMaps[this.name].orientation) {
case "isometric":
drawTileLayer = function(layer, pg) {
'use strict';
var n, x, y, p, tileN;
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
var xstart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).x - this.drawmargin));
var xstop = min(this.mapwidth, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).x + this.drawmargin));
var ystart = max(0, floor(this.canvasToMap(this.camleft + pg.width, this.camtop).y - this.drawmargin));
var ystop = min(this.mapheight, ceil(this.canvasToMap(this.camleft, this.camtop + pg.height).y + this.drawmargin));
for (var ny = ystart; ny < ystop; ny++) for (var nx = xstart; nx < xstop; nx++) {
n = layer.data[nx + ny * this.mapwidth];
if (this.tile[n]) tileN = this.tile[n];
else tileN = this.tile[0];
p = this.mapToCam(nx, ny);
x = p.x - this.tilewidth / 2 + tileN.offsetx + offsetx;
y = p.y - this.tileheight / 2 + tileN.offsety + offsety + (this.tileheight - tileN.image.height);;
pg.image(tileN.image, x, y);
}
}
mapToCanvas = function(p) {
return createVector((this.mapwidth + this.mapheight) * this.tilewidth / 4 + (p.x - p.y) * this.tilewidth / 2, (p.x + p.y + 1) * this.tileheight / 2);
}
canvasToMap = function(p) {
var dif = (this.mapwidth + this.mapheight) * this.tilewidth / 4;
return createVector(p.y / this.tileheight + ((p.x - dif) / this.tilewidth) - 0.5, p.y / this.tileheight - ((p.x - dif) / this.tilewidth) - 0.5);
}
break;
case "staggered":
this.hexsidelength = 0;
case "hexagonal":
switch(this.staggeraxis) {
case("x"):
drawTileLayer = function(layer, pg) {
'use strict';
var n, x, y, p, tileN;
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
var xstart = floor(this.canvasToMap(this.camleft, this.camtop).x - this.drawmargin)
xstart = max(0, xstart - xstart % 2);
var xstop = min(this.mapwidth, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).x + this.drawmargin));
var ystart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).y - this.drawmargin));
var ystop = min(this.mapheight, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).y + this.drawmargin));
for (var ny = ystart; ny < ystop; ny++) for (var nx = xstart; nx <= xstop; nx+=2) for (var nz = this.staggerindex; nz >= this.staggerindex - 1; nz--) {
if (nx + nz >= 0 && nx + nz < this.mapwidth) {
n = layer.data[nx + nz + ny * this.mapwidth];
if (this.tile[n]) tileN = this.tile[n];
else tileN = this.tile[0];
p = this.mapToCam(nx + nz, ny);
x = p.x - this.tilewidth / 2 + tileN.offsetx + offsetx;
y = p.y - this.tileheight / 2 + tileN.offsety + offsety + (this.tileheight - tileN.image.height);
pg.image(tileN.image, x, y);
}
}
}
mapToCanvas = function(p) {
var x = (p.x * (this.hexsidelength + this.tilewidth) + this.tilewidth) / 2;
var y = (p.y + 0.5 + this.staggerindex / 2) * this.tileheight + (this.staggerindex * 2 - 1) * (abs(abs(p.x) % 2 - 1) - 1) * (this.tileheight) / 2;
return createVector(x, y);
}
canvasToMap = function(p) {
var ny;
var nx = (p.x * 2 - this.tilewidth) / (this.hexsidelength + this.tilewidth);
if (this.staggerindex == 0) ny = p.y / this.tileheight - 1 + (abs(abs(nx) % 2 - 1)) / 2;
else ny = p.y / this.tileheight - 0.5 - (abs(abs(nx) % 2 - 1)) / 2;
return createVector(nx, ny);
}
break;
case("y"):
drawTileLayer = function(layer, pg) {
'use strict';
var n, x, y, p, tileN;
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
var xstart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).x - this.drawmargin));
var xstop = min(this.mapwidth, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).x + this.drawmargin));
var ystart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).y - this.drawmargin));
var ystop = min(this.mapheight, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).y + this.drawmargin));
for (var ny = ystart; ny < ystop; ny++) for (var nx = xstart; nx < xstop; nx++) {
n = layer.data[nx + ny * this.mapwidth];
if (this.tile[n]) tileN = this.tile[n];
else tileN = this.tile[0];
p = this.mapToCam(nx, ny);
x = p.x - this.tilewidth / 2 + tileN.offsetx + offsetx;
y = p.y - this.tileheight / 2 + tileN.offsety + offsety + (this.tileheight - tileN.image.height);;
pg.image(tileN.image, x, y);
}
}
mapToCanvas = function(p) {
var y = p.y * (this.hexsidelength + this.tileheight) / 2 + this.tileheight / 2;
var x = (p.x + 0.5 + this.staggerindex / 2) * this.tilewidth + (this.staggerindex * 2 - 1) * (abs(abs(p.y) % 2 - 1) - 1) * (this.tilewidth) / 2;
return createVector(x, y);
}
canvasToMap = function(p) {
var nx;
var ny = (p.y * 2 - this.tileheight) / (this.hexsidelength + this.tileheight);
if (this.staggerindex == 0) nx = p.x / this.tilewidth - 1 + (abs(abs(ny) % 2 - 1)) / 2;
else nx = p.x / this.tilewidth - 0.5 - (abs(abs(ny) % 2 - 1)) / 2;
return createVector(nx, ny);
}
break;
}
break;
default: // drawTileLayer for Orthogonal geometry (default)
drawTileLayer = function(layer, pg) {
'use strict';
var n, x, y, p, tileN;
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
var xstart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).x - this.drawmargin));
var xstop = min(this.mapwidth, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).x + this.drawmargin));
var ystart = max(0, floor(this.canvasToMap(this.camleft, this.camtop).y - this.drawmargin));
var ystop = min(this.mapheight, ceil(this.canvasToMap(this.camleft + pg.width, this.camtop + pg.height).y + this.drawmargin));
for (var ny = ystart; ny < ystop; ny++) for (var nx = xstart; nx < xstop; nx++) {
n = layer.data[nx + ny * this.mapwidth];
if (this.tile[n]) tileN = this.tile[n];
else tileN = this.tile[0];
p = this.mapToCam(nx, ny);
x = p.x - this.tilewidth / 2 + tileN.offsetx + offsetx;
y = p.y - this.tileheight / 2 + tileN.offsety + offsety + (this.tileheight - tileN.image.height);;
pg.image(tileN.image, x, y);
}
}
mapToCanvas = function(p) {
return createVector((p.x + 0.5) * this.tilewidth, (p.y + 0.5) * this.tileheight)
}
canvasToMap = function(p) {
return createVector(p.x / this.tilewidth - 0.5, p.y / this.tileheight - 0.5)
}
}
for (var n = 0; n < TileMaps[this.name].layers.length; n++) {
var layer = TileMaps[this.name].layers[n];
if (layer.type == "imagelayer") this.layerimage[layer.image] = loadImage(this.path + layer.image);
}
this.tile[0] = new Tile(createImage(1,1), 0, 0);
cicleTilesets.call(this, 0);
}
// =============================================================================
// Private Functions
// =============================================================================
Tile = function(image, offsetx, offsety) {
'use strict';
this.image = image;
this.offsetx = offsetx;
this.offsety = offsety;
}
function cicleTilesets(n) {
'use strict';
if (n < TileMaps[this.name].tilesets.length) {
var tileset = TileMaps[this.name].tilesets[n];
if (tileset.image){
loadImage(this.path + tileset.image, (function(img) {loadTiles.call(this, img, n)}).bind(this));
}
else if (tileset.tiles) {
var firstgid = tileset.firstgid;
if (tileset.tileoffset) {
var tileoffsetx = tileset.tileoffset.x || 0;
var tileoffsety = tileset.tileoffset.y || 0;
}
else {
var tileoffsetx = 0;
var tileoffsety = 0;
}
for (var k in tileset.tiles) {
var img = loadImage(this.path + tileset.tiles[k].image);
this.tile[firstgid + int(k)] = new Tile(img, tileoffsetx, tileoffsety);
}
}
}
}
function loadTiles(imgsrc, n) {
'use strict';
var tileset = TileMaps[this.name].tilesets[n];
var firstgid = tileset.firstgid;
var tilewidth = tileset.tilewidth;
var tileheight = tileset.tileheight;
var tilecount = tileset.tilecount || 0;
var columns = tileset.columns || 0;
var spacing = tileset.spacing || 0;
var margin = tileset.margin || 0;
if (tileset.tileoffset) {
var tileoffsetx = tileset.tileoffset.x || 0;
var tileoffsety = tileset.tileoffset.y || 0;
}
else {
var tileoffsetx = 0;
var tileoffsety = 0;
}
if (columns == 0) columns = floor((imgsrc.width - margin) / (tilewidth + spacing));
if (tilecount == 0) tilecount = columns * floor((imgsrc.height - margin) / (tileheight + spacing));
if (tileset.transparentcolor) applyTransColor(imgsrc, color(tileset.transparentcolor), this.transparentoffset);
for (var m = 0; m < tilecount; m++) {
var img = imgsrc.get(margin + (m % columns) * (tilewidth + spacing), margin + floor(m / columns) * (tileheight + spacing), tilewidth, tileheight);
this.tile[firstgid + m] = new Tile(img, tileoffsetx, tileoffsety);
}
cicleTilesets.call(this, ++n);
}
function applyTransColor(img, trans, offset) {
'use strict';
img.loadPixels();
for (var p = 0; p < img.pixels.length; p+=4) {
if (abs(img.pixels[p] - red(trans)) < offset
&& abs(img.pixels[p+1] - green(trans)) < offset
&& abs(img.pixels[p+2] == blue(trans)) < offset) {
img.pixels[p+3] = 0;
}
}
img.updatePixels();
}
// Functions skeleton
drawTileLayer = function(layer, pg) {}
mapToCanvas = function(p) {}
canvasToMap = function(p) {}
// =============================================================================
// Public Methods
// =============================================================================
p5.prototype.registerPreloadMethod('loadTiledMap', p5.prototype);
/**
* loadTiledMap() returns a new p5.TiledMap.
* TiledMap uses Tiled JavaScript export, which must be loaded previously:
* <script language="javascript" type="text/javascript" src="data/desert.js"></script>
* External TileSets are not supported, just because they are not part of the script.
* If called during preload(), the p5.TiledMap will be ready to play in time for setup()
* and draw().
* If called outside of preload, the p5.TiledMap will not be ready immediately,
* so loadTiledMap accepts a callback as the last parameter.
*
* @method loadTiledMap
* @param {String} mapName Map name.
* @param {String} [imagePath] Path to the image file(s). Default: "/".
* @param {Number} [transparentOffset] Maximum difference on RGB channels to apply Tile
* transparency. Default: 4.
* @param {Function} [callback] Name of a function to call once map loads.
* @return {p5.TiledMap}
* @example
* <div><code>
* function preload() {
* tmap = loadTiledMap("desert", "data");
* }
*
* function setup() {
* createCanvas(800, 600);
* }
* function draw() {
* background(128);
* tmap.draw(0, 0);
* }
* </code></div>
*/
p5.prototype.loadTiledMap = function () {
'use strict';
var mapname = arguments[0];
var imagepath = "";
var transparentoffset = 4;
var callback = null;
for (var i = 1; i < arguments.length; i++) {
if (typeof(arguments[i]) === 'string') imagepath = arguments[i];
if (typeof(arguments[i]) === 'number') transparentoffset = arguments[i];
if (typeof(arguments[i]) === 'function') callback = arguments[i];
}
if (imagepath[-1] != "/") imagepath = imagepath + "/";
if (!TileMaps) throw "No Tiled Map found!";
if (!TileMaps[mapname]) throw "No Tiled Map named "+mapname+" found!";
var t = new p5.TiledMap(mapname, imagepath, transparentoffset);
if (typeof self._decrementPreload === 'function') self._decrementPreload(); //Direct calls to _decrementPreload from all loading functions (0.5.12^)
if (typeof(callback) === 'function') callback(t);
return t;
}
/**
* drawLayer() draws a TiledMap Layer.
* Visible property is ignored when drawing individual layers.
* The use of a off-screen graphics buffer is optional. Can be used for extra image editing or
* if you want to use the layer opacity property (which is ignored when drawing to canvas).
* Canvas (or the off-screen graphics buffer) IS NOT CLEARED BEFORE (that's up to you).
* That means you can draw several layers on a buffer, but the layer opacity will affect all
* layers drawn before.
*
* @method drawLayer
* @param {Number} layer Layer Index.
* @param {Number} camLeft Left Coordinate.
* @param {Number} camTop Top Coordinate.
* @param {Object} [pg] Off-screen graphics buffer.
* @example
* <div><code>
* function preload() {
* tmap = loadTiledMap("desert", "data");
* }
*
* function setup() {
* createCanvas(800, 600);
* pg = createGraphics(800, 600);
* }
* function draw() {
* pg.clear();
* tmap.drawLayer(0, 0, 0, pg);
* image(pg, 0, 0);
* }
* </code></div>
*/
p5.TiledMap.prototype.drawLayer = function(n, x, y, pg) {
'use strict';
if (!pg) pg = parent;
this.camwidth = pg.width;
this.camheight = pg.height;
if (this.positionmode == "MAP") {
var pos = this.mapToCanvas(x, y);
x = pos.x;
y = pos.y;
}
if (this.drawmode == CENTER) {
this.camleft = x - this.camwidth / 2;
this.camtop = y - this.camheight / 2;
}
else{
this.camleft = x;
this.camtop = y;
}
pg.push();
pg.resetMatrix();
pg.ellipseMode(CORNER);
pg.angleMode(DEGREES);
pg.imageMode(CORNER);
var layer = TileMaps[this.name].layers[n];
switch (layer.type) {
case "tilelayer":
drawTileLayer.call(this, layer, pg);
break;
case "imagelayer":
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
pg.image(this.layerimage[layer.image], -this.camleft + offsetx, -this.camtop + offsety);
break;
case "objectgroup":
var offsetx = layer.offsetx || 0;
var offsety = layer.offsety || 0;
pg.fill(color(layer.color || 0));
pg.stroke(color(layer.color || 0));
pg.strokeWeight(1);
for (var n in layer.objects) {
var o = layer.objects[n];
if (o.visible) {
pg.push();
pg.resetMatrix();
pg.translate(o.x - offsetx - this.camleft, o.y - offsety - this.camtop);
if (o.rotation) pg.rotate(o.rotation);
if (o.ellipse) pg.ellipse(0, 0, o.width, o.height);
else if (o.polyline) {
pg.noFill();
pg.beginShape();
for (var p in o.polyline) pg.vertex(o.polyline[p].x, o.polyline[p].y);
pg.endShape();
}
else if (o.polygon) {
pg.beginShape();
for (var p in o.polygon) pg.vertex(o.polygon[p].x, o.polygon[p].y);
pg.endShape(CLOSE);
}
else if (o.gid) {
if (o.rotation) pg.rotate(-o.rotation);
pg.translate(0, -this.tile[o.gid].image.height);
if (o.rotation) pg.rotate(o.rotation);
pg.image(this.tile[o.gid].image, this.tile[o.gid].offsetx, this.tile[o.gid].offsety, o.width, o.height);
}
else pg.rect(0, 0, o.width, o.height);
pg.pop();
}
}
break;
}
if ((pg !== parent) && (layer.opacity < 1)) { //applyOpacity
pg.loadPixels();
var op = map(layer.opacity, 0, 1, 0, 255);
for (var p = 3; p < pg.pixels.length; p+=4) if (pg.pixels[p] > op) pg.pixels[p] = op;
pg.updatePixels();
}
pg.pop();
}
/**
* draw() draws a TiledMap.
* Layer Visible property is respected.
* Opacity is ignored.
* The use of a off-screen graphics buffer is optional.
* Canvas (or the off-screen graphics buffer) IS NOT CLEARED BEFORE.
*
* @method draw
* @param {Number} camLeft Left Coordinate.
* @param {Number} camTop Top Coordinate.
* @param {Object} [pg] Off-screen graphics buffer.
* @example
* <div><code>
* function preload() {
* tmap = loadTiledMap("desert", "data");
* }
*
* function setup() {
* createCanvas(800, 600);
* }
* function draw() {
* background(128);
* tmap.draw(0, 0);
* }
* </code></div>
*/
p5.TiledMap.prototype.draw = function(camleft, camtop, pg) {
var o;
for (var n = 0; n < TileMaps[this.name].layers.length; n++)
if (TileMaps[this.name].layers[n].visible) {
o = TileMaps[this.name].layers[n].opacity;
TileMaps[this.name].layers[n].opacity = 1;
this.drawLayer(n, camleft, camtop, pg);
TileMaps[this.name].layers[n].opacity = o;
}
}
/**
* Maps are stored and accessible in TiledMaps Object.
* These are helper methods to ease Map and Layer management.
*/
/**
* @method getName
* @return {String} Map Name
*/
p5.TiledMap.prototype.getName = function() {
'use strict';
return this.name;
}
/**
* @method getVersion
* @return {Number} Map Version
*/
p5.TiledMap.prototype.getVersion = function() {
'use strict';
return TileMaps[this.name].version;
}
/**
* @method getOrientation
* @return {String} Map Orientation
*/
p5.TiledMap.prototype.getOrientation = function() {
'use strict';
return TileMaps[this.name].orientation;
}
/**
* @method getBackgroundColor
* @return {Array} P5 Color
*/
p5.TiledMap.prototype.getBackgroundColor = function() {
'use strict';
if (TileMaps[this.name].backgroundcolor)
return color(TileMaps[this.name].backgroundcolor);
else return color(255);
}
/**
* @method getMapSize
* @return {p5.Vector} Map Width and Height (in number of tiles)
*/
p5.TiledMap.prototype.getMapSize = function() {
return createVector(this.mapwidth, this.mapheight);
}
/**
* @method getTileSize
* @return {p5.Vector} Tile Width and Height (in pixels)
*/
p5.TiledMap.prototype.getTileSize = function() {
return createVector(this.tilewidth, this.tileheight);
}
/**
* @method getHexSideLength
* @return {Number} Only for Hexagonal Maps
*/
p5.TiledMap.prototype.getHexSideLength = function() {
return this.hexsidelength;
}
/**
* @method getCamCorner
* @return {p5.Vector} Left Top Corner Coordinates
*/
p5.TiledMap.prototype.getCamCorner = function() {
if (this.positionmode == "MAP")
return this.canvasToMap(this.camleft, this.camtop);
else return createVector(this.camleft, this.camtop);
}
/**
* @method getCamCenter
* @return {p5.Vector} Center Coordinates
*/
p5.TiledMap.prototype.getCamCenter = function() {
if (this.positionmode == "MAP")
return this.canvasToMap(this.camleft + this.camwidth / 2, this.camtop + this.camheight / 2);
else return createVector(this.camleft + this.camwidth / 2, this.camtop + this.camheight / 2);
}
/**
* Depending on drawMode, returns the camera corner or center coordinates.
* Depending on positionMode, returns the Map or Canvas coordinates.
* Essentially, you get the coordinates of last draw.
*
* @method getPosition
* @return {p5.Vector}
*/
p5.TiledMap.prototype.getPosition = function() {
if (this.drawmode == parent.CORNER) return this.getCamCorner();
else return this.getCamCenter();
}
/**
* @method getCamSize
* @return {p5.Vector} Camera (last used Canvas) Width and Height
*/
p5.TiledMap.prototype.getCamSize = function() {
return createVector(this.camwidth, this.camheight);
}
/**
* Only useful for some pre-draw calculations, since CamSize is always the last Canvas used to draw.
*
* @method setCamSize
* @param {P5.Vector} (width, height)
*/
/**
* Only useful for some pre-draw calculations, since Cam size is always the last Canvas used to draw.
*
* @method setCamSize
* @param {Number} width
* @param {Number} height
*/
p5.TiledMap.prototype.setCamSize = function(a, b) {
if (typeof(a) === "object") {
this.camwidth = a.x;
this.camheight = a.y;
}
else {
this.camwidth = a;
this.camheight = b;
}
}
/**
* @method getDrawMargin
* @return {Number} margin Number of tiles to be draw in excess around Canvas. Default: 2.
*/
p5.TiledMap.prototype.getDrawMargin = function() {
return this.drawmargin;
}
/**
* @method setDrawMargin
* @param {Number} margin Number of tiles to be draw in excess around Canvas. Default: 2.
*/
p5.TiledMap.prototype.setDrawMargin = function(n) {
this.drawmargin = n;
}
/**
* @method getDrawMode
* @return {Constant} mode CORNER or CENTER. Default: CORNER.
*/
p5.TiledMap.prototype.getDrawMode = function() {
return this.drawmode;
}
/**
* Define the meaning of draw and drawLayer coordinates.
* By default, they are camLeft and camTop - the left/top coordinates of the camera,
* but with setDrawMode(CENTER), they become the coordinates of the camera center.
*
* @method setDrawMode
* @param {Constant} mode CORNER or CENTER
*/
p5.TiledMap.prototype.setDrawMode = function(s) {
if (s == parent.CORNER || s == parent.CENTER) this.drawmode = s;
}
/**
* @method getPositionMode
* @return {String} mode "CANVAS" or "MAP". Default: "CANVAS".
*/
p5.TiledMap.prototype.getPositionMode = function() {
return this.positionmode;
}
/**
* Define the meaning of draw and drawLayer coordinates.
* By default, the coordinates are read as pixel position,
* but with setPositionMode("MAP"), they become Tiles Coordinates.
*
* @method setPositionMode
* @param {String} mode "CANVAS" or "MAP".
*/
p5.TiledMap.prototype.setPositionMode = function(s) {
if (s == "CANVAS" || s == "MAP") this.positionmode = s;
}
/**
* @method getType
* @param {Number} layer Layer Index.
* @return {String} Returns Layer Type ("tilelayer", "imagelayer" or "objectgroup").
*/
p5.TiledMap.prototype.getType = function(n) {
'use strict';
return TileMaps[this.name].layers[n].type;
}
/**
* @method getVisible
* @param {Number} layer Layer Index.
* @return {Boolean} Returns Layer Visibility.
*/
p5.TiledMap.prototype.getVisible = function(n) {
'use strict';
return TileMaps[this.name].layers[n].visible;
}
/**
* @method setVisible
* @param {Number} layer Layer Index.
* @param {Boolean} visible Visible value.
*/
p5.TiledMap.prototype.setVisible = function(n, v) {
'use strict';
TileMaps[this.name].layers[n].visible = v;
}
/**
* @method getImage
* @param {Number} layer Layer Index.
* @return {p5.Image} Image of a Image Layer.
*/
p5.TiledMap.prototype.getImage = function(n) {
'use strict';
return this.layerimage[TileMaps[this.name].layers[n].image];
}
/**
* @method getObjects
* @param {Number} layer Layer Index.
* @return {Array} Array of Objects on a Object Layer.
*/
p5.TiledMap.prototype.getObjects = function(n) {
'use strict';
return TileMaps[this.name].layers[n].objects;
}
/**
* @method getObjectsColor
* @param {Number} layer Layer Index.
* @return {Array} P5 Color of an Object Layer. Default: Black.
*/
p5.TiledMap.prototype.getObjectsColor = function(n) {
'use strict';
return color(TileMaps[this.name].layers[n].color || 0);
}
/**
* @method getData
* @param {Number} layer Layer Index.
* @return {Array} Data of a Tile Layer.
*/
p5.TiledMap.prototype.getData = function(n) {
'use strict';
return TileMaps[this.name].layers[n].data;
}
/**
* @method getCustomProperties
* @param {Number} layer Layer Index.
* @return {Object} Custom Properties of a Layer.
*/
p5.TiledMap.prototype.getCustomProperties = function(n) {
'use strict';
return TileMaps[this.name].layers[n].properties;
}
/**
* @method getOpacity
* @param {Number} layer Layer Index.
* @return {Number} Opacity of a Layer (between 0 and 1).
*/
p5.TiledMap.prototype.getOpacity = function(n) {
'use strict';
return TileMaps[this.name].layers[n].opacity;
}
/**
* @method setOpacity
* @param {Number} layer Layer Index.
* @param {Number} opacity Opacity of the Layer (between 0 and 1).
*/
p5.TiledMap.prototype.setOpacity = function(n, o) {
'use strict';
TileMaps[this.name].layers[n].opacity = min(max(0, o), 1);
}
/**
* getTileIndex return the tile index. Remember that:
* - x and y are Integers.
* - 0 is an empty tile.
* - The stored tile index is the index indicated by Tiled +1.
*
* @method getTileIndex
* @param {Number} layer Layer Index.
* @param {Number} x Horizontal Coordinate.
* @param {Number} y Vertical Coordinate.
* @return {Number} Tile Index.
*/
/**
* getTileIndex return the tile index. Remember that:
* - x and y are Integers.
* - 0 is an empty tile.
* - The stored tile index is the index indicated by Tiled +1.
*
* @method getTileIndex
* @param {Number} layer Layer Index.
* @param {p5.Vector} v Coordinates.
* @return {Number} Tile Index.
*/
p5.TiledMap.prototype.getTileIndex = function(n, a, b) {
'use strict';
var x, y;
if (typeof(a) === "object") {x = a.x; y = a.y;}
else {x = a; y = b;}
if (TileMaps[this.name].layers[n].type == "tilelayer" && x >= 0 && y >= 0 && x < this.mapwidth && y < this.mapheight)
return TileMaps[this.name].layers[n].data[x + y * this.mapwidth];
else return undefined;
}
/**
* setTileIndex changes a tile index. Remember that:
* - x, y and t are Integers.
* - 0 is an empty tile.
* - The stored tile index is the index presented by Tiled +1.
*
* @method setTileIndex
* @param {Number} layer Layer Index.
* @param {Number} x Horizontal Coordinate.
* @param {Number} y Vertical Coordinate.
* @param {Number} t Tile Index.
*/
/**
* setTileIndex changes a tile index. Remember that:
* - x, y and t are Integers.
* - 0 is an empty tile.
* - The stored tile index is the index presented by Tiled +1.
*
* @method setTileIndex
* @param {Number} layer Layer Index.
* @param {p5.Vector} v Coordinates.
* @param {Number} t Tile Index.
*/
p5.TiledMap.prototype.setTileIndex = function(n, a, b, c) {
'use strict';
var x, y, t;
if (typeof(a) === "object") {x = a.x; y = a.y; t = b;}
else {x = a; y = b; t = c;}
if (TileMaps[this.name].layers[n].type == "tilelayer" && x >= 0 && y >= 0 && x < this.mapwidth && y < this.mapheight)
TileMaps[this.name].layers[n].data[x + y * this.mapwidth] = t;
}
/**
* Conversion Methods between the 3 coordinate systems in use:
* MAP (Tile coordinates), CANVAS (Pixel coordinates) and CAM (relative coordinates of the draw)
*/
/**
* @method canvasToMap
* @param {Number} x Canvas Horizontal Coordinate.
* @param {Number} y Canvas Vertical Coordinate.
* @return {p5.Vector} Map Coordinates
*/
/**
* @method canvasToMap
* @param {p5.Vector} v Canvas Coordinates.
* @return {p5.Vector} Map Coordinates
*/
p5.TiledMap.prototype.canvasToMap = function(a, b) {
if (typeof(a) === "number") p = createVector(a, b);
else p = a;
return canvasToMap.call(this, p);
}
/**
* @method mapToCanvas
* @param {Number} x Map Horizontal Coordinate.
* @param {Number} y Map Vertical Coordinate.
* @return {p5.Vector} Canvas Coordinates
*/
/**
* @method mapToCanvas
* @param {p5.Vector} v Map Coordinates.
* @return {p5.Vector} Canvas Coordinates
*/
p5.TiledMap.prototype.mapToCanvas = function(a, b) {
var p;
if (typeof(a) === "number") p = createVector(a, b);
else p = a;
return mapToCanvas.call(this, p);
}
/**
* @method camToCanvas
* @param {Number} x Cam Horizontal Coordinate.
* @param {Number} y Cam Vertical Coordinate.
* @return {p5.Vector} Canvas Coordinates
*/
/**
* @method camToCanvas
* @param {p5.Vector} v Cam Coordinates.
* @return {p5.Vector} Canvas Coordinates
*/
p5.TiledMap.prototype.camToCanvas = function(a, b) {
var x, y;
if (typeof(a) === "object") {x = a.x; y = a.y;}
else {x = a; y = b;}
return createVector(x + this.camleft, y + this.camtop);
}
/**
* @method canvasToCam
* @param {Number} x Canvas Horizontal Coordinate.
* @param {Number} y Canvas Vertical Coordinate.
* @return {p5.Vector} Cam Coordinates
*/
/**
* @method canvasToCam
* @param {p5.Vector} v Canvas Coordinates.
* @return {p5.Vector} Cam Coordinates
*/
p5.TiledMap.prototype.canvasToCam = function(a, b) {
var x, y;
if (typeof(a) === "object") {x = a.x; y = a.y;}
else {x = a; y = b;}
return createVector(x - this.camleft, y - this.camtop);
}
/**
* @method camToMap
* @param {Number} x Cam Horizontal Coordinate.
* @param {Number} y Cam Vertical Coordinate.
* @return {p5.Vector} Map Coordinates
*/
/**
* @method camToMap
* @param {p5.Vector} v Cam Coordinates.
* @return {p5.Vector} Map Coordinates
*/
p5.TiledMap.prototype.camToMap = function(a, b) {
var x, y;
if (typeof(a) === "object") {x = a.x; y = a.y;}
else {x = a; y = b;}
return this.canvasToMap(this.camToCanvas(x, y));
}
/**
* @method mapToCam
* @param {Number} x Map Horizontal Coordinate.
* @param {Number} y Map Vertical Coordinate.
* @return {p5.Vector} Cam Coordinates
*/
/**
* @method mapToCam
* @param {p5.Vector} v Map Coordinates.
* @return {p5.Vector} Cam Coordinates
*/
p5.TiledMap.prototype.mapToCam = function(a, b) {
var nx, ny;
if (typeof(a) === "object") {nx = a.x; ny = a.y;}
else {nx = a; ny = b;}
return this.canvasToCam(this.mapToCanvas(nx, ny));
}
// =============================================================================
// The End
// =============================================================================
}));