-
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathTMXObjectGroup.js
405 lines (362 loc) · 11.6 KB
/
TMXObjectGroup.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
/*
* MelonJS Game Engine
* Copyright (C) 2011 - 2015, Olivier Biot, Jason Oster, Aaron McLeod
* http://www.melonjs.org
*
* Tile QT 0.7.x format
* http://www.mapeditor.org/
*
*/
(function () {
/**
* TMX Object Group <br>
* contains the object group definition as defined in Tiled. <br>
* note : object group definition is translated into the virtual `me.game.world` using `me.Container`.
* @see me.Container
* @class
* @extends Object
* @memberOf me
* @constructor
*/
me.TMXObjectGroup = me.Object.extend({
init : function (name, tmxObjGroup, orientation, tilesets, z) {
/**
* group name
* @public
* @type String
* @name name
* @memberOf me.TMXObjectGroup
*/
this.name = name;
/**
* group width
* @public
* @type Number
* @name width
* @memberOf me.TMXObjectGroup
*/
this.width = tmxObjGroup.width;
/**
* group height
* @public
* @type Number
* @name height
* @memberOf me.TMXObjectGroup
*/
this.height = tmxObjGroup.height;
/**
* group z order
* @public
* @type Number
* @name z
* @memberOf me.TMXObjectGroup
*/
this.z = z;
/**
* group objects list definition
* @see me.TMXObject
* @public
* @type Array
* @name name
* @memberOf me.TMXObjectGroup
*/
this.objects = [];
var visible = typeof(tmxObjGroup.visible) !== "undefined" ? tmxObjGroup.visible : true;
this.opacity = (visible === true) ? (+tmxObjGroup.opacity || 1.0).clamp(0.0, 1.0) : 0;
// check if we have any user-defined properties
me.TMXUtils.applyTMXProperties(this, tmxObjGroup);
// parse all objects
var _objects = tmxObjGroup.objects;
if (_objects) {
var self = this;
_objects.forEach(function (tmxObj) {
self.objects.push(new me.TMXObject(tmxObj, orientation, tilesets, z));
});
}
},
/**
* reset function
* @ignore
* @function
*/
destroy : function () {
// clear all allocated objects
this.objects = null;
},
/**
* return the object count
* @ignore
* @function
*/
getObjectCount : function () {
return this.objects.length;
},
/**
* returns the object at the specified index
* @ignore
* @function
*/
getObjectByIndex : function (idx) {
return this.objects[idx];
}
});
/**
* a TMX Object defintion, as defined in Tiled. <br>
* note : object definition are translated into the virtual `me.game.world` using `me.Entity`.
* @see me.Entity
* @class
* @extends Object
* @memberOf me
* @constructor
*/
me.TMXObject = me.Object.extend({
init : function (tmxObj, orientation, tilesets, z) {
/**
* object point list (for Polygon and PolyLine)
* @public
* @type Vector2d[]
* @name points
* @memberOf me.TMXObject
*/
this.points = undefined;
/**
* object name
* @public
* @type String
* @name name
* @memberOf me.TMXObject
*/
this.name = tmxObj.name;
/**
* object x position
* @public
* @type Number
* @name x
* @memberOf me.TMXObject
*/
this.x = +tmxObj.x;
/**
* object y position
* @public
* @type Number
* @name y
* @memberOf me.TMXObject
*/
this.y = +tmxObj.y;
/**
* object z order
* @public
* @type Number
* @name z
* @memberOf me.TMXObject
*/
this.z = +z;
/**
* object width
* @public
* @type Number
* @name width
* @memberOf me.TMXObject
*/
this.width = +tmxObj.width || 0;
/**
* object height
* @public
* @type Number
* @name height
* @memberOf me.TMXObject
*/
this.height = +tmxObj.height || 0;
/**
* object gid value
* when defined the object is a tiled object
* @public
* @type Number
* @name gid
* @memberOf me.TMXObject
*/
this.gid = +tmxObj.gid || null;
/**
* object type
* @public
* @type String
* @name type
* @memberOf me.TMXObject
*/
this.type = tmxObj.type;
/**
* The rotation of the object in radians clockwise (defaults to 0)
* @public
* @type Number
* @name rotation
* @memberOf me.TMXObject
*/
this.rotation = Number.prototype.degToRad(+tmxObj.rotation || 0);
/**
* object unique identifier per level (Tiled 0.11.x+)
* @public
* @type Number
* @name id
* @memberOf me.TMXObject
*/
this.id = +tmxObj.id || undefined;
/**
* object orientation (orthogonal or isometric)
* @public
* @type String
* @name orientation
* @memberOf me.TMXObject
*/
this.orientation = orientation;
/**
* the collision shapes defined for this object
* @public
* @type Array
* @name shapes
* @memberOf me.TMXObject
*/
this.shapes = undefined;
/**
* if true, the object is an Ellipse
* @public
* @type Boolean
* @name isEllipse
* @memberOf me.TMXObject
*/
this.isEllipse = false;
/**
* if true, the object is a Polygon
* @public
* @type Boolean
* @name isPolygon
* @memberOf me.TMXObject
*/
this.isPolygon = false;
/**
* if true, the object is a PolyLine
* @public
* @type Boolean
* @name isPolyLine
* @memberOf me.TMXObject
*/
this.isPolyLine = false;
// check if the object has an associated gid
if (typeof this.gid === "number") {
this.setTile(tilesets);
}
else {
if (typeof(tmxObj.ellipse) !== "undefined") {
this.isEllipse = true;
}
else {
var points = tmxObj.polygon;
if (typeof(points) !== "undefined") {
this.isPolygon = true;
}
else {
points = tmxObj.polyline;
if (typeof(points) !== "undefined") {
this.isPolyLine = true;
}
}
if (typeof(points) !== "undefined") {
this.points = [];
var self = this;
points.forEach(function (point) {
self.points.push(new me.Vector2d(point.x, point.y));
});
}
}
}
// Adjust the Position to match Tiled
me.game.tmxRenderer.adjustPosition(this);
// set the object properties
me.TMXUtils.applyTMXProperties(this, tmxObj);
// define the object shapes if required
if (!this.shapes) {
this.shapes = this.parseTMXShapes();
}
},
/**
* set the object image (for Tiled Object)
* @ignore
* @function
*/
setTile : function (tilesets) {
// get the corresponding tileset
var tileset = tilesets.getTilesetByGid(this.gid);
// set width and height equal to tile size
this.width = this.framewidth = tileset.tilewidth;
this.height = this.frameheight = tileset.tileheight;
// the object corresponding tile object
this.tile = new me.Tile(this.x, this.y, this.gid, tileset);
},
/**
* parses the TMX shape definition and returns a corresponding array of me.Shape object
* @name parseTMXShapes
* @memberOf me.TMXObject
* @private
* @function
* @return {me.Polygon[]|me.Line[]|me.Ellipse[]} an array of shape objects
*/
parseTMXShapes : function () {
var i = 0;
var shapes = [];
// add an ellipse shape
if (this.isEllipse === true) {
// ellipse coordinates are the center position, so set default to the corresonding radius
shapes.push((new me.Ellipse(
this.width / 2,
this.height / 2,
this.width,
this.height
)).rotate(this.rotation));
}
// add a polygon
else if (this.isPolygon === true) {
shapes.push((new me.Polygon(0, 0, this.points)).rotate(this.rotation));
}
// add a polyline
else if (this.isPolyLine === true) {
var p = this.points;
var p1, p2;
var segments = p.length - 1;
for (i = 0; i < segments; i++) {
// clone the value before, as [i + 1]
// is reused later by the next segment
p1 = p[i];
p2 = p[i + 1].clone();
if (this.rotation !== 0) {
p1 = p1.rotate(this.rotation);
p2 = p2.rotate(this.rotation);
}
shapes.push(new me.Line(0, 0, [ p1, p2 ]));
}
}
// it's a rectangle, returns a polygon object anyway
else {
shapes.push((new me.Polygon(
0, 0, [
new me.Vector2d(), new me.Vector2d(this.width, 0),
new me.Vector2d(this.width, this.height), new me.Vector2d(0, this.height)
]
)).rotate(this.rotation));
}
// Apply isometric projection
if (this.orientation === "isometric") {
for (i = 0; i < shapes.length; i++) {
shapes[i].rotate(Math.PI / 4).scale(Math.SQRT2, Math.SQRT1_2);
}
}
return shapes;
},
/**
* getObjectPropertyByName
* @ignore
* @function
*/
getObjectPropertyByName : function (name) {
return this[name];
}
});
})();