Search in sources :

Example 1 with Grid

use of com.almasb.fxgl.core.collection.grid.Grid in project FXGL by AlmasB.

the class MapGenerationSample method plain.

private void plain() {
    // tile size
    int size = 5;
    int W = (getAppWidth() - 200) / size;
    int H = getAppHeight() / size;
    Grid<BiomeMapGenerator.BiomeData> map = new Grid<>(BiomeMapGenerator.BiomeData.class, W, H, new BiomeMapGenerator(W, H, 2.4));
    map.forEach(cell -> {
        Texture texture;
        if (cell.getElevation() < 0.2) {
            // water
            texture = new ColoredTexture(size, size, Color.BLUE);
        } else if (cell.getElevation() < 0.8) {
            // grass
            texture = texture("grass.png", size, size);
        } else {
            // in-land grass / mud?
            texture = texture("grass.png", size, size).desaturate().brighter().desaturate();
        }
        entityBuilder().at(cell.getX() * size, cell.getY() * size).view(texture).buildAndAttach();
    });
}
Also used : BiomeMapGenerator(com.almasb.fxgl.procedural.BiomeMapGenerator) Grid(com.almasb.fxgl.core.collection.grid.Grid) Texture(com.almasb.fxgl.texture.Texture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture)

Example 2 with Grid

use of com.almasb.fxgl.core.collection.grid.Grid in project FXGL by AlmasB.

the class MapGenerationSample method variety.

private void variety() {
    // tile size
    int size = 5;
    int W = (getAppWidth() - 200) / size;
    int H = getAppHeight() / size;
    Grid<HeightMapGenerator.HeightData> map = new Grid<>(HeightMapGenerator.HeightData.class, W, H, new CustomHeightMapGenerator(W, H));
    map.forEach(cell -> {
        Texture texture;
        if (cell.getHeight() < 0.2) {
            // water
            texture = new ColoredTexture(size, size, Color.BLUE);
        } else if (cell.getHeight() < 0.8) {
            // grass
            texture = texture("grass.png", size, size);
        } else {
            // in-land grass / mud?
            texture = texture("grass.png", size, size).desaturate().brighter().desaturate();
        }
        entityBuilder().at(cell.getX() * size, cell.getY() * size).view(texture).buildAndAttach();
    });
}
Also used : Grid(com.almasb.fxgl.core.collection.grid.Grid) HeightMapGenerator(com.almasb.fxgl.procedural.HeightMapGenerator) Texture(com.almasb.fxgl.texture.Texture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture)

Example 3 with Grid

use of com.almasb.fxgl.core.collection.grid.Grid in project FXGL by AlmasB.

the class MapGenerationSample method coloredMap.

private void coloredMap() {
    // tile size
    int size = 1;
    int W = (getAppWidth() + 200) / size;
    int H = (getAppHeight() + 200) / size;
    Grid<HeightMapGenerator.HeightData> map = new Grid<>(HeightMapGenerator.HeightData.class, W, H, new CustomHeightMapGenerator(W, H));
    AnimatedColor water = new AnimatedColor(Color.DARKBLUE, Color.AQUA);
    AnimatedColor land = new AnimatedColor(Color.DARKGREEN, Color.LIGHTGREEN);
    AnimatedColor desert = new AnimatedColor(Color.GREENYELLOW, Color.LIGHTYELLOW);
    AnimatedColor snow = new AnimatedColor(Color.GREEN, Color.WHITESMOKE);
    WritableImage image = new WritableImage(W, H);
    map.forEach(cell -> {
        AnimatedColor anim;
        double adjustedValue;
        if (cell.getHeight() < 0.2) {
            anim = water;
            adjustedValue = map(cell.getHeight(), 0.0, 0.2, 0.0, 1.0);
        } else if (cell.getHeight() < 0.4) {
            anim = land;
            adjustedValue = map(cell.getHeight(), 0.2, 0.4, 0.0, 1.0);
        } else if (cell.getHeight() < 0.9) {
            anim = desert;
            adjustedValue = map(cell.getHeight(), 0.4, 0.9, 0.0, 1.0);
        } else {
            anim = snow;
            adjustedValue = map(cell.getHeight(), 0.9, 1.0, 0.0, 1.0);
        }
        image.getPixelWriter().setColor(cell.getX(), cell.getY(), anim.getValue(adjustedValue, interpolator.EASE_OUT()));
    });
    getGameScene().getViewport().bindToEntity(new Entity(), 0, 0);
    var t = new Texture(image);
    t.setTranslateX(-100);
    t.setTranslateY(-100);
    getGameScene().addGameView(new GameView(t, 0));
}
Also used : AnimatedColor(com.almasb.fxgl.animation.AnimatedColor) Entity(com.almasb.fxgl.entity.Entity) Grid(com.almasb.fxgl.core.collection.grid.Grid) Texture(com.almasb.fxgl.texture.Texture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture) WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.procedural.HeightMapGenerator) GameView(com.almasb.fxgl.app.scene.GameView)

Aggregations

Grid (com.almasb.fxgl.core.collection.grid.Grid)3 ColoredTexture (com.almasb.fxgl.texture.ColoredTexture)3 Texture (com.almasb.fxgl.texture.Texture)3 HeightMapGenerator (com.almasb.fxgl.procedural.HeightMapGenerator)2 AnimatedColor (com.almasb.fxgl.animation.AnimatedColor)1 GameView (com.almasb.fxgl.app.scene.GameView)1 Entity (com.almasb.fxgl.entity.Entity)1 BiomeMapGenerator (com.almasb.fxgl.procedural.BiomeMapGenerator)1 WritableImage (javafx.scene.image.WritableImage)1