Example usage for com.badlogic.gdx.maps.objects PolylineMapObject getProperties

List of usage examples for com.badlogic.gdx.maps.objects PolylineMapObject getProperties

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.objects PolylineMapObject getProperties.

Prototype

public MapProperties getProperties() 

Source Link

Usage

From source file:headmade.arttag.utils.MapUtils.java

License:Apache License

public static void loadMap(ArtTagScreen artTagScreen, String mapName) {
    if (artTagScreen.currentRoom != null) {
        unloadMap(artTagScreen);/*  ww w  .  j ava2 s .  c  o m*/
    }

    if (null != Player.instance.warpDirection) {
        Gdx.app.log(TAG, "Loading map " + mapName);
        if (DIRECTION_LEFT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == 0) {
                artTagScreen.currentRoomIndexX = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexX--;
            }
        } else if (DIRECTION_RIGHT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexX = 0;
            } else {
                artTagScreen.currentRoomIndexX++;
            }
        } else if (DIRECTION_TOP.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexY = 0;
            } else {
                artTagScreen.currentRoomIndexY++;
            }
        } else if (DIRECTION_BOTTOM.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == 0) {
                artTagScreen.currentRoomIndexY = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexY--;
            }
        }

    }

    boolean isNewRoom = true;
    if (artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] == null) {
        artTagScreen.currentRoom = new Room(mapName);
        artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] = artTagScreen.currentRoom;
    } else {
        artTagScreen.currentRoom = artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY];
        isNewRoom = false;
    }

    final Box2DMapObjectParser parser = new Box2DMapObjectParser(ArtTag.UNIT_SCALE);
    // final Box2DMapObjectParser.Listener.Adapter listener = new Box2DMapObjectParser.Listener.Adapter() {
    //
    // @Override
    // public void created(Fixture fixture, MapObject mapObject) {
    //
    // Gdx.app.log(TAG, "mapObject.getProperties()" + fixture.getFilterData().maskBits);
    // super.created(fixture, mapObject);
    // }
    //
    // };
    // parser.setListener(listener);

    final TiledMap map = artTagScreen.currentRoom.getMap();

    final String onGameOver = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    final String hideJobDesc = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    artTagScreen.onGameOver = onGameOver;
    artTagScreen.isHideJobDesc = hideJobDesc == null ? false : true;

    parser.load(artTagScreen.world, map);
    if (null == artTagScreen.mapRenderer) {
        // artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(artTagScreen.map, artTagScreen.getGame().getBatch());
        artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(map, parser.getUnitScale(),
                artTagScreen.getGame().getBatch());
    } else {
        artTagScreen.mapRenderer.setMap(map);
    }

    MapLayer layer = map.getLayers().get("objects");
    for (final MapObject mapObject : layer.getObjects()) {
        if (OBJ_ART.equals(mapObject.getName())) {
            if (isNewRoom) {
                // add Art only if this a new room. If this is an old room the art was created before.
                if (mapObject instanceof RectangleMapObject) {
                    createNewArt(artTagScreen, mapObject, ((RectangleMapObject) mapObject).getRectangle(),
                            parser.getUnitScale());
                } else {
                    Gdx.app.error(TAG, OBJ_ART + " has to be a Rectangle");
                }
            }
        } else if (OBJ_WARP.equals(mapObject.getName())) {
            final Body warp = createWarp(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String direction = mapObject.getProperties().get(PROP_DIRECTION, String.class);
            final String room = mapObject.getProperties().get(PROP_ROOM, String.class);
            warp.setUserData(new WarpVo(direction, room));
        } else if (OBJ_EXIT.equals(mapObject.getName())) {
            createExit(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(), parser.getUnitScale());
        } else if (OBJ_HINT.equals(mapObject.getName())) {
            final Body hintBody = createHint(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String hint = mapObject.getProperties().get(PROP_TEXT, String.class);
            hintBody.setUserData(hint);
        } else if (OBJ_PLAYER.equals(mapObject.getName())) {
            final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
            if (null == Player.instance.body) {
                final String direction = mapObject.getProperties().get("direction", String.class);
                if (null != Player.instance.warpDirection) {
                    if (Player.instance.warpDirection.equalsIgnoreCase(direction)) {
                        Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                                (e.y + e.height / 2f) * parser.getUnitScale());
                        Player.instance.warpDirection = null;
                    }
                } else if (direction == null) {
                    Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                            (e.y + e.height / 2f) * parser.getUnitScale());
                }
            }
        }
    }

    if (!isNewRoom) {
        // create ArtSensors for old Art
        for (final Art art : artTagScreen.currentRoom.getArtList()) {
            createArtSensor(artTagScreen, art);
        }
    }

    { // guards
        final HashMap<String, Guard> guards = new HashMap<String, Guard>();
        final Array<MapObject> paths = new Array<MapObject>();
        layer = map.getLayers().get("guards");
        for (final MapObject mapObject : layer.getObjects()) {
            if (mapObject.getName() != null && mapObject.getName().contains(OBJ_GUARD)) {
                final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
                final Guard g = new Guard();
                g.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                        (e.y + e.height / 2f) * parser.getUnitScale());
                guards.put(mapObject.getName().trim(), g);
            } else if (mapObject.getName() != null && mapObject.getName().contains(OBJ_PATH)) {
                paths.add(mapObject);
            } else {
                Gdx.app.log(TAG, "WTF");
            }
        }
        for (final MapObject mapObject : paths) {
            final PolylineMapObject pl = (PolylineMapObject) mapObject;
            final String ownerName = pl.getProperties().get("owner", String.class);
            final Guard g = guards.get(ownerName.trim());
            Gdx.app.log(TAG, "guards " + guards.keySet());
            if (g != null) {
                final Polyline orgPolyline = pl.getPolyline();
                final Polyline p = new Polyline(orgPolyline.getVertices());
                p.setScale(parser.getUnitScale(), parser.getUnitScale());
                p.setPosition(orgPolyline.getX() * parser.getUnitScale(),
                        orgPolyline.getY() * parser.getUnitScale());
                final float[] vertices = p.getTransformedVertices();
                for (int i = 0; i < vertices.length; i += 2) {
                    g.path.add(new Vector2(vertices[i], vertices[i + 1]));
                }
                artTagScreen.guards.add(g);
            } else {
                Gdx.app.log(TAG, "No guard for path " + ownerName);
            }
        }

    }

    layer = map.getLayers().get("lights");
    for (final MapObject mapObject : layer.getObjects()) {
        if (mapObject.getProperties().get("type", String.class).contains(LIGTH_POINT)) {
            if (mapObject instanceof EllipseMapObject) {
                createPointLight(artTagScreen, (EllipseMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_POINT + " light has to be a Circle not " + mapObject);
            }
        } else if (mapObject.getProperties().get("type", String.class).contains(LIGTH_CONE)) {
            if (mapObject instanceof PolygonMapObject) {
                createConeLight(artTagScreen, (PolygonMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_CONE + " light has to be a Polygon");
            }
        }
    }

}

From source file:releasethekraken.LevelLoader.java

/**
 * Parses the level file and constructs a game world from it, and returns it.
 * Throws an exception if loading failed.
 * @return The newly loaded GameWorld//w  ww  .  ja va 2  s  .c om
 */
public GameWorld loadWorld() {
    Gdx.app.log("LevelLoader", "Loading level \"" + this.levelName + "\"");

    MapBodyManager mapBodyManager;
    GameWorld newWorld = new GameWorld(this.levelName);

    //Make a new TiledMap
    TiledMap map = new TmxMapLoader().load(this.levelName + ".tmx");

    float unitScale = 16.0F; //16 pixels = 1 meter

    mapBodyManager = new MapBodyManager(newWorld.getPhysWorld(), unitScale, null, 2);
    mapBodyManager.createPhysics(map, "physics");

    //Get the map properties
    MapProperties properties = map.getProperties();

    //Load world properties
    newWorld.setName(properties.get("levelname", String.class));
    newWorld.setPointsForKraken(Integer.parseInt(properties.get("krakenpoints", String.class)));
    newWorld.setWidth(properties.get("width", Integer.class) * 2);
    newWorld.setHeight(properties.get("height", Integer.class) * 2);
    newWorld.setTiledMap(map);
    newWorld.setTiledMapUnitScale(unitScale);
    newWorld.spawnWorldBoundaries();

    //Load Entity Tile Data
    Gdx.app.log("LevelLoader", "Parsing entity tile data");

    TiledMapTileSet entityTileSet = map.getTileSets().getTileSet("Entities");
    Iterator<TiledMapTile> entityTileIterator = entityTileSet.iterator();

    while (entityTileIterator.hasNext()) //Iterate through entity tiles
    {
        TiledMapTile tile = entityTileIterator.next();
        String type = tile.getProperties().get("type", String.class);

        if (type == null) //Only check tiles that have a type set
            continue;

        //See if there is an entity class for a given name
        Class<? extends Entity> entityClass = ReleaseTheKraken.getEntityFromName(type);

        //If there is, register the ID
        if (entityClass != null)
            this.entityTileIDs.put(tile.getId(), entityClass);

        Gdx.app.log("LevelLoader", "Tile ID: " + tile.getId() + " type: " + type);
    }

    //If you thought that was complicated, wait until you see what's next: paths!

    //Load world paths
    Gdx.app.log("LevelLoader", "Parsing world paths");
    MapLayer pathLayer = map.getLayers().get("paths");

    //If the layer doesn't exist, throw an exception
    if (pathLayer == null)
        throw new NullPointerException("paths layer is null");

    MapObjects pathObjects = pathLayer.getObjects();
    Iterator<MapObject> pathObjIterator = pathObjects.iterator();

    //Create an IntMap to map integer path IDs to paths
    IntMap<SeaCreaturePath> pathMap = new IntMap<SeaCreaturePath>();
    //Create an IntMap to map integer path IDs to arrays of next path IDs
    IntMap<int[]> nextPathsMap = new IntMap<int[]>();

    while (pathObjIterator.hasNext()) //Iterate over paths on the map
    {
        MapObject pathObject = pathObjIterator.next();

        //Gdx.app.log("LevelLoader", "Parsing map object: " + pathObject);

        if (pathObject instanceof PolylineMapObject) //If the mapObject is of the correct type
        {
            PolylineMapObject polylineMapObject = (PolylineMapObject) pathObject;

            int pathID = polylineMapObject.getProperties().get("id", Integer.class);

            Gdx.app.log("LevelLoader", "Attempting to load path with ID: " + pathID);

            //Get and parse the list of next paths
            String nextPaths = polylineMapObject.getProperties().get("next", String.class);
            String[] nextPathIDs = nextPaths.split(",");

            //Make an array of next path integer IDs
            int[] nextPathIntIDs = new int[nextPathIDs.length];

            //Parse the integer IDs from the string IDs
            for (int i = 0; i < nextPathIDs.length; i++)
                if (!nextPathIDs[i].equals(""))
                    nextPathIntIDs[i] = Integer.parseInt(nextPathIDs[i]);

            //Create a new SeaCreaturePath object
            SeaCreaturePath seaCreaturePath = new SeaCreaturePath(pathID, null, null,
                    polylineMapObject.getPolyline());

            //Add the SeaCreaturePath object and its next paths to the IntMaps
            pathMap.put(pathID, seaCreaturePath); //Add the path to the IntMap so that it can be connected later
            nextPathsMap.put(pathID, nextPathIntIDs); //Add the next paths to the IntMap so that the paths can be connected later
        }
    }

    //Gdx.app.log("LevelLoader", "Done creating paths");
    //Gdx.app.log("LevelLoader", "pathMap: " + pathMap);
    //Gdx.app.log("LevelLoader", "nextPathsMap: " + nextPathsMap);

    //Now that all of the SeaCreaturePaths are created, it's time to connect them

    SeaCreaturePath firstPath = null; //The first path, which will be the one added to the world
    Iterator<IntMap.Entry<SeaCreaturePath>> pathIterator = pathMap.iterator();

    //Iterate over each path, connecting it to the other paths it should be connected to
    while (pathIterator.hasNext()) {
        IntMap.Entry<SeaCreaturePath> currentPathEntry = pathIterator.next();

        int pathID = currentPathEntry.key;
        SeaCreaturePath currentPath = currentPathEntry.value;

        //Create the next paths list
        Array<SeaCreaturePath> nextPaths = new Array<SeaCreaturePath>();

        //Get the array of next path IDs
        int[] nextPathIDs = nextPathsMap.get(pathID);

        //Connect the paths
        for (int i = 0; i < nextPathIDs.length; i++) {
            nextPaths.add(pathMap.get(nextPathIDs[i])); //Add the path to the list of next paths

            if (pathMap.get(nextPathIDs[i]) != null)
                pathMap.get(nextPathIDs[i]).setParent(currentPath); //Tell the next path that this current path is the parent
            //BUG: Each next path overwrites this, only the last "next path" gets to be the parent
        }

        currentPath.setNextPaths(nextPaths); //Set the next paths list for the path

        //Set the first path to the path that has a null parent
        if (currentPath.getParent() == null)
            firstPath = currentPath;
        //BUG: Only the last path that has a null parent becomes the first path.  
        //This might not be a problem, as only one path should ever have a null parent.
    }

    newWorld.setFirstPath(firstPath); //Set the first path in the world
    //Done loading paths

    //Load world entities
    Gdx.app.log("LevelLoader", "Parsing world entities");
    MapLayer entityLayer = map.getLayers().get("entities");

    //If the layer doesn't exist, throw an exception
    if (entityLayer == null)
        throw new NullPointerException("entities layer is null");

    MapObjects entityObjects = entityLayer.getObjects();
    Iterator<MapObject> entityObjIterator = entityObjects.iterator();

    while (entityObjIterator.hasNext()) //Iterate over entities on the map
    {
        MapObject entityObject = entityObjIterator.next();

        //Gdx.app.log("LevelLoader", "Parsing map object: " + entityObject);

        if (entityObject instanceof TextureMapObject) //If the mapObject is of the correct type
        {
            //Get the entity class from the mapObject's tile ID
            int gid = ((TextureMapObject) entityObject).getProperties().get("gid", Integer.class);
            Class<? extends Entity> entityClass = this.entityTileIDs.get(gid);

            if (entityClass != null) {
                Gdx.app.log("LevelLoader", "Attempting to spawn " + entityClass.getSimpleName());

                try {
                    //Construct a new entity by finding the correct constructor from its class, and reflectively instantiating it.  Magic!
                    Constructor<? extends Entity> constructor = entityClass.getConstructor(GameWorld.class,
                            TextureMapObject.class);
                    Entity entity = constructor.newInstance(newWorld, entityObject);
                    //newWorld.addEntity(entity); //Add the entity to the world //With Box2D, the entity should add itself to the world

                    if (entity instanceof EntityPlayer) //Set the player if the entity is the player
                        newWorld.setPlayer((EntityPlayer) entity);
                    else if (entity instanceof EntityPirateBase) //Set the pirate base if the entity is the pirate base
                        newWorld.setPirateBase((EntityPirateBase) entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    Gdx.app.log("LevelLoader", "Successfully loaded the world!");
    Gdx.app.log("LevelLoader", newWorld.toString());

    return newWorld;
}

From source file:tools.TiledMapHelper.java

License:Apache License

private void loadLines(World world) {
    System.out.println("LoadingLines");
    Array<PolylineMapObject> objects = getMap().getLayers().get("collision1").getObjects()
            .getByType(PolylineMapObject.class);
    //      System.out.println("printing array");
    //      System.out.println(objects.size);
    if (objects != null && objects.size != 0) {
        for (int j = 0; j < objects.size; j++) {
            PolylineMapObject poly = objects.get(j);
            float x = (Integer) (poly.getProperties().get("x"));
            float y = (Integer) (poly.getProperties().get("y"));
            x /= 16;/*from  ww w  .  j a v  a2  s.  c  om*/
            y /= 16;
            //            System.out.println("x = " + x );
            //            System.out.println("y = " + y );

            Polyline p = poly.getPolyline();
            float vertices[] = p.getVertices();
            //         float vertices[] = new float[]{0,0, 20,0};

            //            System.out.println("vertices:");
            for (int i = 0; i < vertices.length; i++) {
                //               System.out.println(vertices[i]);
                vertices[i] = (Float) ((vertices[i] / scale) + x);
                //               System.out.println(vertices[i]);
                i++;
                //               System.out.println(vertices[i]);
                vertices[i] = (Float) ((vertices[i] / scale) + y);
                //               System.out.println(vertices[i]);
            }
            createPolygon(world, vertices);
        }
    }

}