This repository has been archived by the owner on May 24, 2018. It is now read-only.
/
TiledMap.hx
297 lines (238 loc) · 8.24 KB
/
TiledMap.hx
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
// Copyright (C) 2013 Christopher "Kasoki" Kaster
//
// This file is part of "openfl-tiled". <http://github.com/Kasoki/openfl-tiled>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package openfl.tiled;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import haxe.io.Path;
import openfl.display.Tilesheet;
import openfl.tiled.display.Renderer;
#if !flash
import openfl.tiled.display.TilesheetRenderer;
#else
import openfl.tiled.display.CopyPixelsRenderer;
#end
/**
* This class represents a TILED map
* @author Christopher Kaster
*/
class TiledMap extends Sprite {
/** The path of the map file */
public var path(default, null):String;
/** The map width in tiles */
public var widthInTiles(default, null):Int;
/** The map height in tiles */
public var heightInTiles(default, null):Int;
/** The map width in pixels */
public var totalWidth(get_totalWidth, null):Int;
/** The map height in pixels */
public var totalHeight(get_totalHeight, null):Int;
/** TILED orientation: Orthogonal or Isometric */
public var orientation(default, null):TiledMapOrientation;
/** The tile width */
public var tileWidth(default, null):Int;
/** The tile height */
public var tileHeight(default, null):Int;
/** The background color of the map */
public var backgroundColor(default, null):UInt;
/** All tilesets the map is using */
public var tilesets(default, null):Array<Tileset>;
/** Contains all layers from this map */
public var layers(default, null):Array<Layer>;
/** All objectgroups */
public var objectGroups(default, null):Array<TiledObjectGroup>;
/** All image layers **/
public var imageLayers(default, null):Array<ImageLayer>;
/** All map properties */
public var properties(default, null):Map<String, String>;
public var backgroundColorSet(default, null):Bool = false;
public var renderer(default, null):Renderer;
private function new(path:String, renderer:Renderer, ?render:Bool = true) {
super();
this.path = path;
var xml = Helper.getText(path);
parseXML(xml);
this.renderer = renderer;
renderer.setTiledMap(this);
if(render) {
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
private function onAddedToStage(e:Event) {
renderer.clear(this);
for(layer in this.layers) {
renderer.drawLayer(this, layer);
}
for(imageLayer in this.imageLayers) {
renderer.drawImageLayer(this, imageLayer);
}
}
/**
* Creates a new TiledMap from Assets
* @param path The path to your asset
* @param render Should openfl-tiled render the map?
* @return A TiledMap object
*/
public static function fromAssets(path:String, ?render:Bool = true):TiledMap {
#if !flash
var renderer = new TilesheetRenderer();
#else
var renderer = new CopyPixelsRenderer();
#end
return new TiledMap(path, renderer, render);
}
/**
* Creates a new TiledMap from Assets with an alternative Renderer
* @param path The path to your asset
* @param renderer Add your own renderer implementation here
* @return A TiledMap object
*/
public static function fromAssetsWithAlternativeRenderer(path:String, renderer:Renderer,
?render:Bool = true):TiledMap {
return new TiledMap(path, renderer, render);
}
private function parseXML(xml:String) {
var xml = Xml.parse(xml).firstElement();
this.widthInTiles = Std.parseInt(xml.get("width"));
this.heightInTiles = Std.parseInt(xml.get("height"));
this.orientation = xml.get("orientation") == "orthogonal" ?
TiledMapOrientation.Orthogonal : TiledMapOrientation.Isometric;
this.tileWidth = Std.parseInt(xml.get("tilewidth"));
this.tileHeight = Std.parseInt(xml.get("tileheight"));
this.tilesets = new Array<Tileset>();
this.layers = new Array<Layer>();
this.objectGroups = new Array<TiledObjectGroup>();
this.imageLayers = new Array<ImageLayer>();
this.properties = new Map<String, String>();
// get background color
var backgroundColor:String = xml.get("backgroundcolor");
// if the element isn't set choose white
if(backgroundColor != null) {
this.backgroundColorSet = true;
// replace # with 0xff to match ARGB
backgroundColor = StringTools.replace(backgroundColor, "#", "0xff");
this.backgroundColor = Std.parseInt(backgroundColor);
} else {
this.backgroundColor = 0x00000000;
}
for (child in xml) {
if(Helper.isValidElement(child)) {
if (child.nodeName == "tileset") {
var tileset:Tileset = null;
if (child.get("source") != null) {
var prefix = Path.directory(this.path) + "/";
tileset = Tileset.fromGenericXml(this, Helper.getText(child.get("source"), prefix));
} else {
tileset = Tileset.fromGenericXml(this, child.toString());
}
tileset.setFirstGID(Std.parseInt(child.get("firstgid")));
this.tilesets.push(tileset);
} else if (child.nodeName == "properties") {
for (property in child) {
if (!Helper.isValidElement(property))
continue;
properties.set(property.get("name"), property.get("value"));
}
} else if (child.nodeName == "layer") {
var layer:Layer = Layer.fromGenericXml(child, this);
this.layers.push(layer);
} else if (child.nodeName == "objectgroup") {
var objectGroup = TiledObjectGroup.fromGenericXml(child);
this.objectGroups.push(objectGroup);
} else if (child.nodeName == "imagelayer") {
var imageLayer = ImageLayer.fromGenericXml(this, child);
this.imageLayers.push(imageLayer);
}
}
}
}
/**
* Returns the Tileset which contains the given GID.
* @return The tileset which contains the given GID, or if it doesn't exist "null"
*/
public function getTilesetByGID(gid:Int):Tileset {
var tileset:Tileset = null;
for(t in this.tilesets) {
if(gid >= t.firstGID) {
tileset = t;
}
}
return tileset;
}
/**
* Returns the total Width of the map
* @return Map width in pixels
*/
private function get_totalWidth():Int {
return this.widthInTiles * this.tileWidth;
}
/**
* Returns the total Height of the map
* @return Map height in pixels
*/
private function get_totalHeight():Int {
return this.heightInTiles * this.tileHeight;
}
/**
* Returns the layer with the given name.
* @param name The name of the layer
* @return The searched layer, null if there is no such layer.
*/
public function getLayerByName(name:String):Layer {
for(layer in this.layers) {
if(layer.name == name) {
return layer;
}
}
return null;
}
/**
* Returns the object group with the given name.
* @param name The name of the object group
* @return The searched object group, null if there is no such object group.
*/
public function getObjectGroupByName(name:String):TiledObjectGroup {
for(objectGroup in this.objectGroups) {
if(objectGroup.name == name) {
return objectGroup;
}
}
return null;
}
/**
* Returns an object in a given object group
* @param name The name of the object
* @param inObjectGroup The object group which contains this object.
* @return An TiledObject, null if there is no such object.
*/
public function getObjectByName(name:String, inObjectGroup:TiledObjectGroup):TiledObject {
for(object in inObjectGroup.objects) {
if(object.name == name) {
return object;
}
}
return null;
}
}