Search in sources :

Example 11 with Cell

use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell in project libgdx by libgdx.

the class SuperKoalio method getTiles.

private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) {
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
    rectPool.freeAll(tiles);
    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);
            }
        }
    }
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Rectangle(com.badlogic.gdx.math.Rectangle) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 12 with Cell

use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell in project libgdx by libgdx.

the class SuperKoalio method renderDebug.

private void renderDebug() {
    debugRenderer.setProjectionMatrix(camera.combined);
    debugRenderer.begin(ShapeType.Line);
    debugRenderer.setColor(Color.RED);
    debugRenderer.rect(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    debugRenderer.setColor(Color.YELLOW);
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
    for (int y = 0; y <= layer.getHeight(); y++) {
        for (int x = 0; x <= layer.getWidth(); x++) {
            Cell cell = layer.getCell(x, y);
            if (cell != null) {
                if (camera.frustum.boundsInFrustum(x + 0.5f, y + 0.5f, 0, 1, 1, 0))
                    debugRenderer.rect(x, y, 1, 1);
            }
        }
    }
    debugRenderer.end();
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 13 with Cell

use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell in project libgdx by libgdx.

the class BaseTmxMapLoader method loadTileLayer.

protected void loadTileLayer(TiledMap map, Element element) {
    if (element.getName().equals("layer")) {
        int width = element.getIntAttribute("width", 0);
        int height = element.getIntAttribute("height", 0);
        int tileWidth = element.getParent().getIntAttribute("tilewidth", 0);
        int tileHeight = element.getParent().getIntAttribute("tileheight", 0);
        TiledMapTileLayer layer = new TiledMapTileLayer(width, height, tileWidth, tileHeight);
        loadBasicLayerInfo(layer, element);
        int[] ids = getTileIds(element, width, height);
        TiledMapTileSets tilesets = map.getTileSets();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int id = ids[y * width + x];
                boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);
                TiledMapTile tile = tilesets.getTile(id & ~MASK_CLEAR);
                if (tile != null) {
                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                    cell.setTile(tile);
                    layer.setCell(x, flipY ? height - 1 - y : y, cell);
                }
            }
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 14 with Cell

use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell in project libgdx by libgdx.

the class BaseTmxMapLoader method createTileLayerCell.

protected Cell createTileLayerCell(boolean flipHorizontally, boolean flipVertically, boolean flipDiagonally) {
    Cell cell = new Cell();
    if (flipDiagonally) {
        if (flipHorizontally && flipVertically) {
            cell.setFlipHorizontally(true);
            cell.setRotation(Cell.ROTATE_270);
        } else if (flipHorizontally) {
            cell.setRotation(Cell.ROTATE_270);
        } else if (flipVertically) {
            cell.setRotation(Cell.ROTATE_90);
        } else {
            cell.setFlipVertically(true);
            cell.setRotation(Cell.ROTATE_270);
        }
    } else {
        cell.setFlipHorizontally(flipHorizontally);
        cell.setFlipVertically(flipVertically);
    }
    return cell;
}
Also used : Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 15 with Cell

use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell in project libgdx by libgdx.

the class IsometricStaggeredTiledMapRenderer method renderTileLayer.

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final Color batchColor = batch.getColor();
    final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());
    final int layerWidth = layer.getWidth();
    final int layerHeight = layer.getHeight();
    final float layerOffsetX = layer.getOffsetX() * unitScale;
    // offset in tiled is y down, so we flip it
    final float layerOffsetY = -layer.getOffsetY() * unitScale;
    final float layerTileWidth = layer.getTileWidth() * unitScale;
    final float layerTileHeight = layer.getTileHeight() * unitScale;
    final float layerTileWidth50 = layerTileWidth * 0.50f;
    final float layerTileHeight50 = layerTileHeight * 0.50f;
    final int minX = Math.max(0, (int) (((viewBounds.x - layerTileWidth50 - layerOffsetX) / layerTileWidth)));
    final int maxX = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth + layerTileWidth50 - layerOffsetX) / layerTileWidth));
    final int minY = Math.max(0, (int) (((viewBounds.y - layerTileHeight - layerOffsetY) / layerTileHeight)));
    final int maxY = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight50));
    for (int y = maxY - 1; y >= minY; y--) {
        float offsetX = (y % 2 == 1) ? layerTileWidth50 : 0;
        for (int x = maxX - 1; x >= minX; x--) {
            final TiledMapTileLayer.Cell cell = layer.getCell(x, y);
            if (cell == null)
                continue;
            final TiledMapTile tile = cell.getTile();
            if (tile != null) {
                final boolean flipX = cell.getFlipHorizontally();
                final boolean flipY = cell.getFlipVertically();
                final int rotations = cell.getRotation();
                TextureRegion region = tile.getTextureRegion();
                float x1 = x * layerTileWidth - offsetX + tile.getOffsetX() * unitScale + layerOffsetX;
                float y1 = y * layerTileHeight50 + tile.getOffsetY() * unitScale + layerOffsetY;
                float x2 = x1 + region.getRegionWidth() * unitScale;
                float y2 = y1 + region.getRegionHeight() * unitScale;
                float u1 = region.getU();
                float v1 = region.getV2();
                float u2 = region.getU2();
                float v2 = region.getV();
                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;
                            }
                    }
                }
                batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
            }
        }
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) Color(com.badlogic.gdx.graphics.Color) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Aggregations

Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)19 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)13 Point (java.awt.Point)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)5 Color (com.badlogic.gdx.graphics.Color)3 Texture (com.badlogic.gdx.graphics.Texture)3 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)3 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 MapLayers (com.badlogic.gdx.maps.MapLayers)2 Rectangle (com.badlogic.gdx.math.Rectangle)2 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)2 Element (com.badlogic.gdx.utils.XmlReader.Element)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 MapLayer (com.badlogic.gdx.maps.MapLayer)1 MapObject (com.badlogic.gdx.maps.MapObject)1 MapObjects (com.badlogic.gdx.maps.MapObjects)1 HexagonalTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer)1