Search in sources :

Example 11 with MapProperties

use of com.badlogic.gdx.maps.MapProperties in project libgdx by libgdx.

the class AtlasTmxMapLoader method loadTileset.

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);
        int offsetX = 0;
        int offsetY = 0;
        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;
        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }
        String atlasFilePath = map.getProperties().get("atlas", String.class);
        if (atlasFilePath == null) {
            FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
            if (atlasFile.exists())
                atlasFilePath = atlasFile.name();
        }
        if (atlasFilePath == null) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }
        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        String regionsName = name;
        for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        props.put("imagesource", imageSource);
        props.put("imagewidth", imageWidth);
        props.put("imageheight", imageHeight);
        props.put("tilewidth", tilewidth);
        props.put("tileheight", tileheight);
        props.put("margin", margin);
        props.put("spacing", spacing);
        if (imageSource != null && imageSource.length() > 0) {
            int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
            for (AtlasRegion region : atlas.findRegions(regionsName)) {
                // handle unused tile ids
                if (region != null) {
                    int tileid = region.index + 1;
                    if (tileid >= firstgid && tileid <= lastgid) {
                        StaticTiledMapTile tile = new StaticTiledMapTile(region);
                        tile.setId(tileid);
                        tile.setOffsetX(offsetX);
                        tile.setOffsetY(flipY ? -offsetY : offsetY);
                        tileset.putTile(tileid, tile);
                    }
                }
            }
        }
        for (Element tileElement : element.getChildrenByName("tile")) {
            int tileid = firstgid + tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(tileid);
            if (tile == null) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    // Is a tilemap with individual images.
                    String regionName = imageElement.getAttribute("source");
                    regionName = regionName.substring(0, regionName.lastIndexOf('.'));
                    AtlasRegion region = atlas.findRegion(regionName);
                    if (region == null)
                        throw new GdxRuntimeException("Tileset region not found: " + regionName);
                    tile = new StaticTiledMapTile(region);
                    tile.setId(tileid);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(tileid, tile);
                }
            }
            if (tile != null) {
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");
        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {
                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }
                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 12 with MapProperties

use of com.badlogic.gdx.maps.MapProperties in project libgdx by libgdx.

the class TiledMapObjectLoadingTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 100, 100);
    camera.zoom = 2;
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    font = new BitmapFont();
    batch = new SpriteBatch();
    map = new TmxMapLoader().load("data/maps/tiled-objects/test-load-mapobjects.tmx");
    MapProperties properties = map.getProperties();
    shapeRenderer = new ShapeRenderer();
}
Also used : TmxMapLoader(com.badlogic.gdx.maps.tiled.TmxMapLoader) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) MapProperties(com.badlogic.gdx.maps.MapProperties) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer)

Example 13 with MapProperties

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

the class LandingDetectorTest method testDetect_noLanding.

@Test
public void testDetect_noLanding() {
    GameState state = new GameState();
    GameEngine game = new GameEngine(new GameSettings(800, 600), state, new ScreenManager());
    Mockito.when(mockScreenManager.getScreen(Mockito.anyInt())).thenReturn(Mockito.mock(Screen.class));
    List<Cell> nextCells = new ArrayList<>();
    Cell landingCell = new Cell();
    TiledMapTile mockTile = Mockito.mock(TiledMapTile.class);
    MapProperties mapProperties = new MapProperties();
    mapProperties.put("notlanding", true);
    Mockito.when(mockTile.getProperties()).thenReturn(mapProperties);
    landingCell.setTile(mockTile);
    nextCells.add(landingCell);
    subject.detect(game, nextCells, mockPlayer, 100, 100);
    assertEquals("Mismatching game level", 1, game.getState().getLvl());
    Mockito.verifyZeroInteractions(mockSoundPlayer);
}
Also used : ScreenManager(com.relic.highflyer.screens.ScreenManager) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) Screen(com.badlogic.gdx.Screen) 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 14 with MapProperties

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

the class LandingDetectorTest method testDetect_hasLanding.

@Test
public void testDetect_hasLanding() {
    GameState state = new GameState();
    // state.setScore(110);
    GameEngine game = new GameEngine(new GameSettings(800, 600), state, new ScreenManager());
    Mockito.when(mockScreenManager.getScreen(Mockito.anyInt())).thenReturn(Mockito.mock(Screen.class));
    List<Cell> nextCells = new ArrayList<>();
    Cell landingCell = new Cell();
    TiledMapTile mockTile = Mockito.mock(TiledMapTile.class);
    MapProperties mapProperties = new MapProperties();
    mapProperties.put("landing", true);
    Mockito.when(mockTile.getProperties()).thenReturn(mapProperties);
    landingCell.setTile(mockTile);
    nextCells.add(landingCell);
    subject.detect(game, nextCells, mockPlayer, 100, 100);
    assertEquals("Mismatching game level", 2, game.getState().getLvl());
    Mockito.verify(mockPlayer).setX(Mockito.eq(0f));
    Mockito.verify(mockPlayer).setY(Mockito.eq(260f));
    Mockito.verify(mockSoundPlayer).playSound(Mockito.eq("data/sounds/Finish.wav"));
// assertEquals("Mismatching game score", 10, state.getScore());
}
Also used : ScreenManager(com.relic.highflyer.screens.ScreenManager) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) Screen(com.badlogic.gdx.Screen) 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 15 with MapProperties

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

the class PowerUpDetectorTest method testDetect_noCollistion.

@Test
public void testDetect_noCollistion() {
    GameState state = new GameState();
    state.setScore(100);
    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("notpower", "star");
    Mockito.when(mockTile.getProperties()).thenReturn(mapProperties);
    collisionCell.setTile(mockTile);
    nextCells.add(collisionCell);
    subject.detect(game, nextCells, mockPlayer, 100, 100);
    Mockito.verifyZeroInteractions(mockSoundPlayer);
    assertEquals("Mismatching game score", 100, 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)

Aggregations

MapProperties (com.badlogic.gdx.maps.MapProperties)19 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)10 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)10 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 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)5 AnimatedTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile)4 Array (com.badlogic.gdx.utils.Array)4 Element (com.badlogic.gdx.utils.XmlReader.Element)4 Screen (com.badlogic.gdx.Screen)3 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)3 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)3 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)3 Tile (objects.Tile)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2