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();
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations