Search in sources :

Example 31 with Tile

use of org.pepsoft.worldpainter.Tile in project WorldPainter by Captain-Chaos.

the class NibbleLayerPaint method remove.

@Override
public void remove(Dimension dimension, int centreX, int centreY, float dynamicLevel) {
    if (brush.getRadius() == 0) {
        // Special case: if the radius is 0, assume that the user wants to remove complete pixels instead of trying
        // to apply the brush
        removePixel(dimension, centreX, centreY);
        return;
    }
    final int effectiveRadius = brush.getEffectiveRadius();
    final int x1 = centreX - effectiveRadius, y1 = centreY - effectiveRadius, x2 = centreX + effectiveRadius, y2 = centreY + effectiveRadius;
    final int tileX1 = x1 >> TILE_SIZE_BITS, tileY1 = y1 >> TILE_SIZE_BITS, tileX2 = x2 >> TILE_SIZE_BITS, tileY2 = y2 >> TILE_SIZE_BITS;
    if ((tileX1 == tileX2) && (tileY1 == tileY2)) {
        // The bounding box of the brush is entirely on one tile; optimize by painting directly to the tile
        final Tile tile = dimension.getTileForEditing(tileX1, tileY1);
        if (tile == null) {
            return;
        }
        final int x1InTile = x1 & TILE_SIZE_MASK, y1InTile = y1 & TILE_SIZE_MASK, x2InTile = x2 & TILE_SIZE_MASK, y2InTile = y2 & TILE_SIZE_MASK;
        final int tileXInWorld = tileX1 << TILE_SIZE_BITS, tileYInWorld = tileY1 << TILE_SIZE_BITS;
        for (int y = y1InTile; y <= y2InTile; y++) {
            for (int x = x1InTile; x <= x2InTile; x++) {
                final int currentValue = tile.getLayerValue(layer, x, y);
                final float strength = dynamicLevel * getFullStrength(centreX, centreY, tileXInWorld + x, tileYInWorld + y);
                if (strength != 0f) {
                    int targetValue = 14 - (int) (strength * 14 + 0.5f);
                    if (targetValue < currentValue) {
                        tile.setLayerValue(layer, x, y, targetValue);
                    }
                }
            }
        }
    } else {
        // The bounding box of the brush straddles more than one tile; paint to the dimension
        for (int y = y1; y <= y2; y++) {
            for (int x = x1; x <= x2; x++) {
                final int currentValue = dimension.getLayerValueAt(layer, x, y);
                final float strength = dynamicLevel * getFullStrength(centreX, centreY, x, y);
                if (strength != 0f) {
                    int targetValue = 14 - (int) (strength * 14 + 0.f);
                    if (targetValue < currentValue) {
                        dimension.setLayerValueAt(layer, x, y, targetValue);
                    }
                }
            }
        }
    }
}
Also used : Tile(org.pepsoft.worldpainter.Tile)

Aggregations

Tile (org.pepsoft.worldpainter.Tile)31 Dimension (org.pepsoft.worldpainter.Dimension)6 CombinedLayer (org.pepsoft.worldpainter.layers.CombinedLayer)5 Point (java.awt.Point)4 FileOutputStream (java.io.FileOutputStream)4 Terrain (org.pepsoft.worldpainter.Terrain)4 World2 (org.pepsoft.worldpainter.World2)4 Rectangle (java.awt.Rectangle)3 BufferedImage (java.awt.image.BufferedImage)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 SubProgressReceiver (org.pepsoft.util.SubProgressReceiver)3 Layer (org.pepsoft.worldpainter.layers.Layer)3 Graphics2D (java.awt.Graphics2D)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PrintWriter (java.io.PrintWriter)2 java.util (java.util)2 List (java.util.List)2