-
Notifications
You must be signed in to change notification settings - Fork 18
/
Tiled.js
114 lines (89 loc) · 3.29 KB
/
Tiled.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
(function (Ω) {
"use strict";
var Tiled = Ω.Class.extend({
w: null,
h: null,
tileW: null,
tileH: null,
layers: null,
init: function (file, onload) {
var self = this;
this.layers = [];
this.onload = onload;
// TODO: make sure not cached!
Ω.utils.ajax(file, function (xhr) {
// TODO: handle xhr errors
self.processLevel(JSON.parse(xhr.responseText));
});
},
layer: function (name) {
var layer = Ω.utils.getByKeyValue(this.layers, "name", name),
tileH = this.tileH,
adjustYPos = function (obj) {
// Weird thing of Tiled when stamping objects
if (obj.width === 0 && obj.height === 0) {
obj.y -= tileH;
}
return obj;
}
return {
get: function () {
return layer;
},
// Might add "optional" param and throw error if not found by default.
// Maybe param can be bool (if true, ok to be undefined - else if func, run it - else return as value
name: function (selector, defaultTo) {
var objs = layer.data.filter(function (obj) {
return obj.name === selector;
})
.map(adjustYPos);
return objs.length ? objs[0] : defaultTo;
},
type: function (selector, defaultTo) {
var objs = layer.data.filter(function (obj) {
return obj.type === selector;
})
.map(adjustYPos);
return objs.length === 0 && defaultTo ? defaultTo : objs;
}
}
},
processLevel: function (json) {
this.raw = json;
this.w = json.width;
this.h = json.height;
this.tileW = json.tilewidth;
this.tileH = json.tileheight;
this.properties = json.properties;
this.layers = json.layers.map(function (l) {
var data;
if (l.type === "tilelayer") {
// convert to 2d arrays.
data = l.data.reduce(function (acc, el) {
if (acc.length === 0 || acc[acc.length - 1].length % json.width === 0) {
acc.push([]);
}
acc[acc.length - 1].push(el);
return acc;
}, []);
} else {
// Parse the objects into something useful
data = l.objects.map(function (o) {
return o;
});
}
return {
name: l.name,
type: l.type,
data: data,
opacity: l.opacity,
visible: l.visible
};
});
if (this.onload) {
this.onload(this);
}
}
});
Ω.Tiled = Tiled;
}(Ω));