private int addBlock(int[][] layer, int layerNum, int blockRow, int blockCol, boolean blended) { int tile; AtlasRegion region; cache.beginCache(); int firstCol = blockCol * tilesPerBlockX; int firstRow = blockRow * tilesPerBlockY; int lastCol = firstCol + tilesPerBlockX; int lastRow = firstRow + tilesPerBlockY; int row, col; float x, y; for (row = firstRow; row < lastRow && row < layer.length; row++) { for (col = firstCol; col < lastCol && col < layer[row].length; col++) { tile = layer[row][col]; if (tile != 0) { if (blended == blendedTiles.contains(tile)) { region = atlas.getRegion(tile); if (region != null) { y = (layer.length - row) * tileHeight - (region.packedHeight + region.offsetY); x = col * tileWidth + region.offsetX; cache.add(region, x, y); } } } } } return cache.endCache(); }
/** * Carga los layers desde el arrayList * * @param maplayers un conjunto de layers que se dibujaran en el mismo orden que esten en el * ArrayList */ public void Cargar(ArrayList<TiledLayer> maplayers) { texture = new Texture(Gdx.files.internal("gfx/Tiles/ambiente.png")); for (int i = 0; i < LAYERS; i++) { caches[i] = new SpriteCache(); cache = caches[i]; cache.beginCache(); for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { int textureX = maplayers.get(i).tiles[y][x] % 16; int textureY = maplayers.get(i).tiles[y][x] / 16; if (textureX != 0 || textureY != 0) { int tileX = x * TILE_SIZE; int tileY = ((HEIGHT - y) * TILE_SIZE); cache.add( texture, tileX, tileY, textureX * (TILE_SIZE), textureY * (TILE_SIZE), TILE_SIZE, TILE_SIZE); } } } layers[i] = cache.endCache(); } }
private void initBackground() { backCache = new SpriteCache(); backCache.beginCache(); TextureRegion backRegion = Globals.atlas.findRegion("back"); float w = backRegion.getRegionWidth() * 2; float h = backRegion.getRegionWidth() * 2; for (int y = 0; y < Globals.PSCR_H; y += h) for (int x = 0; x < Globals.PSCR_W; x += w) backCache.add(backRegion, x, y, w, h); backCacheId = backCache.endCache(); }
/** * Constructs a map from a TMX file * * @param fileHandle A {@link FileHandle} to a .tmx file * @param loadTilesets True if the tileset images should be loaded * @throws IOException Thrown if the map file could not be parsed */ public TiledMap(FileHandle fileHandle, boolean loadTilesets) throws IOException { this(); this.loadTilesets = loadTilesets; this.fileHandle = fileHandle; TiledParser parser = new TiledParser(); parser.addListener(this); parser.parse(fileHandle); if (loadTilesets) { layerCache = new SpriteCache(getWidth() * getHeight() * tileLayers.size(), true); for (int layer = 0; layer < tileLayers.size(); layer++) { layerCache.beginCache(); for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth(); x++) { int tileId = tileLayers.get(layer).getTileId(x, y); if (tileId > 0) { int tileRenderX = x * getTileWidth(); int tileRenderY = y * getTileHeight(); for (int i = 0; i < tilesets.size(); i++) { Tileset tileset = tilesets.get(i); if (tileset.contains(tileId)) { layerCache.add(tileset.getTile(tileId).getTileImage(), tileRenderX, tileRenderY); break; } } } } } int layerCacheId = layerCache.endCache(); layerCacheIds.put(tileLayers.get(layer), layerCacheId); } } }
private int addBlock(int[][] layer, int blockRow, int blockCol, boolean blended) { cache.beginCache(); int firstCol = blockCol * tilesPerBlockX; int firstRow = blockRow * tilesPerBlockY; int lastCol = firstCol + tilesPerBlockX; int lastRow = firstRow + tilesPerBlockY; float offsetX = ((tileWidth - unitsPerTileX) / 2); float offsetY = ((tileHeight - unitsPerTileY) / 2); for (int row = firstRow; row < lastRow && row < layer.length; row++) { for (int col = firstCol; col < lastCol && col < layer[row].length; col++) { int tile = layer[row][col]; boolean flipX = ((tile & FLAG_FLIP_X) != 0); boolean flipY = ((tile & FLAG_FLIP_Y) != 0); boolean rotate = ((tile & FLAG_ROTATE) != 0); tile = tile & ~MASK_CLEAR; if (tile != 0) { if (blended == blendedTiles.contains(tile)) { TextureRegion reg = atlas.getRegion(tile); if (reg != null) { float x = col * unitsPerTileX - offsetX; float y = (layer.length - row - 1) * unitsPerTileY - offsetY; float width = reg.getRegionWidth(); float height = reg.getRegionHeight(); float originX = width * 0.5f; float originY = height * 0.5f; float scaleX = unitsPerTileX / tileWidth; float scaleY = unitsPerTileY / tileHeight; float rotation = 0; int sourceX = reg.getRegionX(); int sourceY = reg.getRegionY(); int sourceWidth = reg.getRegionWidth(); int sourceHeight = reg.getRegionHeight(); if (rotate) { if (flipX && flipY) { rotation = -90; sourceX += sourceWidth; sourceWidth = -sourceWidth; } else if (flipX && !flipY) { rotation = -90; } else if (flipY && !flipX) { rotation = +90; } else if (!flipY && !flipX) { rotation = -90; sourceY += sourceHeight; sourceHeight = -sourceHeight; } } else { if (flipX) { sourceX += sourceWidth; sourceWidth = -sourceWidth; } if (flipY) { sourceY += sourceHeight; sourceHeight = -sourceHeight; } } cache.add( reg.getTexture(), x, y, originX, originY, width, height, scaleX, scaleY, rotation, sourceX, sourceY, sourceWidth, sourceHeight, false, false); } } } } } return cache.endCache(); }