forked from mleveck/Crafty-Tiled-Map-Importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiledLevelImporter.js
137 lines (113 loc) · 4.15 KB
/
TiledLevelImporter.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
(function() {
Crafty.c("TiledLevel", {
makeTiles: function(ts, drawType) {
var components, i, posx, posy, sMap, sName, tHeight, tName, tNum, tWidth, tsHeight,
tsImage, tsProperties, tsWidth, xCount, yCount, _ref, rect;
tsImage = ts.image, tNum = ts.firstgid, tsWidth = ts.imagewidth;
tsHeight = ts.imageheight, tWidth = ts.tilewidth, tHeight = ts.tileheight;
tsProperties = ts.tileproperties;
xCount = tsWidth / tWidth | 0;
yCount = tsHeight / tHeight | 0;
sMap = {};
// loop through tilset tiles
for (i = 0, _ref = yCount * xCount; i < _ref; i += 1) {
posx = i % xCount;
posy = i / xCount | 0;
sName = "tileSprite" + tNum;
tName = "tile" + tNum;
sMap[sName] = [posx, posy];
rect = [];
components = "2D, " + sName + ", MapTile, Collision";
if (tsProperties && tsProperties[i]) {
if (tsProperties[i]["components"]) {
components += ", " + tsProperties[i]["components"];
} // components
//collision rect in the form of "rect":"x,y,w,h"
if (tsProperties[i]["rect"]) {
var rectstr = tsProperties[i]["rect"].split(",");
for(var rr=0; rr<rectstr.length; rr++) { rect[rr] = +rectstr[rr]; }
} // rect
} // if tile properties
// register Crafty component that is this unique tiletype
Crafty.c(tName, {
comp: components,
cRect: rect,
init: function() {
this.addComponent(this.comp);
return this;
}
});
tNum++;
} // loop tileset tiles
Crafty.sprite(tWidth, tHeight, tsImage, sMap);
return null;
}, // makeTiles()
makeLayer: function(layer, drawType) {
// make sure it's actually a tilelayer
if (layer.type == "tilelayer") {
var i, lData, lHeight, lWidth, tDatum, tile, _len;
lData = layer.data, lWidth = layer.width, lHeight = layer.height;
for (i = 0, _len = lData.length; i < _len; i++) {
if (lData[i] == null)
continue;
tDatum = lData[i];
if (drawType != null)
tile = Crafty.e(drawType + ", tile" + tDatum);
else
tile = Crafty.e("tile" + tDatum);
tile.x = (i % lWidth) * tile.w;
tile.y = (i / lWidth | 0) * tile.h;
tile.z = layer.level;
if( tile.cRect != null && tile.cRect.length == 4) {
var c = tile.cRect;
tile.collision([c[0],c[1]],[c[0]+c[2],c[1]],[c[0]+c[2],c[1]+c[3]],[c[0],c[1]+c[3]]);
} // add collision 'rect' as quad
} // loop data
} // if type is 'tilelayer'
return null;
}, // makeLayer()
tiledLevel: function(levelURL, drawType) {
var _this = this;
$.ajax({
type: 'GET',
url: levelURL,
dataType: 'json',
data: {},
async: false,
success: function(level) {
var lLayers, ts, tsImages, tss;
lLayers = level.layers, tss = level.tilesets;
drawType = drawType != null ? drawType : "Canvas";
tsImages = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = tss.length; _i < _len; _i++) {
ts = tss[_i];
_results.push(ts.image);
}
return _results;
})(); // list images
Crafty.load(tsImages, function() {
var layer, ts, _i, _j, _len, _len2;
for (_i = 0, _len = tss.length; _i < _len; _i++) {
ts = tss[_i];
_this.makeTiles(ts, drawType);
}
for (_j = 0, _len2 = lLayers.length; _j < _len2; _j++) {
layer = lLayers[_j];
layer.level = _j;
// if the layer is not visible, don't give it a drawtype
_this.makeLayer(layer, layer.visible?drawType:null);
}
return null;
}); // load images
return null;
} // ajax success
}); // ajax request
return this;
}, // tiledLevel()
init: function() {
return this;
} // init()
});
}).call(this);