List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer getCell
public Cell getCell(int x, int y)
From source file:MyGdxGame.java
License:Apache License
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls"); rectPool.freeAll(tiles);//from w ww . ja v a 2 s . c o m tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } }
From source file:br.com.animvs.koalory.entities.engine.graphics.tiles.TileRenderer.java
License:Apache License
@Override public void renderTileLayer(TiledMapTileLayer layer) { final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity()); final int layerWidth = layer.getWidth(); final int layerHeight = layer.getHeight(); final float layerTileWidth = layer.getTileWidth() * unitScale; final float layerTileHeight = layer.getTileHeight() * unitScale; final int col1 = Math.max(0, (int) (cacheBounds.x / layerTileWidth)); final int col2 = Math.min(layerWidth, (int) ((cacheBounds.x + cacheBounds.width + layerTileWidth) / layerTileWidth)); final int row1 = Math.max(0, (int) (cacheBounds.y / layerTileHeight)); final int row2 = Math.min(layerHeight, (int) ((cacheBounds.y + cacheBounds.height + layerTileHeight) / layerTileHeight)); canCacheMoreN = row2 < layerHeight; canCacheMoreE = col2 < layerWidth; canCacheMoreW = col1 > 0;//from ww w.ja v a 2 s. c o m canCacheMoreS = row1 > 0; float[] vertices = this.vertices; for (int row = row2; row >= row1; row--) { for (int col = col1; col < col2; col++) { final TiledMapTileLayer.Cell cell = layer.getCell(col, row); if (cell == null) continue; final TiledMapTile tile = cell.getTile(); if (tile == null) continue; count++; final boolean flipX = cell.getFlipHorizontally(); final boolean flipY = cell.getFlipVertically(); final int rotations = cell.getRotation(); final TextureRegion region = tile.getTextureRegion(); final Texture texture = region.getTexture(); final float x1 = col * layerTileWidth + tile.getOffsetX() * unitScale; final float y1 = row * layerTileHeight + tile.getOffsetY() * unitScale; final float x2 = x1 + region.getRegionWidth() * unitScale; final float y2 = y1 + region.getRegionHeight() * unitScale; final float adjustX = 0.5f / texture.getWidth(); final float adjustY = 0.5f / texture.getHeight(); final float u1 = region.getU() + adjustX; final float v1 = region.getV2() - adjustY; final float u2 = region.getU2() - adjustX; final float v2 = region.getV() + adjustY; vertices[X1] = x1; vertices[Y1] = y1; vertices[C1] = color; vertices[U1] = u1; vertices[V1] = v1; vertices[X2] = x1; vertices[Y2] = y2; vertices[C2] = color; vertices[U2] = u1; vertices[V2] = v2; vertices[X3] = x2; vertices[Y3] = y2; vertices[C3] = color; vertices[U3] = u2; vertices[V3] = v2; vertices[X4] = x2; vertices[Y4] = y1; vertices[C4] = color; vertices[U4] = u2; vertices[V4] = v1; if (flipX) { float temp = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = temp; temp = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = temp; } if (flipY) { float temp = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = temp; temp = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = temp; } if (rotations != 0) { switch (rotations) { case Cell.ROTATE_90: { float tempV = vertices[V1]; vertices[V1] = vertices[V2]; vertices[V2] = vertices[V3]; vertices[V3] = vertices[V4]; vertices[V4] = tempV; float tempU = vertices[U1]; vertices[U1] = vertices[U2]; vertices[U2] = vertices[U3]; vertices[U3] = vertices[U4]; vertices[U4] = tempU; break; } case Cell.ROTATE_180: { float tempU = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = tempU; tempU = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = tempU; float tempV = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = tempV; tempV = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = tempV; break; } case Cell.ROTATE_270: { float tempV = vertices[V1]; vertices[V1] = vertices[V4]; vertices[V4] = vertices[V3]; vertices[V3] = vertices[V2]; vertices[V2] = tempV; float tempU = vertices[U1]; vertices[U1] = vertices[U4]; vertices[U4] = vertices[U3]; vertices[U3] = vertices[U2]; vertices[U2] = tempU; break; } } } spriteCache.add(texture, vertices, 0, 20); } } }
From source file:ca.hiphiparray.amazingmaze.MazeScreen.java
License:Open Source License
/** Create the bounding boxes for collision detection. */ private void createBoundingBoxes() { obstacleBoxes = new Array<Rectangle>(false, 16); TiledMapTileLayer objects = (TiledMapTileLayer) map.getLayers().get(MapFactory.OBJECT_LAYER); for (int r = 0; r < mapHeight; r++) { for (int c = 0; c < mapWidth; c++) { Cell cell = objects.getCell(c, r); if (cell != null) { obstacleBoxes.add(new Rectangle(c, r, 1, 1)); }/*from w w w. ja v a 2 s . c o m*/ } } wireBoxes = new Array<Rectangle>(false, 16); TiledMapTileLayer wires = (TiledMapTileLayer) map.getLayers().get(MapFactory.WIRE_LAYER); for (int r = 0; r < mapHeight; r++) { for (int c = 0; c < mapWidth; c++) { WireCell wire = (WireCell) wires.getCell(c, r); if (wire != null && wire.isOn()) { wireBoxes.add(new Rectangle(c + 5f / 16f, r, 6f / 16f, 1)); } } } fishBoxes = new Array<Rectangle>(false, 16); cheeseBoxes = new Array<Rectangle>(false, 16); TiledMapTileLayer items = (TiledMapTileLayer) map.getLayers().get(MapFactory.ITEM_LAYER); for (int r = 0; r < mapHeight; r++) { for (int c = 0; c < mapWidth; c++) { Cell item = items.getCell(c, r); if (item != null) { if (item.getClass() == FishCell.class) { fishBoxes.add(new Rectangle(c, r, 1, 1)); } else { cheeseBoxes.add(new Rectangle(c, r, 1, 1)); } } } } }
From source file:ca.hiphiparray.amazingmaze.MazeScreen.java
License:Open Source License
@Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { Vector3 worldClickPos = viewport.getCamera().unproject(new Vector3(screenX, screenY, 0)); int x = (int) worldClickPos.x; int y = (int) worldClickPos.y; TiledMapTileLayer objects = (TiledMapTileLayer) map.getLayers().get(MapFactory.OBJECT_LAYER); Cell gate;/* w ww . j a v a 2s . c om*/ int newID; for (Point point : gateLocations) { if (point.x == x && point.y == y) { gate = objects.getCell(x, y); newID = TileIDs.computeID(TileIDs.stripElectricState(gate.getTile().getId())); if (button == Buttons.LEFT) { newID = TileIDs.computeID(newID, TileIDs.ON); updateWires(x, y, TileIDs.ON); } else if (button == Buttons.RIGHT) { newID = TileIDs.computeID(newID, TileIDs.OFF); updateWires(x, y, TileIDs.OFF); } else if (button == Buttons.MIDDLE) { newID = TileIDs.computeID(newID, TileIDs.UNKNOWN); updateWires(x, y, TileIDs.UNKNOWN); } else { return true; } gate.setTile(game.assets.tiles.getTile(newID)); break; } } return true; }
From source file:ca.hiphiparray.amazingmaze.MazeScreen.java
License:Open Source License
/** * Update the wires connected to the gate at (x, y). * * @param x the x position of the gate being updated. * @param y the y position of the gate being updated. * @param state the new state of the wires. */// w ww . ja v a2 s . c o m private void updateWires(int x, int y, int state) { TiledMapTileLayer wires = (TiledMapTileLayer) map.getLayers().get(MapFactory.WIRE_LAYER); for (int r = y + 1; r < mapHeight; r++) { if (wires.getCell(x, r) != null) { Cell cell = wires.getCell(x, r); int newID = TileIDs.stripElectricState(cell.getTile().getId()); newID = TileIDs.computeID(newID, state); cell.setTile(game.assets.tiles.getTile(newID)); } else { break; } } for (int r = y - 1; r >= 0; r--) { if (wires.getCell(x, r) != null) { Cell cell = wires.getCell(x, r); int newID = TileIDs.stripElectricState(cell.getTile().getId()); newID = TileIDs.computeID(newID, state); cell.setTile(game.assets.tiles.getTile(newID)); } else { break; } } }
From source file:ca.hiphiparray.amazingmaze.Player.java
License:Open Source License
/** Handle the player collecting fish. */ private void collectFish() { Rectangle thisBox = getBoundingRectangle(); for (int i = 0; i < maze.fishBoxes.size; i++) { if (thisBox.overlaps(maze.fishBoxes.get(i))) { TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER); int x = (int) maze.fishBoxes.get(i).x; int y = (int) maze.fishBoxes.get(i).y; FishColour colour = ((FishCell) layer.getCell(x, y)).getColour(); layer.setCell(x, y, null);/* w w w . j a v a 2s . c om*/ maze.fishBoxes.removeIndex(i); switch (colour) { case BLUE: blueCollected++; break; case PURPLE: purpleCollected++; break; case GREEN: greenCollected++; break; case RED: redCollected++; break; case ORANGE: orangeCollected++; break; } break; } } }
From source file:com.agateau.pixelwheels.map.Track.java
License:Open Source License
public Array<Vector2> findStartTilePositions() { Array<Vector2> lst = new Array<Vector2>(); TiledMapTileLayer groundLayer = mBackgroundLayers.get(0); for (int ty = 0; ty < groundLayer.getHeight(); ++ty) { for (int tx = 0; tx < groundLayer.getWidth(); ++tx) { TiledMapTileLayer.Cell cell = groundLayer.getCell(tx, ty); if (cell == null) { continue; }//from w w w . j a va 2s. c o m int tileId = cell.getTile().getId(); if (tileId == mStartTileId) { Vector2 pos = new Vector2(tx * mTileWidth + mTileWidth / 2, ty * mTileHeight); lst.add(pos); } } } return lst; }
From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java
License:Apache License
/** * Reads a file describing the collision boundaries that should be set * per-tile and adds static bodies to the boxd world. * /*w ww. j a v a 2 s .c o m*/ * @param collisionsFile * @param world * @param pixelsPerMeter * the pixels per meter scale used for this world */ public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) { /** * Detect the tiles and dynamically create a representation of the map * layout, for collision detection. Each tile has its own collision * rules stored in an associated file. * * The file contains lines in this format (one line per type of tile): * tileNumber XxY,XxY XxY,XxY * * Ex: * * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31 * * For a 32x32 tileset, the above describes one line segment for tile #3 * and two for tile #4. Tile #3 has a line segment across the top. Tile * #1 has a line segment across most of the top and a line segment from * the top to the bottom, 30 pixels in. */ // FileHandle fh = Gdx.files.internal(collisionsFile); // String collisionFile = fh.readString(); // String lines[] = collisionFile.split("\\r?\\n"); // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new // HashMap<Integer, ArrayList<LineSegment>>(); // /** // * Some locations on the map (perhaps most locations) are "undefined", // * empty space, and will have the tile type 0. This code adds an empty // * list of line segments for this "default" tile. // */ // tileCollisionJoints.put(Integer.valueOf(0), new // ArrayList<LineSegment>()); // for (int n = 0; n < lines.length; n++) { // String cols[] = lines[n].split(" "); // int tileNo = Integer.parseInt(cols[0]); // // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>(); // // for (int m = 1; m < cols.length; m++) { // String coords[] = cols[m].split(","); // // String start[] = coords[0].split("x"); // String end[] = coords[1].split("x"); // // tmp.add(new LineSegment(Integer.parseInt(start[0]), // Integer.parseInt(start[1]), // Integer.parseInt(end[0]), Integer.parseInt(end[1]), "")); // } // // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp); // } ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>(); for (int l = 0; l < getMap().getLayers().getCount(); l++) { MapLayer nextLayer = getMap().getLayers().get(l); if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) { TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer; if (layer.getProperties().containsKey("collide")) { for (int y = 0; y < layer.getHeight(); y++) { for (int x = 0; x < layer.getWidth(); x++) { Cell tile = layer.getCell(x, y); if (tile != null) { int tileID = tile.getTile().getId(); int start = 0; String type = ""; if (tile.getTile().getProperties().containsKey("type")) { type = (String) getMap().getTileSets().getTile(tileID).getProperties() .get("type"); } if (layer.getProperties().containsKey("WALLS")) { type = "WALLS"; addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32, collisionLineSegments, type); } else if (layer.getProperties().containsKey("STAIRS")) { if (!type.equals("LEFTSTAIRS")) { type = "STAIRS"; addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } else { addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32, collisionLineSegments, type); } } else if (layer.getProperties().containsKey("PLATFORMS")) { type = "PLATFORMS"; addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } } } } } } else { MapObjects objects = nextLayer.getObjects(); for (MapObject o : objects) { RectangleMapObject rect = (RectangleMapObject) o; if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) { Rectangle shape = rect.getRectangle(); BodyDef nodeBodyDef = new BodyDef(); nodeBodyDef.type = BodyDef.BodyType.StaticBody; nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX, (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX); Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef); if (!nextLayer.getName().equals("DOORS")) { String set = (String) rect.getProperties().get("set"); nodeBody.setUserData(new AINode(Integer.parseInt(set))); } else { if (rect.getProperties().containsKey("to_level") && rect.getProperties().containsKey("exitX") && rect.getProperties().containsKey("exitY")) { String to_level = (String) rect.getProperties().get("to_level"); String xPos = (String) rect.getProperties().get("exitX"); String yPos = (String) rect.getProperties().get("exitY"); nodeBody.setUserData(new Door(to_level, xPos, yPos)); } else { nodeBody.setUserData(new Door("9999", "1024", "256")); } } PolygonShape nodeShape = new PolygonShape(); nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX, shape.height * 0.5f * Util.PIXEL_TO_BOX); FixtureDef fixture = new FixtureDef(); fixture.shape = nodeShape; fixture.isSensor = true; fixture.density = 0; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; nodeBody.createFixture(fixture); nodeShape.dispose(); } } } } int platnum = 0; for (LineSegment lineSegment : collisionLineSegments) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; groundBodyDef.position.set(0, 0); Body groundBody = GamePlayManager.world.createBody(groundBodyDef); if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) { groundBody.setUserData( new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end())); } else if (lineSegment.type.equals("WALLS")) { groundBody.setUserData(new Entity().setType(EntityType.WALL)); } else { Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()); groundBody.setUserData(plat); if (GamePlayManager.lowestPlatInLevel == null || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) { GamePlayManager.lowestPlatInLevel = plat; } } EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter), lineSegment.end().scl(1 / pixelsPerMeter)); FixtureDef fixture = new FixtureDef(); fixture.shape = environmentShape; fixture.isSensor = true; fixture.density = 1f; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES; groundBody.createFixture(fixture); environmentShape.dispose(); } /** * Drawing a boundary around the entire map. We can't use a box because * then the world objects would be inside and the physics engine would * try to push them out. */ TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3); BodyDef bodydef = new BodyDef(); bodydef.type = BodyType.StaticBody; bodydef.position.set(0, 0); Body body = GamePlayManager.world.createBody(bodydef); // left wall EdgeShape mapBounds = new EdgeShape(); if (level == 1) { mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new Entity().setType(EntityType.WALL)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } else { mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f), new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new LevelWall(level - 1)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } // right wall body = GamePlayManager.world.createBody(bodydef); body.setUserData(new LevelWall(level + 1)); EdgeShape mapBounds2 = new EdgeShape(); mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX), new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); Fixture fixture = body.createFixture(mapBounds2, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); // roof body = GamePlayManager.world.createBody(bodydef); body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32), new Vector2(layer.getWidth() * 32, layer.getHeight()))); EdgeShape mapBounds3 = new EdgeShape(); mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX), new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); fixture = body.createFixture(mapBounds3, 0); fixture.setFilterData(filter); mapBounds.dispose(); mapBounds2.dispose(); mapBounds3.dispose(); }
From source file:com.badlogic.gdx.tests.superkoalio.SuperKoalio.java
License:Apache License
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); rectPool.freeAll(tiles);/*from ww w. j a v a2 s .c om*/ tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } }
From source file:com.barconr.games.marblegame.Maze3Drenderer.java
License:Apache License
private void createModels(TiledMap tiledmap) { // Load the Tiled map layer and store some relevant variables TiledMapTileLayer tilelayer = (TiledMapTileLayer) (tiledmap.getLayers().get(0)); layerHeight = tilelayer.getHeight(); layerWidth = tilelayer.getWidth();/* w w w . ja va 2 s . c om*/ tileWidth = tileHeight = CUBE_SIZE; for (int y_pos = 0; y_pos < layerHeight; y_pos++) { for (int x_pos = 0; x_pos < layerWidth; x_pos++) { //boolean impassibleBlock = tilelayer.getCell(x_pos, layerHeight-y_pos-1).getTile().getProperties().containsKey("block"); boolean impassibleBlock = tilelayer.getCell(x_pos, y_pos).getTile().getProperties() .containsKey("block"); if (impassibleBlock) { //Draw a cube here float xcord = x_pos * tileWidth; float ycord = y_pos * tileHeight; addBox(new Vector3(xcord, ycord, 0)); // mazeBuilder.createMazePart(xcord,ycord,0); } } } //Background addBox(new Vector3((layerWidth * tileWidth) / 2, (layerHeight * tileHeight) / 2, -CUBE_SIZE), layerWidth * tileWidth, layerHeight * tileHeight, CUBE_SIZE, Color.GRAY); }