forked from mleveck/Crafty-Tiled-Map-Importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiledLevelImporter.js
97 lines (94 loc) · 2.9 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
(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;
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 = {};
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];
components = ["2D", drawType, sName, "MapTile"].join(",");
if (tsProperties) {
if (tsProperties[tNum - 1]) {
if (tsProperties[tNum - 1]["components"]) {
components += ", " + tsProperties[tNum - 1]["components"];
}
}
}
Crafty.c(tName, {
comp: components,
init: function() {
this.addComponent(this.comp);
return this;
}
});
tNum++;
}
Crafty.sprite(tWidth, tHeight, tsImage, sMap);
return null;
},
makeLayer: function(layer) {
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++) {
tDatum = lData[i];
if (tDatum) {
tile = Crafty.e("tile" + tDatum);
tile.x = (i % lWidth) * tile.w;
tile.y = (i / lWidth | 0) * tile.h;
}
}
return null;
},
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 || "Canvas";
tsImages = _getImages(tss);
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];
_this.makeLayer(layer);
}
return null;
});
return null;
}
});
return this;
},
_getImages: function(tss){
var i, len, results;
results = [];
for (i = 0, len = tss.length; i < len; i++) {
results.push(tss[i].image);
}
return results;
},
init: function() {
return this;
}
});
}).call(this);