Search in sources :

Example 1 with MapObject

use of com.badlogic.gdx.maps.MapObject in project libgdx by libgdx.

the class TiledMapObjectLoadingTest method render.

@Override
public void render() {
    Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    shapeRenderer.setProjectionMatrix(camera.combined);
    batch.setProjectionMatrix(camera.combined);
    shapeRenderer.setColor(Color.BLUE);
    Gdx.gl20.glLineWidth(2);
    MapLayer layer = map.getLayers().get("Objects");
    AnimatedTiledMapTile.updateAnimationBaseTime();
    for (MapObject mapObject : layer.getObjects()) {
        if (mapObject instanceof TiledMapTileMapObject) {
            batch.begin();
            TiledMapTileMapObject tmtObject = (TiledMapTileMapObject) mapObject;
            TextureRegion textureRegion = tmtObject.getTile().getTextureRegion();
            // TilEd rotation is clockwise, we need counter-clockwise.
            float rotation = -tmtObject.getRotation();
            float scaleX = tmtObject.getScaleX();
            float scaleY = tmtObject.getScaleY();
            float xPos = tmtObject.getX();
            float yPos = tmtObject.getY();
            textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
            batch.draw(textureRegion, xPos, yPos, tmtObject.getOriginX() * scaleX, tmtObject.getOriginY() * scaleY, textureRegion.getRegionWidth() * scaleX, textureRegion.getRegionHeight() * scaleY, 1f, 1f, rotation);
            // We flip back to the original state.
            textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
            batch.end();
        } else if (mapObject instanceof EllipseMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            shapeRenderer.ellipse(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
            shapeRenderer.end();
        } else if (mapObject instanceof RectangleMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
            shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
            shapeRenderer.end();
        } else if (mapObject instanceof PolygonMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
            Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
            shapeRenderer.polygon(polygon.getTransformedVertices());
            shapeRenderer.end();
        }
    }
    batch.begin();
    font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
    batch.end();
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) Ellipse(com.badlogic.gdx.math.Ellipse) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) MapLayer(com.badlogic.gdx.maps.MapLayer) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) Rectangle(com.badlogic.gdx.math.Rectangle) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) TextureMapObject(com.badlogic.gdx.maps.objects.TextureMapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject)

Example 2 with MapObject

use of com.badlogic.gdx.maps.MapObject in project ultimate-java by pantinor.

the class CombatScreen method setAmbushingMonsters.

public void setAmbushingMonsters(BaseScreen returnScreen, int x, int y, TextureAtlas a1) {
    CreatureType ct = Ultima4.creatures.getRandomAmbushing();
    fillCreatureTable(ct);
    MapLayer mLayer = tmap.getLayers().get("Monster Positions");
    Iterator<MapObject> iter = mLayer.getObjects().iterator();
    while (iter.hasNext()) {
        MapObject obj = iter.next();
        int index = (Integer) obj.getProperties().get("index");
        int startX = (Integer) obj.getProperties().get("startX");
        int startY = (Integer) obj.getProperties().get("startY");
        if (crSlots[index] == null) {
            continue;
        }
        Creature c = creatureSet.getInstance(crSlots[index], a1);
        c.currentX = startX;
        c.currentY = startY;
        c.currentPos = getMapPixelCoords(startX, startY);
        combatMap.addCreature(c);
    }
    // for the chest when returning after combat
    returnScreen.currentEncounter = creatureSet.getInstance(ct, a1);
    returnScreen.currentEncounter.currentX = x;
    returnScreen.currentEncounter.currentY = y;
    returnScreen.currentEncounter.currentPos = returnScreen.getMapPixelCoords(x, y);
}
Also used : Creature(objects.Creature) MapLayer(com.badlogic.gdx.maps.MapLayer) MapObject(com.badlogic.gdx.maps.MapObject)

Example 3 with MapObject

use of com.badlogic.gdx.maps.MapObject in project ultimate-java by pantinor.

the class UltimaTiledMapLoader method loadCombatPositions.

private void loadCombatPositions(TiledMap map) throws Exception {
    InputStream is = new FileInputStream("assets/data/" + gameMap.getMap().getFname());
    byte[] bytes = IOUtils.toByteArray(is);
    // 0x0 	16 	start_x for monsters 0-15
    // 0x10 	16 	start_y for monsters 0-15
    // 0x20 	8 	start_x for party members 0-7
    // 0x28 	8 	start_y for party members 0-7
    MapLayer mlayer = new MapLayer();
    mlayer.setName("Monster Positions");
    Position[] monPos = new Position[16];
    for (int i = 0; i < 16; i++) {
        monPos[i] = new Position(i, (int) bytes[i], 0);
    }
    for (int i = 0; i < 16; i++) {
        monPos[i].startY = (int) bytes[i + 16];
    }
    for (int i = 0; i < 16; i++) {
        MapObject object = new MapObject();
        object.getProperties().put("index", monPos[i].index);
        object.getProperties().put("startX", monPos[i].startX);
        object.getProperties().put("startY", monPos[i].startY);
        mlayer.getObjects().add(object);
    }
    map.getLayers().add(mlayer);
    MapLayer player = new MapLayer();
    player.setName("Player Positions");
    Position[] playerPos = new Position[8];
    for (int i = 0; i < 8; i++) {
        playerPos[i] = new Position(i, (int) bytes[i + 32], 0);
    }
    for (int i = 0; i < 8; i++) {
        playerPos[i].startY = (int) bytes[i + 40];
    }
    for (int i = 0; i < 8; i++) {
        MapObject object = new MapObject();
        object.getProperties().put("index", playerPos[i].index);
        object.getProperties().put("startX", playerPos[i].startX);
        object.getProperties().put("startY", playerPos[i].startY);
        player.getObjects().add(object);
    }
    map.getLayers().add(player);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MapLayer(com.badlogic.gdx.maps.MapLayer) MapObject(com.badlogic.gdx.maps.MapObject) FileInputStream(java.io.FileInputStream)

Example 4 with MapObject

use of com.badlogic.gdx.maps.MapObject in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method getHierarchy.

/**
 * @param layer the {@link MapLayer} which hierarchy to print
 * @return a human readable {@link String} containing the hierarchy of the {@link com.badlogic.gdx.maps.MapObjects} of the given {@link MapLayer}
 */
public String getHierarchy(MapLayer layer) {
    String hierarchy = "", key;
    for (MapObject object : layer.getObjects()) {
        hierarchy += object.getName() + " (" + object.getClass().getName() + "):\n";
        Iterator<String> keys = object.getProperties().getKeys();
        while (keys.hasNext()) hierarchy += "\t" + (key = keys.next()) + ": " + object.getProperties().get(key) + "\n";
    }
    return hierarchy;
}
Also used : MapObject(com.badlogic.gdx.maps.MapObject)

Example 5 with MapObject

use of com.badlogic.gdx.maps.MapObject in project libgdx by libgdx.

the class BaseTmxMapLoader method loadObject.

protected void loadObject(TiledMap map, MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;
        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;
        float x = element.getFloatAttribute("x", 0) * scaleX;
        float y = (flipY ? (mapHeightInPixels - element.getFloatAttribute("y", 0)) : element.getFloatAttribute("y", 0)) * scaleY;
        float width = element.getFloatAttribute("width", 0) * scaleX;
        float height = element.getFloatAttribute("height", 0) * scaleY;
        if (element.getChildCount() > 0) {
            Element child = null;
            if ((child = element.getChildByName("polygon")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polygon polygon = new Polygon(vertices);
                polygon.setPosition(x, y);
                object = new PolygonMapObject(polygon);
            } else if ((child = element.getChildByName("polyline")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polyline polyline = new Polyline(vertices);
                polyline.setPosition(x, y);
                object = new PolylineMapObject(polyline);
            } else if ((child = element.getChildByName("ellipse")) != null) {
                object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        if (object == null) {
            String gid = null;
            if ((gid = element.getAttribute("gid", null)) != null) {
                int id = (int) Long.parseLong(gid);
                boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                TiledMapTile tile = map.getTileSets().getTile(id & ~MASK_CLEAR);
                TiledMapTileMapObject tiledMapTileMapObject = new TiledMapTileMapObject(tile, flipHorizontally, flipVertically);
                TextureRegion textureRegion = tiledMapTileMapObject.getTextureRegion();
                tiledMapTileMapObject.getProperties().put("gid", id);
                tiledMapTileMapObject.setX(x);
                tiledMapTileMapObject.setY(flipY ? y : y - height);
                float objectWidth = element.getFloatAttribute("width", textureRegion.getRegionWidth());
                float objectHeight = element.getFloatAttribute("height", textureRegion.getRegionHeight());
                tiledMapTileMapObject.setScaleX(scaleX * (objectWidth / textureRegion.getRegionWidth()));
                tiledMapTileMapObject.setScaleY(scaleY * (objectHeight / textureRegion.getRegionHeight()));
                tiledMapTileMapObject.setRotation(element.getFloatAttribute("rotation", 0));
                object = tiledMapTileMapObject;
            } else {
                object = new RectangleMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        object.setName(element.getAttribute("name", null));
        String rotation = element.getAttribute("rotation", null);
        if (rotation != null) {
            object.getProperties().put("rotation", Float.parseFloat(rotation));
        }
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int id = element.getIntAttribute("id", 0);
        if (id != 0) {
            object.getProperties().put("id", id);
        }
        object.getProperties().put("x", x);
        if (object instanceof TiledMapTileMapObject) {
            object.getProperties().put("y", y);
        } else {
            object.getProperties().put("y", (flipY ? y - height : y));
        }
        object.getProperties().put("width", width);
        object.getProperties().put("height", height);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) Polyline(com.badlogic.gdx.math.Polyline) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject)

Aggregations

MapObject (com.badlogic.gdx.maps.MapObject)12 MapLayer (com.badlogic.gdx.maps.MapLayer)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)3 MapObjects (com.badlogic.gdx.maps.MapObjects)2 EllipseMapObject (com.badlogic.gdx.maps.objects.EllipseMapObject)2 PolygonMapObject (com.badlogic.gdx.maps.objects.PolygonMapObject)2 RectangleMapObject (com.badlogic.gdx.maps.objects.RectangleMapObject)2 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)2 TiledMapTileMapObject (com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject)2 Polygon (com.badlogic.gdx.math.Polygon)2 Creature (objects.Creature)2 Tile (objects.Tile)2 FileHandle (com.badlogic.gdx.files.FileHandle)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)1 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)1 MapProperties (com.badlogic.gdx.maps.MapProperties)1 PolylineMapObject (com.badlogic.gdx.maps.objects.PolylineMapObject)1 TextureMapObject (com.badlogic.gdx.maps.objects.TextureMapObject)1