com.badlogic.gdx.maps.tiled.TiledMapTileLayer Java Examples

The following examples show how to use com.badlogic.gdx.maps.tiled.TiledMapTileLayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlayerCar.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
private void updateCarFriction () {
	Vector2 tilePosition = carState.tilePosition;

	if (frictionMap != null && gameWorld.isValidTilePosition(tilePosition)) {
		// compute realsize-based pixel offset car-tile (top-left origin)
		float scaledTileSize = gameWorld.getTileSizePx();
		float tsx = tilePosition.x * scaledTileSize;
		float tsy = tilePosition.y * scaledTileSize;
		offset.set(Convert.mt2px(getBody().getPosition()));
		offset.y = gameWorld.worldSizePx.y - offset.y;
		offset.x = offset.x - tsx;
		offset.y = offset.y - tsy;
		offset.scl(gameWorld.getTileSizePxInv()).scl(gameWorld.tileWidth);

		TiledMapTileLayer layerTrack = gameWorld.getLayer(Layer.Track);

		// int id = layerTrack.getCell((int)tilePosition.x, gameWorld.mapHeight - (int)tilePosition.y - 1).getTile().getId() - 1;
		int id = layerTrack.getCell((int)tilePosition.x, (int)tilePosition.y).getTile().getId() - 1;

		// int xOnMap = (id %4) * (int)gameWorld.map.tileWidth + (int)offset.x;
		// int yOnMap = (int)( id/4f ) * (int)gameWorld.map.tileWidth + (int)offset.y;

		// bit twiddling, faster version
		int xOnMap = (id & 3) * (int)gameWorld.tileWidth + (int)offset.x;
		int yOnMap = (id >> 2) * (int)gameWorld.tileWidth + (int)offset.y;

		int pixel = frictionMap.getPixel(xOnMap, yOnMap);
		boolean inTrack = (pixel == -256);
		frictionMean.addValue((inTrack ? 0 : -1));

		// Gdx.app.log( "PlayerCar", "xmap=" + xOnMap + ", ymap=" + yOnMap );
		// Gdx.app.log( "PlayerCar", "mean=" + frictionMean.getMean() + ", pixel=" + pixel + ", xmap=" + xOnMap +
		// ", ymap=" + yOnMap );
		// Gdx.app.log("PlayerCar", "id=" + id);
		// Gdx.app.log("PlayerCar", "#" + id + ", xt=" + (int)tilePosition.x + "," + (int)tilePosition.y);

	} else {
		Gdx.app.log("PlayerCar", "PlayerCar out of map!");
	}
}
 
Example #2
Source File: UAtlasTmxMapLoader.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
protected void loadTileLayer (TiledMap map, Element element) {
	if (element.getName().equals("layer")) {
		String name = element.getAttribute("name", null);
		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);
		boolean visible = element.getIntAttribute("visible", 1) == 1;
		float opacity = element.getFloatAttribute("opacity", 1.0f);
		TiledMapTileLayer layer = new TiledMapTileLayer(width, height, tileWidth, tileHeight);
		layer.setVisible(visible);
		layer.setOpacity(opacity);
		layer.setName(name);

		int[] ids = TmxMapHelper.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, yUp ? height - 1 - y : y, cell);
				}
			}
		}

		Element properties = element.getChildByName("properties");
		if (properties != null) {
			loadProperties(layer.getProperties(), properties);
		}
		map.getLayers().add(layer);
	}
}
 
Example #3
Source File: TiledMapActor.java    From Norii with Apache License 2.0 5 votes vote down vote up
public TiledMapActor(final Map tiledMap, final TiledMapTileLayer tiledLayer, final TiledMapTileLayer.Cell cell) {
	this.tiledMap = tiledMap;
	this.tiledLayer = tiledLayer;
	this.cell = cell;
	isFreeSpawn = false;
	isHovered = false;
	observers = new Array<TiledMapObserver>();
}
 
Example #4
Source File: TiledMapStage.java    From Norii with Apache License 2.0 5 votes vote down vote up
public TiledMapStage(Map tiledMap, String layername, BattleManager battlemanager) {
    this.tiledMap = tiledMap;
    this.battlemanager = battlemanager;
    TiledMapTileLayer tiledLayer = (TiledMapTileLayer) tiledMap.getCurrentTiledMap().getLayers().get(layername);
    actors = new TiledMapActor[tiledLayer.getWidth()][tiledLayer.getHeight()];
    createActorsForLayer(tiledLayer);  
}
 
Example #5
Source File: TiledMapStage.java    From Norii with Apache License 2.0 5 votes vote down vote up
private void createActorsForLayer(TiledMapTileLayer tiledLayer) {
    for (int x = 0; x < tiledLayer.getWidth(); x++) {
        for (int y = 0; y < tiledLayer.getHeight(); y++) {
            TiledMapTileLayer.Cell cell = tiledLayer.getCell(x, y);
            TiledMapActor actor = new TiledMapActor(tiledMap, tiledLayer, cell);
            actors[x][y] = actor;
            
            initiateActor(x, y, actor);
            addEventListener(actor);
        }
    }
}
 
Example #6
Source File: WorldRenderer.java    From killingspree with MIT License 5 votes vote down vote up
public void loadLevel(String level, boolean isServer, String name) {
    this.isServer = isServer;
    map = new TmxMapLoader().load(level);
    TiledMapTileLayer layer = (TiledMapTileLayer) map.
                                getLayers().get("terrain");
    VIEWPORT_WIDTH = (int) (layer.getTileWidth() * layer.getWidth());
    VIEWPORT_HEIGHT = (int) (layer.getTileHeight() * layer.getHeight());
    viewport = new FitViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
    renderer = new OrthogonalTiledMapRenderer(map);
    name = name.trim();
    if (name.length() == 0)
        name = "noname";
    if (name.length() >= 10){
        name = name.substring(0, 10);
    }
    ClientDetailsMessage clientDetails = new ClientDetailsMessage();
    clientDetails.name = name;
    clientDetails.protocolVersion = Constants.PROTOCOL_VERSION;
    if (isServer) {
        MapLayer collision =  map.
                getLayers().get("collision");
        for(MapObject object: collision.getObjects()) {
            worldManager.createWorldObject(object);
        }
        worldManager.addIncomingEvent(MessageObjectPool.instance.
                eventPool.obtain().set(State.RECEIVED, clientDetails));
    } else {
        client.sendTCP(clientDetails);
    }
    controls = new onScreenControls();
    
}
 
Example #7
Source File: FringeLayer.java    From RuinsOfRevenge with MIT License 5 votes vote down vote up
public FringeLayer(TiledMap map, TiledMapTileLayer fringeLayer) {
	this.map = map;
	this.layer = fringeLayer;
	this.tiles = new ArrayList<>();

	for (int x = 0; x < layer.getWidth(); x++) {
		for (int y = 0; y < layer.getHeight(); y++) {
			TiledMapTileLayer.Cell cell = layer.getCell(x, y);
			if (cell == null) continue;
			String yoffsetStr = cell.getTile().getProperties().get("yoffset", String.class);

			if (yoffsetStr == null || yoffsetStr.isEmpty()) {
				System.out.println("Warning - Fringe tile at [" + x + ", " + y + "] missing property <yoffset>");
			} else {
				try {
					tiles.add(new FringeTile(
							cell,
							x, y,
							Float.parseFloat(yoffsetStr)));
				} catch (NumberFormatException e) {
					System.err.println("Can't parse \"" + yoffsetStr + "\" as float: " + e);
				}
			}
		}
	}
	Collections.sort(tiles);
}
 
Example #8
Source File: GameWorld.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
public TiledMapTileLayer getLayer (Layer layer) {
	return mapUtils.getLayer(layer);
}
 
Example #9
Source File: TiledMapActor.java    From Norii with Apache License 2.0 4 votes vote down vote up
public TiledMapTileLayer getTiledLayer() {
	return tiledLayer;
}
 
Example #10
Source File: FringeLayer.java    From RuinsOfRevenge with MIT License 4 votes vote down vote up
FringeTile(TiledMapTileLayer.Cell cell, int x, int y, float yoffset) {
	this.cell = cell;
	this.x = x;
	this.y = y;
	this.yoffset = yoffset;
}
 
Example #11
Source File: Map.java    From RuinsOfRevenge with MIT License 4 votes vote down vote up
public Map(FileLocation loc, String mapfile, Physics physics) {
	TmxObjectsLoader objs = null;
	try {
		objs = new TmxObjectsLoader(new XmlReader().parse(loc.getFile(mapfile)));
	} catch (IOException e) {
		e.printStackTrace();
	}
	this.map = new TmxMapLoader(loc.getResolver()).load(mapfile);
	this.renderer = new OrthogonalTiledMapRenderer(map, 1f / objs.getTileWidth());

	fringeLayerIndex = computeEntityLayer();
	if (fringeLayerIndex == 999) {
		belowEntities = new int[map.getLayers().getCount()];
		aboveEntities = new int[0];
		fillCounting(0, belowEntities);
		fringeLayer = null;
	} else {
		belowEntities = new int[fringeLayerIndex];
		aboveEntities = new int[map.getLayers().getCount()-fringeLayerIndex-1];
		fillCounting(0, belowEntities);
		fillCounting(fringeLayerIndex+1, aboveEntities);
		MapLayer mapLayer = map.getLayers().get(fringeLayerIndex);
		if (mapLayer instanceof TiledMapTileLayer) {
			fringeLayer = new FringeLayer(map, (TiledMapTileLayer) mapLayer);
		} else {
			fringeLayer = null;
		}
	}

	if (physics != null) {
		for (TmxObjectsLoader.TmxObjectGroup group : objs.getObjectGroups()) {
			for (TmxObjectsLoader.TmxObject obj : group.objects) {
				if (!obj.name.equalsIgnoreCase("spawnpoint")) {
					objs.loadToPhysics(obj, physics);
				} else {
					spawnpoint.set(obj.x / objs.getTileWidth(), obj.y / objs.getTileHeight());
				}
			}
		}
	}
}