public LMap2D( String datafile, String mapFile, int x, int y, int width, int height, int rowTileWidth, int colTileHeight, int tileSize) throws IOException { super(x, y, width, height); int[][] maps = TileMapConfig.loadJustArray(datafile); this.blockSize = tileSize; if (background == null) { LMap2D.background = TextureUtils.createTexture(1, 1, LColor.black); } this.fied2d = new Field2D(maps); this.texture = new SpriteBatchSheet(mapFile, rowTileWidth, colTileHeight, 0); this.batch = new SpriteBatch(); this.setElastic(true); this.setLocked(true); this.setLayer(100); if (width == 0 || height == 0) { setWidth(fied2d.getHeight() * blockSize); setHeight(fied2d.getWidth() * blockSize); } }
public void draw(SpriteBatch batch, int x, int y) { int windowX = x + getWidth(); int windowY = y + getHeight(); int tempX; int tempY; int minCol = -(x / blockSize); int maxCol = (minCol + ((windowX + (blockSize * 2)) / blockSize)); if (minCol < 0) { minCol = 0; } if (maxCol < 0) { maxCol = 0; } if (maxCol > fied2d.getHeight()) { maxCol = fied2d.getHeight(); } if (minCol > maxCol) { minCol = maxCol; } int minRow = -(y / blockSize); int maxRow = (minRow + ((windowY + (blockSize * 2)) / blockSize)); if (minRow < 0) { minRow = 0; } if (maxRow < 0) { maxRow = 0; } if (maxRow > fied2d.getWidth()) { maxRow = fied2d.getWidth(); } if (minRow > maxRow) { minRow = maxRow; } batch.draw(background, x, y, getWidth(), getHeight()); for (int row = minRow; row < maxRow; row++) { for (int col = minCol; col < maxCol; col++) { tempX = (x + (col * blockSize)); tempY = (y + (row * blockSize)); int id = fied2d.getType(col, row) - 1; if (id != -1) { texture.animate(id); texture.update(tempX, tempY, blockSize, blockSize); texture.draw(batch, windowX, windowY); } if (grid) { batch.drawRect(tempX, tempY, blockSize, blockSize); } } } }
/** * 设定Layer对应的二维数组地图 * * @param map */ public void setField2D(Field2D field) { if (isClose) { return; } if (field == null) { return; } if (tmpField != null) { if ((field.getMap().length == tmpField.getMap().length) && (field.getTileWidth() == tmpField.getTileWidth()) && (field.getTileHeight() == tmpField.getTileHeight())) { tmpField.set(field.getMap(), field.getTileWidth(), field.getTileHeight()); } } else { tmpField = field; } }
protected void processTouchPressed() { if (!input.isMoving()) { int posx = 0; int posy = 0; if (getContainer() == null) { posx = (int) ((getX() + input.getTouchX()) / blockSize); posy = (int) ((getY() + input.getTouchY()) / blockSize); } else { posx = (int) ((getContainer().getX() + getX() + input.getTouchX()) / blockSize); posy = (int) ((getContainer().getY() + getY() + input.getTouchY()) / blockSize); } mapId = fied2d.getType(posx, posy); super.processTouchPressed(); } }
public int getMapPixelHeight() { return (int) blockSize * fied2d.getWidth(); }