Search in sources :

Example 11 with TiledMapTile

use of com.badlogic.gdx.maps.tiled.TiledMapTile in project high-flyer by sangngh.

the class PowerUpDetectorTest method testDetect_hasCollision.

@Test
public void testDetect_hasCollision() {
    GameState state = new GameState();
    state.setScore(100);
    typePower = "";
    GameEngine game = new GameEngine(new GameSettings(800, 600), state, new ScreenManager());
    List<Cell> nextCells = new ArrayList<>();
    Cell collisionCell = new Cell();
    TiledMapTile mockTile = Mockito.mock(TiledMapTile.class);
    MapProperties mapProperties = new MapProperties();
    mapProperties.put("power", "star");
    Mockito.when(mockTile.getProperties()).thenReturn(mapProperties);
    collisionCell.setTile(mockTile);
    nextCells.add(collisionCell);
    subject.detect(game, nextCells, mockPlayer, 100, 100);
    Mockito.verify(mockSoundPlayer).playSound(Mockito.eq("data/sounds/PowerUp.wav"));
    assertEquals("Mismatching game score", 110, state.getScore());
}
Also used : ScreenManager(com.relic.highflyer.screens.ScreenManager) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) ArrayList(java.util.ArrayList) MapProperties(com.badlogic.gdx.maps.MapProperties) GameState(com.relic.highflyer.GameState) GameEngine(com.relic.highflyer.GameEngine) GameSettings(com.relic.highflyer.GameSettings) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) Test(org.junit.Test)

Example 12 with TiledMapTile

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

the class TiledMapPacker method stripUnusedTiles.

private void stripUnusedTiles() {
    int mapWidth = map.getProperties().get("width", Integer.class);
    int mapHeight = map.getProperties().get("height", Integer.class);
    int numlayers = map.getLayers().getCount();
    int bucketSize = mapWidth * mapHeight * numlayers;
    Iterator<MapLayer> it = map.getLayers().iterator();
    while (it.hasNext()) {
        MapLayer layer = it.next();
        // some layers can be plain MapLayer instances (ie. object groups), just ignore them
        if (layer instanceof TiledMapTileLayer) {
            TiledMapTileLayer tlayer = (TiledMapTileLayer) layer;
            for (int y = 0; y < mapHeight; ++y) {
                for (int x = 0; x < mapWidth; ++x) {
                    if (tlayer.getCell(x, y) != null) {
                        TiledMapTile tile = tlayer.getCell(x, y).getTile();
                        if (tile instanceof AnimatedTiledMapTile) {
                            AnimatedTiledMapTile aTile = (AnimatedTiledMapTile) tile;
                            for (StaticTiledMapTile t : aTile.getFrameTiles()) {
                                addTile(t, bucketSize);
                            }
                        }
                        // Adds non-animated tiles and the base animated tile
                        addTile(tile, bucketSize);
                    }
                }
            }
        }
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) MapLayer(com.badlogic.gdx.maps.MapLayer) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 13 with TiledMapTile

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

the class HexagonalTiledMapRenderer method renderCell.

/** render a single cell */
private void renderCell(final TiledMapTileLayer.Cell cell, final float x, final float y, final float color) {
    if (cell != null) {
        final TiledMapTile tile = cell.getTile();
        if (tile != null) {
            if (tile instanceof AnimatedTiledMapTile)
                return;
            final boolean flipX = cell.getFlipHorizontally();
            final boolean flipY = cell.getFlipVertically();
            final int rotations = cell.getRotation();
            TextureRegion region = tile.getTextureRegion();
            float x1 = x + tile.getOffsetX() * unitScale;
            float y1 = y + tile.getOffsetY() * unitScale;
            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 == 2) {
                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;
            }
            batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
        }
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile)

Example 14 with TiledMapTile

use of com.badlogic.gdx.maps.tiled.TiledMapTile 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)

Example 15 with TiledMapTile

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

the class TiledMapAssetManagerTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 10, 10);
    camera.zoom = 2;
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    font = new BitmapFont();
    batch = new SpriteBatch();
    assetManager = new AssetManager();
    assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
    assetManager.load("data/maps/tiled/isometric_grass_and_water.tmx", TiledMap.class);
    assetManager.finishLoading();
    map = assetManager.get("data/maps/tiled/isometric_grass_and_water.tmx");
    renderer = new IsometricTiledMapRenderer(map, 1f / 64f);
    String mapCustomValue = map.getProperties().get(MAP_PROPERTY_NAME, String.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + MAP_PROPERTY_NAME + ", Value : " + mapCustomValue);
    if (!MAP_PROPERTY_VALUE.equals(mapCustomValue)) {
        throw new RuntimeException("Failed to get map properties");
    }
    boolean boolCustomValue = map.getProperties().get(BOOL_PROPERTY_NAME, Boolean.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + BOOL_PROPERTY_NAME + ", Value : " + boolCustomValue);
    if (boolCustomValue != BOOL_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get boolean map properties");
    }
    int intCustomValue = map.getProperties().get(INT_PROPERTY_NAME, Integer.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + INT_PROPERTY_NAME + ", Value : " + intCustomValue);
    if (intCustomValue != INT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get int map properties");
    }
    float floatCustomValue = map.getProperties().get(FLOAT_PROPERTY_NAME, Float.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + FLOAT_PROPERTY_NAME + ", Value : " + floatCustomValue);
    if (floatCustomValue != FLOAT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get float map properties");
    }
    TiledMapTileSet tileset = map.getTileSets().getTileSet(0);
    String tilesetCustomValue = tileset.getProperties().get(TILESET_PROPERTY_NAME, String.class);
    if (!TILESET_PROPERTY_VALUE.equals(tilesetCustomValue)) {
        throw new RuntimeException("Failed to get tileset properties");
    }
    TiledMapTile tile = tileset.getTile(1);
    String tileCustomValue = tile.getProperties().get(TILE_PROPERTY_NAME, String.class);
    if (!TILE_PROPERTY_VALUE.equals(tileCustomValue)) {
        throw new RuntimeException("Failed to get tile properties");
    }
    MapLayer layer = map.getLayers().get(0);
    String layerCustomValue = layer.getProperties().get(LAYER_PROPERTY_NAME, String.class);
    if (!LAYER_PROPERTY_VALUE.equals(layerCustomValue)) {
        throw new RuntimeException("Failed to get layer properties");
    }
}
Also used : TmxMapLoader(com.badlogic.gdx.maps.tiled.TmxMapLoader) AssetManager(com.badlogic.gdx.assets.AssetManager) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) MapLayer(com.badlogic.gdx.maps.MapLayer) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) InternalFileHandleResolver(com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) IsometricTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)21 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)17 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)10 MapProperties (com.badlogic.gdx.maps.MapProperties)10 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)7 GameEngine (com.relic.highflyer.GameEngine)7 GameSettings (com.relic.highflyer.GameSettings)7 GameState (com.relic.highflyer.GameState)7 ScreenManager (com.relic.highflyer.screens.ScreenManager)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Color (com.badlogic.gdx.graphics.Color)4 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)4 AnimatedTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile)4 Screen (com.badlogic.gdx.Screen)3 Tile (objects.Tile)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 Texture (com.badlogic.gdx.graphics.Texture)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2