Search in sources :

Example 1 with AnimatedColor

use of com.almasb.fxgl.animation.AnimatedColor 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)1 AnimatedColor (com.almasb.fxgl.animation.AnimatedColor)1 Texture (com.almasb.fxgl.texture.Texture)1 WritableImage (javafx.scene.image.WritableImage)1