Skip to content

Commit ee51906

Browse files
committed
Tilemap: upgraded version of createFromObjects from v2
1 parent e5a8600 commit ee51906

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
var Class = require('../../utils/Class');
2-
var MapData = require('./mapdata/MapData');
2+
var Extend = require('../../utils/object/Extend');
33
var LayerData = require('./mapdata/LayerData');
44
var StaticTilemapLayer = require('./staticlayer/StaticTilemapLayer.js');
55
var DynamicTilemapLayer = require('./dynamiclayer/DynamicTilemapLayer.js');
66
var Tileset = require('./Tileset');
77
var Formats = require('./Formats');
88
var TilemapComponents = require('./components');
99
var Tile = require('./Tile');
10+
var Rotate = require('../../math/Rotate');
11+
var DegToRad = require('../../math/DegToRad');
1012

1113
var Tilemap = new Class({
1214

@@ -133,6 +135,89 @@ var Tilemap = new Class({
133135
return dynamicLayer;
134136
},
135137

138+
/**
139+
* Creates a Sprite for every object matching the given gid in the map data. All properties from
140+
* the map data objectgroup are copied into the `spriteConfig`, so you can use this as an easy
141+
* way to configure Sprite properties from within the map editor. For example giving an object a
142+
* property of alpha: 0.5 in the map editor will duplicate that when the Sprite is created.
143+
*
144+
* @param {string} name - The name of the object layer (from Tiled) to create Sprites from.
145+
* @param {number} id - Either the id (object), gid (tile object) or name (object or tile
146+
* object) from Tiled.
147+
* @param {object} spriteConfig - The config object to pass into the Sprite creator (i.e.
148+
* scene.make.sprite).
149+
* @param {Scene} [scene=the scene the map is within] - The Scene to create the Sprites within.
150+
* @return {array} An array of the Sprites that were created.
151+
*/
152+
createFromObjects: function (name, id, spriteConfig, scene)
153+
{
154+
if (spriteConfig === undefined) { spriteConfig = {}; }
155+
if (scene === undefined) { scene = this.scene; }
156+
157+
if (!this.objects[name])
158+
{
159+
console.warn('Cannot create from object. Invalid objectgroup name given: ' + name);
160+
return;
161+
}
162+
163+
for (var i = 0; i < this.objects[name].length; i++)
164+
{
165+
var found = false;
166+
var obj = this.objects[name][i];
167+
168+
if (obj.gid !== undefined && typeof id === 'number' && obj.gid === id ||
169+
obj.id !== undefined && typeof id === 'number' && obj.id === id ||
170+
obj.name !== undefined && typeof id === 'string' && obj.name === id)
171+
{
172+
found = true;
173+
}
174+
175+
if (found)
176+
{
177+
Extend(spriteConfig, obj.properties);
178+
179+
spriteConfig.x = obj.x;
180+
spriteConfig.y = obj.y;
181+
182+
var sprite = this.scene.make.sprite(spriteConfig);
183+
184+
sprite.name = obj.name;
185+
186+
if (obj.width) { sprite.displayWidth = obj.width; }
187+
if (obj.height) { sprite.displayHeight = obj.height; }
188+
189+
// Origin is (0, 1) in Tiled, so shift it to match (0.5, 0.5) in Phaser
190+
spriteConfig.x = obj.x + sprite.displayWidth / 2;
191+
spriteConfig.y = obj.y - sprite.displayHeight / 2;
192+
193+
// If the object is rotated, then perform an additional correction for the origin
194+
// changing from (0, 1) to (0.5, 0.5)
195+
if (obj.rotation)
196+
{
197+
var angle = DegToRad(obj.rotation);
198+
var offset = {
199+
x: sprite.displayWidth / 2,
200+
y: - sprite.displayHeight / 2
201+
};
202+
Rotate(offset, angle);
203+
204+
sprite.rotation = angle;
205+
sprite.x += offset.x;
206+
sprite.y += offset.y;
207+
}
208+
209+
if (obj.flippedHorizontal !== undefined || obj.flippedVertical !== undefined)
210+
{
211+
sprite.setFlip(obj.flippedHorizontal, obj.flippedVertical);
212+
}
213+
214+
if (!obj.visible) { sprite.visible = false; }
215+
}
216+
}
217+
218+
return sprite;
219+
},
220+
136221
// Creates & selects
137222
createStaticLayer: function (layerID, tileset, x, y)
138223
{

v3/src/gameobjects/tilemap/parsers/parsetiledjson/ParseObject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Pick = require('./Pick');
33
var ParseGID = require('./ParseGID');
44

55
var pointToArray = function (p) { return [ p.x, p.y ]; };
6-
var commonObjectProps = [ 'id', 'name', 'type', 'rotation', 'properties', 'visible', 'x', 'y' ];
6+
var commonObjectProps = [ 'id', 'name', 'type', 'rotation', 'properties', 'visible', 'x', 'y', 'width', 'height' ];
77

88
var ParseObject = function (tiledObject, offsetX, offsetY)
99
{

0 commit comments

Comments
 (0)