forked from mleveck/Crafty-Tiled-Map-Importer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TiledLevelImporter.js
120 lines (118 loc) · 3.66 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
// Generated by CoffeeScript 1.3.3
(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, _i, _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 = _i = 0, _ref = yCount * xCount; _i < _ref; i = _i += 1) {
posx = i % xCount;
posy = i / xCount | 0;
sName = "tileSprite" + tNum;
tName = "tile" + tNum;
sMap[sName] = [posx, posy];
components = "2D, " + drawType + ", " + sName + ", MapTile";
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, layerDetails, tDatum, tile, _i, _len;
lData = layer.data, lWidth = layer.width, lHeight = layer.height;
layerDetails = {
tiles: [],
width: lWidth,
height: lHeight
};
for (i = _i = 0, _len = lData.length; _i < _len; i = ++_i) {
tDatum = lData[i];
if (tDatum) {
tile = Crafty.e("tile" + tDatum);
tile.x = (i % lWidth) * tile.w;
tile.y = (i / lWidth | 0) * tile.h;
layerDetails.tiles[i] = tile;
}
}
this._layerArray.push(layerDetails);
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 != 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;
})();
Crafty.load(tsImages, function() {
var layer, _i, _j, _len, _len1;
for (_i = 0, _len = tss.length; _i < _len; _i++) {
ts = tss[_i];
_this.makeTiles(ts, drawType);
}
for (_j = 0, _len1 = lLayers.length; _j < _len1; _j++) {
layer = lLayers[_j];
_this.makeLayer(layer);
}
_this.trigger("TiledLevelLoaded", _this);
return null;
});
return null;
}
});
return this;
},
getTile: function(r, c, l) {
var layer, tile;
if (l == null) {
l = 0;
}
layer = this._layerArray[l];
if (!(layer != null) || r < 0 || r >= layer.height || c < 0 || c >= layer.width) {
return null;
}
tile = layer.tiles[c + r * layer.width];
if (tile) {
return tile;
} else {
return void 0;
}
},
init: function() {
this._layerArray = [];
return this;
}
});
}).call(this);