Search in sources :

Example 1 with HeightMapGenerator

use of com.almasb.fxgl.algorithm.procedural.HeightMapGenerator in project FXGL by AlmasB.

the class MapGenerationSample method variety.

private void variety() {
    // tile size
    int size = 5;
    int W = getWidth() / size;
    int H = getHeight() / size;
    HeightMapGenerator gen = new HeightMapGenerator(2.4);
    gen.setGenFunction((nx, ny, freq) -> {
        double e = FXGLMath.noise2D(nx, ny) + 0.5 * FXGLMath.noise2D(2 * nx, 2 * ny) + 0.25 * FXGLMath.noise2D(9 * nx, 9 * ny);
        double noiseValue = Math.pow(e, 3.3);
        if (noiseValue < 0) {
            noiseValue = 0;
        }
        if (noiseValue > 1) {
            noiseValue = 1;
        }
        return noiseValue;
    });
    Grid<Double> map = gen.generate(W, H);
    map.forEach((data, x, y) -> {
        Texture texture;
        if (data < 0.2) {
            // water
            texture = new Texture(new WritableImage(size, size)).replaceColor(Color.TRANSPARENT, Color.BLUE);
        } else if (data < 0.8) {
            // grass
            texture = DSLKt.texture("grass.png", size, size);
        } else {
            // in-land grass / mud?
            texture = DSLKt.texture("grass.png", size, size).desaturate().brighter().desaturate();
        }
        Entities.builder().at(x * size, y * size).viewFromNode(texture).buildAndAttach();
    });
}
Also used : WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.algorithm.procedural.HeightMapGenerator) Texture(com.almasb.fxgl.texture.Texture)

Example 2 with HeightMapGenerator

use of com.almasb.fxgl.algorithm.procedural.HeightMapGenerator in project FXGL by AlmasB.

the class MapGenerationSample method coloredMap.

private void coloredMap() {
    // tile size
    int size = 1;
    int W = getWidth() / size;
    int H = getHeight() / size;
    HeightMapGenerator gen = new HeightMapGenerator(2.4);
    gen.setGenFunction((nx, ny, freq) -> {
        double e = FXGLMath.noise2D(nx, ny) + 0.5 * FXGLMath.noise2D(2 * nx, 2 * ny) + 0.25 * FXGLMath.noise2D(9 * nx, 9 * ny);
        double noiseValue = Math.pow(e, 3.3);
        if (noiseValue < 0) {
            noiseValue = 0;
        }
        if (noiseValue > 1) {
            noiseValue = 1;
        }
        return noiseValue;
    });
    Grid<Double> map = gen.generate(W, H);
    // use different interpolators if needed
    AnimatedColor water = new AnimatedColor(Color.DARKBLUE, Color.AQUA, Interpolators.LINEAR.EASE_OUT());
    AnimatedColor land = new AnimatedColor(Color.DARKGREEN, Color.LIGHTGREEN, Interpolators.LINEAR.EASE_OUT());
    AnimatedColor desert = new AnimatedColor(Color.YELLOW, Color.LIGHTYELLOW);
    AnimatedColor snow = new AnimatedColor(Color.BROWN, Color.WHITESMOKE);
    WritableImage image = new WritableImage(W, H);
    map.forEach((data, x, y) -> {
        AnimatedColor anim;
        if (data < 0.2) {
            anim = water;
        } else if (data < 0.4) {
            anim = land;
        } else if (data < 0.8) {
            anim = desert;
        } else {
            anim = snow;
        }
        image.getPixelWriter().setColor(x, y, anim.getValue(data));
    });
    getGameScene().addUINode(new Texture(image));
}
Also used : AnimatedColor(com.almasb.fxgl.animation.AnimatedColor) WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.algorithm.procedural.HeightMapGenerator) Texture(com.almasb.fxgl.texture.Texture)

Aggregations

HeightMapGenerator (com.almasb.fxgl.algorithm.procedural.HeightMapGenerator)2 Texture (com.almasb.fxgl.texture.Texture)2 WritableImage (javafx.scene.image.WritableImage)2 AnimatedColor (com.almasb.fxgl.animation.AnimatedColor)1