private void createCoins() { coins = new Array<Coin>(); MapLayer layer = tileMap.getLayers().get("Coins"); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); for (MapObject mo : layer.getObjects()) { bdef.type = BodyType.StaticBody; float x = mo.getProperties().get("x", Float.class) / PPM; float y = mo.getProperties().get("y", Float.class) / PPM; bdef.position.set(x, y); CircleShape cShape = new CircleShape(); cShape.setRadius(8 / PPM); fdef.shape = cShape; fdef.isSensor = true; fdef.filter.categoryBits = B2DVars.BIT_COINS; fdef.filter.maskBits = B2DVars.BIT_PLAYER; Body body = world.createBody(bdef); body.createFixture(fdef).setUserData("coin"); Coin c = new Coin(body); coins.add(c); body.setUserData(c); } }
private Hashtable<String, Vector2> getSpecialNPCStartPositions() { Hashtable<String, Vector2> specialNPCStartPositions = new Hashtable<String, Vector2>(); for (MapObject object : spawnsLayer.getObjects()) { String objectName = object.getName(); if (objectName == null || objectName.isEmpty()) { continue; } // This is meant for all the special spawn locations, a catch all, so ignore known ones if (objectName.equalsIgnoreCase(NPC_START) || objectName.equalsIgnoreCase(PLAYER_START)) { continue; } // Get center of rectangle float x = ((RectangleMapObject) object).getRectangle().getX(); float y = ((RectangleMapObject) object).getRectangle().getY(); // scale by the unit to convert from map coordinates x *= UNIT_SCALE; y *= UNIT_SCALE; specialNPCStartPositions.put(objectName, new Vector2(x, y)); } return specialNPCStartPositions; }
@Override public void render() { AnimatedTiledMapTile.updateAnimationBaseTime(); spriteBatch.begin(); for (MapLayer layer : map.getLayers()) { if (layer.isVisible()) { if (layer instanceof TiledMapTileLayer) { renderTileLayer((TiledMapTileLayer) layer); } else { for (MapObject object : layer.getObjects()) { renderObject(object); } } } } spriteBatch.end(); }
protected void loadObjectGroup(TiledMap map, Element element) { if (element.getName().equals("objectgroup")) { String name = element.getAttribute("name", null); MapLayer layer = new MapLayer(); layer.setName(name); Element properties = element.getChildByName("properties"); if (properties != null) { loadProperties(layer.getProperties(), properties); } for (Element objectElement : element.getChildrenByName("object")) { loadObject(layer, objectElement); } map.getLayers().add(layer); } }
private void loadCollisionBoxes() { MapLayer collisionLayer = map.getLayers().get("collisions"); Game.debug("Inflating Collision Rectangles / Tile Data"); for (RectangleMapObject object : collisionLayer.getObjects().getByType(RectangleMapObject.class)) { if (object.getProperties().containsKey("spawnpoint")) { spawnpoint = new Vector2(object.getRectangle().x, object.getRectangle().y); Game.debug("Set Spawn Point to: " + spawnpoint.toString()); } if (!object.getProperties().containsKey("collision")) continue; Game.debug("Added Collision Rectangle at: " + object.getRectangle().toString()); collisions.add(object.getRectangle()); } Game.debug("Added " + collisions.size() + " Collision Rectangles"); }
@Override public void render(int[] layers) { beginRender(); for (int layerIdx : layers) { MapLayer layer = map.getLayers().get(layerIdx); if (layer.isVisible()) { if (layer instanceof TiledMapTileLayer) { renderTileLayer((TiledMapTileLayer) layer); } else { for (MapObject object : layer.getObjects()) { renderObject(object); } } } } endRender(); }
private void _loadObjectsRoutine(MapLayer objectLayer) { if (getTileLayer() != null) { Iterator<MapObject> objects = objectLayer.getObjects().iterator(); while (objects.hasNext()) { MapObject object = objects.next(); Integer position_x = object.getProperties().get("x", Float.class).intValue(); Integer position_y = object.getProperties().get("y", Float.class).intValue(); listener.objectFound(object, position_x, position_y); } } }
public static void mapColl() { MapLayer l = Play.currentMap.getLayers().get("Collision"); for (MapObject m : l.getObjects()) { BodyDef bdef = new BodyDef(); bdef.type = BodyType.StaticBody; Body b = Play.world.createBody(bdef); b.setTransform( new Vector2( (float) m.getProperties().get("x") + ((float) m.getProperties().get("width") / 2), (float) m.getProperties().get("y") + ((float) m.getProperties().get("height") / 2)), 0); PolygonShape shape = new PolygonShape(); shape.setAsBox( (float) m.getProperties().get("width") / 2, (float) m.getProperties().get("height") / 2); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; Fixture f = b.createFixture(fdef); f.setUserData("Collision"); shape.dispose(); } }
@Override public void render(GamePlayManager main) { boolean drewBG = false; boolean drewPlayer = false; beginRender(); for (MapLayer layer : map.getLayers()) { if (!layer.getName().equals("AINODEMAP")) { if (!drewBG && layer.getProperties().containsKey("bg")) { drewBG = true; main.drawBackGround(spriteBatch); } if (!drewPlayer && layer.getProperties().containsKey("player")) { main.drawPlayerLayer(spriteBatch); drewPlayer = true; } if (layer.isVisible()) { renderTileLayer((TiledMapTileLayer) layer); } } } endRender(); }
private void setClosestStartPosition(final Vector2 position) { Gdx.app.debug( TAG, "setClosestStartPosition INPUT: (" + position.x + "," + position.y + ") " + currentMapType.toString()); // Get last known position on this map playerStartPositionRect.set(0, 0); closestPlayerStartPosition.set(0, 0); float shortestDistance = 0f; // Go through all player start positions and choose closest to last known position for (MapObject object : spawnsLayer.getObjects()) { String objectName = object.getName(); if (objectName == null || objectName.isEmpty()) { continue; } if (objectName.equalsIgnoreCase(PLAYER_START)) { ((RectangleMapObject) object).getRectangle().getPosition(playerStartPositionRect); float distance = position.dst2(playerStartPositionRect); Gdx.app.debug(TAG, "DISTANCE: " + distance + " for " + currentMapType.toString()); if (distance < shortestDistance || shortestDistance == 0) { closestPlayerStartPosition.set(playerStartPositionRect); shortestDistance = distance; Gdx.app.debug( TAG, "closest START is: (" + closestPlayerStartPosition.x + "," + closestPlayerStartPosition.y + ") " + currentMapType.toString()); } } } playerStart = closestPlayerStartPosition.cpy(); }
private void init(MapLayer objectLayer) { try { sprite = new Sprite(Assets.instance.player.getNorthAnimation().getKeyFrame(0, false)); sprite.scale(1.25f); sprite.setOrigin(0.0f, 0.0f); collisionLayer = (TiledMapTileLayer) Assets.instance.world.getMap().getLayers().get(0); for (MapObject obj : objectLayer.getObjects()) { if (obj instanceof EllipseMapObject) { Ellipse ellipse = ((EllipseMapObject) obj).getEllipse(); position = UtilityMethods.mapToScreenCoordinate(new Vector2(ellipse.x, ellipse.y)); } } sprite.setPosition(position.x, position.y); } catch (Exception e) { System.out.println(e.getMessage()); } }
private Array<Vector2> getNPCStartPositions() { Array<Vector2> npcStartPositions = new Array<Vector2>(); for (MapObject object : spawnsLayer.getObjects()) { String objectName = object.getName(); if (objectName == null || objectName.isEmpty()) { continue; } if (objectName.equalsIgnoreCase(NPC_START)) { // Get center of rectangle float x = ((RectangleMapObject) object).getRectangle().getX(); float y = ((RectangleMapObject) object).getRectangle().getY(); // scale by the unit to convert from map coordinates x *= UNIT_SCALE; y *= UNIT_SCALE; npcStartPositions.add(new Vector2(x, y)); } } return npcStartPositions; }
protected void loadObject(MapLayer layer, Element element) { if (element.getName().equals("object")) { MapObject object = null; int x = element.getIntAttribute("x", 0); int y = (yUp ? mapHeightInPixels - element.getIntAttribute("y", 0) : element.getIntAttribute("y", 0)); int width = element.getIntAttribute("width", 0); int height = element.getIntAttribute("height", 0); 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] = Integer.parseInt(point[0]); vertices[i * 2 + 1] = Integer.parseInt(point[1]); if (yUp) { vertices[i * 2 + 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] = Integer.parseInt(point[0]); vertices[i * 2 + 1] = Integer.parseInt(point[1]); if (yUp) { vertices[i * 2 + 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, yUp ? y - height : y, width, height); } } if (object == null) { object = new RectangleMapObject(x, yUp ? y - height : y, width, height); } object.setName(element.getAttribute("name", null)); String type = element.getAttribute("type", null); if (type != null) { object.getProperties().put("type", type); } int gid = element.getIntAttribute("gid", -1); if (gid != -1) { object.getProperties().put("gid", gid); } object.getProperties().put("x", x); object.getProperties().put("y", yUp ? y - height : y); object.setVisible(element.getIntAttribute("visible", 1) == 1); Element properties = element.getChildByName("properties"); if (properties != null) { loadProperties(object.getProperties(), properties); } layer.getObjects().add(object); } }
public SMovementState update( Vector2 currentPosition, Vector2 nextPosition, float width, float height, float offset) { if (getTileLayer() != null) { // Make sure the positions are even valid if (!validPosition(nextPosition.x, currentPosition.y) || !validPosition(nextPosition.x + width, currentPosition.y)) { nextPosition.x = currentPosition.x; } if (!validPosition(currentPosition.x, nextPosition.y) || !validPosition(currentPosition.x + width, nextPosition.y + height)) { nextPosition.y = currentPosition.y; } BoundingBox new_x_bbox = new BoundingBox(new Vector2(nextPosition.x, currentPosition.y), width, height, offset); Vector2 x_tile_1 = getTileIndex(new_x_bbox.BL.x, new_x_bbox.BL.y); Vector2 x_tile_2 = getTileIndex(new_x_bbox.TR.x, new_x_bbox.TR.y); for (int i = (int) x_tile_1.x; i <= (int) x_tile_2.x; i++) { for (int j = (int) x_tile_1.y; j <= (int) x_tile_2.y; j++) { if (isTileCollidable(i, j)) { nextPosition.x = currentPosition.x; } } } BoundingBox new_y_bbox = new BoundingBox(new Vector2(currentPosition.x, nextPosition.y), width, height, offset); Vector2 y_tile_1 = getTileIndex(new_y_bbox.BL.x, new_y_bbox.BL.y); Vector2 y_tile_2 = getTileIndex(new_y_bbox.TR.x, new_y_bbox.TR.y); for (int i = (int) y_tile_1.x; i <= (int) y_tile_2.x; i++) { for (int j = (int) y_tile_1.y; j <= (int) y_tile_2.y; j++) { if (isTileCollidable(i, j)) { nextPosition.y = currentPosition.y; } } } } Vector2 characterCenter = new Vector2(nextPosition.x + width / 2, nextPosition.y + height / 2); Vector2 centerTile = getTileIndex(characterCenter.x, characterCenter.y); boolean exitFound = false; String exitFilename = null; Iterator<MapObject> objects = objectLayer.getObjects().iterator(); while (objects.hasNext()) { MapObject object = objects.next(); if (object.getProperties().containsKey("x") && object.getProperties().containsKey("y")) { Vector2 propertyCenter = getTileIndex( object.getProperties().get("x", Float.class).intValue(), object.getProperties().get("y", Float.class).intValue()); if (centerTile.x == propertyCenter.x && centerTile.y == propertyCenter.y) { if (object.getProperties().containsKey("exit_to")) { exitFound = true; exitFilename = object.getProperties().get("exit_to").toString(); } } } } // set the tiles if (debug) { _displayDebug(nextPosition, width, height, offset); } return new SMovementState(nextPosition, exitFound, exitFilename); }