SHOW:
|
|
- or go back to the newest paste.
1 | - | package com.digthedipnetonlyclienttest.game.TiledMaps; |
1 | + | package com.bla.game.TiledMaps; |
2 | ||
3 | import com.badlogic.gdx.Gdx; | |
4 | import com.badlogic.gdx.assets.loaders.FileHandleResolver; | |
5 | import com.badlogic.gdx.files.FileHandle; | |
6 | import com.badlogic.gdx.graphics.Texture; | |
7 | import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
8 | import com.badlogic.gdx.maps.ImageResolver; | |
9 | import com.badlogic.gdx.maps.MapProperties; | |
10 | import com.badlogic.gdx.maps.tiled.TiledMap; | |
11 | import com.badlogic.gdx.maps.tiled.TiledMapTile; | |
12 | import com.badlogic.gdx.maps.tiled.TiledMapTileSet; | |
13 | import com.badlogic.gdx.maps.tiled.TmxMapLoader; | |
14 | import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile; | |
15 | import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile; | |
16 | import com.badlogic.gdx.utils.*; | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.util.StringTokenizer; | |
20 | ||
21 | /** | |
22 | * Created by nooooobz on 29/04/2016. | |
23 | */ | |
24 | public class TmxMapLoaderFromString extends TmxMapLoader | |
25 | { | |
26 | public TiledMap load (String xmldata) | |
27 | { | |
28 | return load(xmldata, new TmxMapLoader.Parameters()); | |
29 | } | |
30 | ||
31 | public TiledMap load (String xmldata, TmxMapLoader.Parameters parameters) { | |
32 | try { | |
33 | this.convertObjectToTileSpace = parameters.convertObjectToTileSpace; | |
34 | this.flipY = parameters.flipY; | |
35 | root = xml.parse(xmldata); | |
36 | ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>(); | |
37 | Array<FileHandle> textureFiles = loadTilesets(root); | |
38 | textureFiles.addAll(loadImages(root)); | |
39 | ||
40 | for (FileHandle textureFile : textureFiles) { | |
41 | Texture texture = new Texture(textureFile, parameters.generateMipMaps); | |
42 | texture.setFilter(parameters.textureMinFilter, parameters.textureMagFilter); | |
43 | textures.put(textureFile.path(), texture); | |
44 | } | |
45 | ||
46 | ImageResolver.DirectImageResolver imageResolver = new ImageResolver.DirectImageResolver(textures); | |
47 | TiledMap map = loadTilemap(root, null, imageResolver); | |
48 | map.setOwnedResources(textures.values().toArray()); | |
49 | return map; | |
50 | } catch (IOException e) { | |
51 | throw new GdxRuntimeException("Couldn't load tilemap", e); | |
52 | } | |
53 | } | |
54 | ||
55 | protected Array<FileHandle> loadImages (XmlReader.Element root) throws IOException { | |
56 | Array<FileHandle> images = new Array<FileHandle>(); | |
57 | ||
58 | for (XmlReader.Element imageLayer : root.getChildrenByName("imagelayer")) { | |
59 | XmlReader.Element image = imageLayer.getChildByName("image"); | |
60 | String source = image.getAttribute("source", null); | |
61 | ||
62 | if (source != null) { | |
63 | Gdx.app.log("loadImages","source!=null !!"); | |
64 | FileHandle handle = getRelativeFileHandle(null, source); | |
65 | ||
66 | if (!images.contains(handle, false)) { | |
67 | images.add(handle); | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | return images; | |
73 | } | |
74 | ||
75 | ||
76 | protected Array<FileHandle> loadTilesets (XmlReader.Element root) throws IOException { | |
77 | Array<FileHandle> images = new Array<FileHandle>(); | |
78 | for (XmlReader.Element tileset : root.getChildrenByName("tileset")) { | |
79 | String source = tileset.getAttribute("source", null); | |
80 | if (source != null) { | |
81 | ||
82 | Gdx.app.log("loadTilesets","source!=null !!"); | |
83 | ||
84 | FileHandle tsxFile = getRelativeFileHandle(null, source); | |
85 | tileset = xml.parse(tsxFile); | |
86 | XmlReader.Element imageElement = tileset.getChildByName("image"); | |
87 | if (imageElement != null) { | |
88 | String imageSource = tileset.getChildByName("image").getAttribute("source"); | |
89 | FileHandle image = getRelativeFileHandle(tsxFile, imageSource); | |
90 | images.add(image); | |
91 | } else { | |
92 | for (XmlReader.Element tile : tileset.getChildrenByName("tile")) { | |
93 | String imageSource = tile.getChildByName("image").getAttribute("source"); | |
94 | FileHandle image = getRelativeFileHandle(tsxFile, imageSource); | |
95 | images.add(image); | |
96 | } | |
97 | } | |
98 | } else { | |
99 | XmlReader.Element imageElement = tileset.getChildByName("image"); | |
100 | if (imageElement != null) { | |
101 | String imageSource = tileset.getChildByName("image").getAttribute("source"); | |
102 | FileHandle image = Gdx.files.internal(imageSource);;//getRelativeFileHandle(tmxFile, imageSource); | |
103 | images.add(image); | |
104 | } else { | |
105 | Gdx.app.log("loadTilesets","imageElement==null !!"); | |
106 | for (XmlReader.Element tile : tileset.getChildrenByName("tile")) { | |
107 | String imageSource = tile.getChildByName("image").getAttribute("source"); | |
108 | FileHandle image = getRelativeFileHandle(null, imageSource); | |
109 | images.add(image); | |
110 | } | |
111 | } | |
112 | } | |
113 | } | |
114 | return images; | |
115 | } | |
116 | ||
117 | protected TiledMap loadTilemap (XmlReader.Element root, FileHandle tmxFile, ImageResolver imageResolver) { | |
118 | TiledMap map = new TiledMap(); | |
119 | ||
120 | String mapOrientation = root.getAttribute("orientation", null); | |
121 | int mapWidth = root.getIntAttribute("width", 0); | |
122 | int mapHeight = root.getIntAttribute("height", 0); | |
123 | int tileWidth = root.getIntAttribute("tilewidth", 0); | |
124 | int tileHeight = root.getIntAttribute("tileheight", 0); | |
125 | int hexSideLength = root.getIntAttribute("hexsidelength", 0); | |
126 | String staggerAxis = root.getAttribute("staggeraxis", null); | |
127 | String staggerIndex = root.getAttribute("staggerindex", null); | |
128 | String mapBackgroundColor = root.getAttribute("backgroundcolor", null); | |
129 | ||
130 | MapProperties mapProperties = map.getProperties(); | |
131 | if (mapOrientation != null) { | |
132 | mapProperties.put("orientation", mapOrientation); | |
133 | } | |
134 | mapProperties.put("width", mapWidth); | |
135 | mapProperties.put("height", mapHeight); | |
136 | mapProperties.put("tilewidth", tileWidth); | |
137 | mapProperties.put("tileheight", tileHeight); | |
138 | mapProperties.put("hexsidelength", hexSideLength); | |
139 | if (staggerAxis != null) { | |
140 | mapProperties.put("staggeraxis", staggerAxis); | |
141 | } | |
142 | if (staggerIndex != null) { | |
143 | mapProperties.put("staggerindex", staggerIndex); | |
144 | } | |
145 | if (mapBackgroundColor != null) { | |
146 | mapProperties.put("backgroundcolor", mapBackgroundColor); | |
147 | } | |
148 | mapTileWidth = tileWidth; | |
149 | mapTileHeight = tileHeight; | |
150 | mapWidthInPixels = mapWidth * tileWidth; | |
151 | mapHeightInPixels = mapHeight * tileHeight; | |
152 | ||
153 | if (mapOrientation != null) { | |
154 | if ("staggered".equals(mapOrientation)) { | |
155 | if (mapHeight > 1) { | |
156 | mapWidthInPixels += tileWidth / 2; | |
157 | mapHeightInPixels = mapHeightInPixels / 2 + tileHeight / 2; | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | XmlReader.Element properties = root.getChildByName("properties"); | |
163 | if (properties != null) { | |
164 | loadProperties(map.getProperties(), properties); | |
165 | } | |
166 | Array<XmlReader.Element> tilesets = root.getChildrenByName("tileset"); | |
167 | for (XmlReader.Element element : tilesets) { | |
168 | loadTileSet(map, element, tmxFile, imageResolver); | |
169 | root.removeChild(element); | |
170 | } | |
171 | for (int i = 0, j = root.getChildCount(); i < j; i++) { | |
172 | XmlReader.Element element = root.getChild(i); | |
173 | String name = element.getName(); | |
174 | if (name.equals("layer")) { | |
175 | loadTileLayer(map, element); | |
176 | } else if (name.equals("objectgroup")) { | |
177 | loadObjectGroup(map, element); | |
178 | } | |
179 | else if (name.equals("imagelayer")) { | |
180 | loadImageLayer(map, element, tmxFile, imageResolver); | |
181 | } | |
182 | } | |
183 | return map; | |
184 | } | |
185 | ||
186 | protected void loadTileSet (TiledMap map, XmlReader.Element element, FileHandle tmxFile, ImageResolver imageResolver) { | |
187 | if (element.getName().equals("tileset")) { | |
188 | String name = element.get("name", null); | |
189 | int firstgid = element.getIntAttribute("firstgid", 1); | |
190 | int tilewidth = element.getIntAttribute("tilewidth", 0); | |
191 | int tileheight = element.getIntAttribute("tileheight", 0); | |
192 | int spacing = element.getIntAttribute("spacing", 0); | |
193 | int margin = element.getIntAttribute("margin", 0); | |
194 | String source = element.getAttribute("source", null); | |
195 | ||
196 | int offsetX = 0; | |
197 | int offsetY = 0; | |
198 | ||
199 | String imageSource = ""; | |
200 | int imageWidth = 0, imageHeight = 0; | |
201 | ||
202 | FileHandle image = null; | |
203 | if (source != null) { | |
204 | FileHandle tsx = getRelativeFileHandle(tmxFile, source); | |
205 | try { | |
206 | element = xml.parse(tsx); | |
207 | name = element.get("name", null); | |
208 | tilewidth = element.getIntAttribute("tilewidth", 0); | |
209 | tileheight = element.getIntAttribute("tileheight", 0); | |
210 | spacing = element.getIntAttribute("spacing", 0); | |
211 | margin = element.getIntAttribute("margin", 0); | |
212 | XmlReader.Element offset = element.getChildByName("tileoffset"); | |
213 | if (offset != null) { | |
214 | offsetX = offset.getIntAttribute("x", 0); | |
215 | offsetY = offset.getIntAttribute("y", 0); | |
216 | } | |
217 | XmlReader.Element imageElement = element.getChildByName("image"); | |
218 | if (imageElement != null) { | |
219 | imageSource = imageElement.getAttribute("source"); | |
220 | imageWidth = imageElement.getIntAttribute("width", 0); | |
221 | imageHeight = imageElement.getIntAttribute("height", 0); | |
222 | image = getRelativeFileHandle(tsx, imageSource); | |
223 | } | |
224 | } catch (IOException e) { | |
225 | throw new GdxRuntimeException("Error parsing external tileset."); | |
226 | } | |
227 | } else { | |
228 | XmlReader.Element offset = element.getChildByName("tileoffset"); | |
229 | if (offset != null) { | |
230 | offsetX = offset.getIntAttribute("x", 0); | |
231 | offsetY = offset.getIntAttribute("y", 0); | |
232 | } | |
233 | XmlReader.Element imageElement = element.getChildByName("image"); | |
234 | if (imageElement != null) { | |
235 | imageSource = imageElement.getAttribute("source"); | |
236 | imageWidth = imageElement.getIntAttribute("width", 0); | |
237 | imageHeight = imageElement.getIntAttribute("height", 0); | |
238 | image = getRelativeFileHandle(tmxFile, imageSource); | |
239 | } | |
240 | } | |
241 | ||
242 | TiledMapTileSet tileset = new TiledMapTileSet(); | |
243 | tileset.setName(name); | |
244 | tileset.getProperties().put("firstgid", firstgid); | |
245 | if (image != null) { | |
246 | TextureRegion texture = imageResolver.getImage(image.path()); | |
247 | ||
248 | MapProperties props = tileset.getProperties(); | |
249 | props.put("imagesource", imageSource); | |
250 | props.put("imagewidth", imageWidth); | |
251 | props.put("imageheight", imageHeight); | |
252 | props.put("tilewidth", tilewidth); | |
253 | props.put("tileheight", tileheight); | |
254 | props.put("margin", margin); | |
255 | props.put("spacing", spacing); | |
256 | ||
257 | int stopWidth = texture.getRegionWidth() - tilewidth; | |
258 | int stopHeight = texture.getRegionHeight() - tileheight; | |
259 | ||
260 | int id = firstgid; | |
261 | ||
262 | for (int y = margin; y <= stopHeight; y += tileheight + spacing) { | |
263 | for (int x = margin; x <= stopWidth; x += tilewidth + spacing) { | |
264 | TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight); | |
265 | TiledMapTile tile = new StaticTiledMapTile(tileRegion); | |
266 | tile.setId(id); | |
267 | tile.setOffsetX(offsetX); | |
268 | tile.setOffsetY(flipY ? -offsetY : offsetY); | |
269 | tileset.putTile(id++, tile); | |
270 | } | |
271 | } | |
272 | } else { | |
273 | Array<XmlReader.Element> tileElements = element.getChildrenByName("tile"); | |
274 | for (XmlReader.Element tileElement : tileElements) { | |
275 | XmlReader.Element imageElement = tileElement.getChildByName("image"); | |
276 | if (imageElement != null) { | |
277 | imageSource = imageElement.getAttribute("source"); | |
278 | imageWidth = imageElement.getIntAttribute("width", 0); | |
279 | imageHeight = imageElement.getIntAttribute("height", 0); | |
280 | image = getRelativeFileHandle(tmxFile, imageSource); | |
281 | } | |
282 | TextureRegion texture = imageResolver.getImage(image.path()); | |
283 | TiledMapTile tile = new StaticTiledMapTile(texture); | |
284 | tile.setId(firstgid + tileElement.getIntAttribute("id")); | |
285 | tile.setOffsetX(offsetX); | |
286 | tile.setOffsetY(flipY ? -offsetY : offsetY); | |
287 | tileset.putTile(tile.getId(), tile); | |
288 | } | |
289 | } | |
290 | Array<XmlReader.Element> tileElements = element.getChildrenByName("tile"); | |
291 | ||
292 | Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>(); | |
293 | ||
294 | for (XmlReader.Element tileElement : tileElements) { | |
295 | int localtid = tileElement.getIntAttribute("id", 0); | |
296 | TiledMapTile tile = tileset.getTile(firstgid + localtid); | |
297 | if (tile != null) { | |
298 | XmlReader.Element animationElement = tileElement.getChildByName("animation"); | |
299 | if (animationElement != null) { | |
300 | ||
301 | Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>(); | |
302 | IntArray intervals = new IntArray(); | |
303 | for (XmlReader.Element frameElement: animationElement.getChildrenByName("frame")) { | |
304 | staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid"))); | |
305 | intervals.add(frameElement.getIntAttribute("duration")); | |
306 | } | |
307 | ||
308 | AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles); | |
309 | animatedTile.setId(tile.getId()); | |
310 | animatedTiles.add(animatedTile); | |
311 | tile = animatedTile; | |
312 | } | |
313 | ||
314 | String terrain = tileElement.getAttribute("terrain", null); | |
315 | if (terrain != null) { | |
316 | tile.getProperties().put("terrain", terrain); | |
317 | } | |
318 | String probability = tileElement.getAttribute("probability", null); | |
319 | if (probability != null) { | |
320 | tile.getProperties().put("probability", probability); | |
321 | } | |
322 | XmlReader.Element properties = tileElement.getChildByName("properties"); | |
323 | if (properties != null) { | |
324 | loadProperties(tile.getProperties(), properties); | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | for (AnimatedTiledMapTile tile : animatedTiles) { | |
330 | tileset.putTile(tile.getId(), tile); | |
331 | } | |
332 | ||
333 | XmlReader.Element properties = element.getChildByName("properties"); | |
334 | if (properties != null) { | |
335 | loadProperties(tileset.getProperties(), properties); | |
336 | } | |
337 | map.getTileSets().addTileSet(tileset); | |
338 | } | |
339 | } | |
340 | ||
341 | protected static FileHandle getRelativeFileHandle (FileHandle file, String path) | |
342 | { | |
343 | FileHandle result = Gdx.files.internal(path); | |
344 | return result; | |
345 | } | |
346 | } |