com.github.skittishSloth.openSkies.maps.VoronoiTestScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.github.skittishSloth.openSkies.maps.VoronoiTestScreen.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.skittishSloth.openSkies.maps;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Polygon;
import com.github.skittishSloth.openSkies.maps.generation.perlin.PerlinNoiseGenerator;
import com.github.skittishSloth.openSkies.maps.generation.voronoi.BruteForceVoronoiMap;
import com.github.skittishSloth.openSkies.maps.generation.voronoi.Cell;
import com.github.skittishSloth.openSkies.maps.generation.voronoi.Site;
import com.github.skittishSloth.openSkies.maps.generation.voronoi.VoronoiMap;
import com.github.skittishSloth.openSkies.maps.generation.voronoi.VoronoiTile;
import com.github.skittishSloth.openSkies.maps.terrains.Elevation;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 * @author mcory01
 */
public class VoronoiTestScreen implements Screen {

    public VoronoiTestScreen() {
        final int tileSize = 1;
        final int width = Gdx.graphics.getWidth() / tileSize;
        final int height = Gdx.graphics.getHeight() / tileSize;

        final int numSites = 2000;

        map = VoronoiMap.generateRandom(numSites, width, height);
        final Collection<Site> sites = map.getSites();
        bfMap = new BruteForceVoronoiMap(width, height, tileSize, sites);

        final float[][] landProbability = PerlinNoiseGenerator.generatePerlinNoise(width, height, 2);

        final Pixmap pm = new Pixmap(width, height, Pixmap.Format.RGBA8888);
        pm.setColor(Color.BLACK);
        pm.fill();

        final Map<Site, Cell> cells = bfMap.buildCells(landProbability);
        for (final Site site : sites) {
            final Cell cell = cells.get(site);
            if (cell == null) {
                System.err.println("Cell was null for site " + site.getX() + ", " + site.getY());
                continue;
            }

            final boolean landCell = cell.isLand();
            final Color cellColor;
            if (landCell) {
                cellColor = Elevation.PLAINS.getColor();
            } else {
                cellColor = Elevation.WATER.getColor();
            }

            pm.setColor(cellColor);
            final List<VoronoiTile> tiles = cell.getTiles();
            for (final VoronoiTile tile : tiles) {
                final int x = tile.getX();
                final int y = tile.getY();
                pm.drawPixel(x, y);
            }
        }

        pm.setColor(Color.BLACK);
        for (final Site site : sites) {
            final Cell cell = cells.get(site);
            if (cell == null) {
                System.err.println("Cell was null for site " + site.getX() + ", " + site.getY());
                continue;
            }

            final Polygon polygon = cell.calculateConvexHull();
            final float[] vertices = polygon.getVertices();
            final boolean hasCornerPoint = cell.hasCornerPoint();

            final List<Point> verticesPoints = new ArrayList<Point>();
            for (int i = 0; i < vertices.length - 1;) {
                final int x = Math.round(vertices[i++]);
                final int y = Math.round(vertices[i++]);
                if ((x == 0) && (y == 0)) {
                    if (!hasCornerPoint) {
                        continue;
                    }
                }
                final Point p = new Point(x, y);
                verticesPoints.add(p);
            }

            final int numPoints = verticesPoints.size();

            for (int i = 0; i < numPoints - 1;) {
                final Point p1 = verticesPoints.get(i++);
                final Point p2 = verticesPoints.get(i++);
                pm.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }

        pm.setColor(Color.WHITE);
        for (final Site site : sites) {
            final int x = site.getX();
            final int y = site.getY();
            pm.drawPixel(x, y);
        }

        mapTexture = new Texture(pm);
        pm.dispose();

        batch = new SpriteBatch();
    }

    @Override
    public void render(final float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(mapTexture, 0, 0);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        mapTexture.dispose();
        batch.dispose();
    }

    private final VoronoiMap map;
    private final BruteForceVoronoiMap bfMap;
    private final Texture mapTexture;
    private final SpriteBatch batch;
}