OSDN Git Service

ran gdx-tools HeaderFixer tool
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / maps / tiled / TideMapLoader.java
index d454fea..d60ae10 100644 (file)
@@ -1,7 +1,20 @@
-package com.badlogic.gdx.maps.tiled;
+/*******************************************************************************\r
+ * Copyright 2011 See AUTHORS file.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *   http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ ******************************************************************************/
 
-import java.io.IOException;
-import java.util.StringTokenizer;
+package com.badlogic.gdx.maps.tiled;
 
 import com.badlogic.gdx.assets.AssetDescriptor;
 import com.badlogic.gdx.assets.AssetLoaderParameters;
@@ -13,9 +26,10 @@ import com.badlogic.gdx.files.FileHandle;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.maps.ImageResolver;
-import com.badlogic.gdx.maps.MapProperties;
 import com.badlogic.gdx.maps.ImageResolver.AssetManagerImageResolver;
 import com.badlogic.gdx.maps.ImageResolver.DirectImageResolver;
+import com.badlogic.gdx.maps.MapProperties;
+import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
 import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
 import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
 import com.badlogic.gdx.utils.Array;
@@ -24,6 +38,9 @@ import com.badlogic.gdx.utils.ObjectMap;
 import com.badlogic.gdx.utils.XmlReader;
 import com.badlogic.gdx.utils.XmlReader.Element;
 
+import java.io.IOException;
+import java.util.StringTokenizer;
+
 public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoader.Parameters> {
 
        public static class Parameters extends AssetLoaderParameters<TiledMap> {
@@ -51,7 +68,7 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                        }
                        DirectImageResolver imageResolver = new DirectImageResolver(textures);
                        TiledMap map = loadMap(root, tideFile, imageResolver);
-                       map.setOwnedTextures(textures.values().toArray());
+                       map.setOwnedResources(textures.values().toArray());
                        return map;
                } catch(IOException e) {
                        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
@@ -60,8 +77,7 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
        }
        
        @Override
-       public TiledMap load (AssetManager assetManager, String fileName, Parameters parameter) {
-               FileHandle tideFile = resolve(fileName);
+       public TiledMap load (AssetManager assetManager, String fileName, FileHandle tideFile, Parameters parameter) {
                try {
                        return loadMap(root, tideFile, new AssetManagerImageResolver(assetManager));
                } catch (Exception e) {
@@ -70,10 +86,9 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
        }
 
        @Override
-       public Array<AssetDescriptor> getDependencies (String fileName, Parameters parameter) {
+       public Array<AssetDescriptor> getDependencies (String fileName, FileHandle tmxFile, Parameters parameter) {
                Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
                try {
-                       FileHandle tmxFile = resolve(fileName);
                        root = xml.parse(tmxFile);
                        for(FileHandle image: loadTileSheets(root, tmxFile)) {
                                dependencies.add(new AssetDescriptor(image.path(), Texture.class));
@@ -155,7 +170,7 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                        int spacingY = Integer.parseInt(spacingParts[1]);
 
                        FileHandle image = getRelativeFileHandle(tideFile, imageSource);
-                       Texture texture = imageResolver.getImage(image.path());
+                       TextureRegion texture = imageResolver.getImage(image.path());
                        
                        // TODO: Actually load the tilesheet
                        // Need to make global ids as Tide doesn't have global ids.
@@ -170,17 +185,18 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                        tileset.getProperties().put("firstgid", firstgid);
                        int gid = firstgid;
                        
-                       int stopWidth = texture.getWidth() - tileSizeX;
-                       int stopHeight = texture.getHeight() - tileSizeY;
+                       int stopWidth = texture.getRegionWidth() - tileSizeX;
+                       int stopHeight = texture.getRegionHeight() - tileSizeY;
                        
                        for (int y = marginY; y <= stopHeight; y += tileSizeY + spacingY) {
                                for (int x = marginX; x <= stopWidth; x += tileSizeX + spacingX) {
                                        TiledMapTile tile = new StaticTiledMapTile(new TextureRegion(texture, x, y, tileSizeX, tileSizeY));
+                                       tile.setId(gid);
                                        tileset.putTile(gid++, tile);
                                }
                        }
                        
-                       Element properties = element.getChildByName("Proeprties");
+                       Element properties = element.getChildByName("Properties");
                        if (properties != null) {
                                loadProperties(tileset.getProperties(), properties);
                        }
@@ -207,6 +223,8 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
                        
                        TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
+                       layer.setName(id);
+                       layer.setVisible(visible.equalsIgnoreCase("True"));
                        Element tileArray = element.getChildByName("TileArray");
                        Array<Element> rows = tileArray.getChildrenByName("Row");
                        TiledMapTileSets tilesets = map.getTileSets();
@@ -222,11 +240,13 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                                        String name = currentChild.getName();
                                        if (name.equals("TileSheet")) {
                                                currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
-                                               firstgid = currentTileSet.getProperties().getAsInteger("firstgid");
+                                               firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                                        } else if (name.equals("Null")) {
                                                x += currentChild.getIntAttribute("Count");
                                        } else if (name.equals("Static")) {
-                                               layer.setCell(x++, y, currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
+                                               Cell cell = new Cell();
+                                               cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
+                                               layer.setCell(x++, y, cell);
                                        } else if (name.equals("Animated")) {
                                                // Create an AnimatedTile
                                                int interval = currentChild.getInt("Interval");
@@ -237,16 +257,18 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                                                        String frameName = frame.getName();
                                                        if (frameName.equals("TileSheet")) {
                                                                currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
-                                                               firstgid = currentTileSet.getProperties().getAsInteger("firstgid");
+                                                               firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                                                        } else if (frameName.equals("Static")) {
                                                                frameTiles.add((StaticTiledMapTile) currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
                                                        }
                                                }
-                                               layer.setCell(x++, y, new AnimatedTiledMapTile(interval / 1000f, frameTiles)); //TODO: Reuse existing animated tiles
+                                               Cell cell = new Cell();
+                                               cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
+                                               layer.setCell(x++, y, cell); //TODO: Reuse existing animated tiles
                                        }
                                }
                        }
-                       map.getLayers().addLayer(layer);
+                       map.getLayers().add(layer);
                }
        }
        
@@ -258,11 +280,11 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                                String value = property.getText();
                                
                                if (type.equals("Int32")) {
-                                       
+                                       properties.put(key, Integer.parseInt(value));
                                } else if (type.equals("String")) {
-                                       
+                                       properties.put(key, value);
                                } else if (type.equals("Boolean")) {
-                                       
+                                       properties.put(key, value.equalsIgnoreCase("true"));
                                } else {
                                        properties.put(key, value);                                     
                                }
@@ -284,4 +306,4 @@ public class TideMapLoader extends SynchronousAssetLoader<TiledMap, TideMapLoade
                return result;          
        }
 
-}
+}
\ No newline at end of file