Java tutorial
/* * 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.terrains; import com.badlogic.gdx.graphics.Color; /** * * @author mcory01 */ public class TerrainTile { public TerrainTile(final TileData tileData, final int x, final int y) { this.tileData = tileData; this.x = x; this.y = y; this.elevation = Elevation.findElevation(tileData.getElevation()); final float elevVal = tileData.getElevation(); this.grayscaleElevationColor = new Color(elevVal, elevVal, elevVal, 1.0f); if (tileData.isLandTile()) { this.rawElevationColor = Elevation.WATER.getColor(); this.interpolatedElevationColor = buildInterpolatedElevationColor(elevVal, Elevation.WATER); } else { this.rawElevationColor = elevation.getColor(); this.interpolatedElevationColor = buildInterpolatedElevationColor(elevVal, elevation); } final Latitude latitude = tileData.getLatitude(); this.latitudeColor = buildLatitudeColor(latitude); final float temp = tileData.getTemperature(); this.temperatureColor = new Color(temp, 0.5f * temp, 0.5f * temp, 1); final float rainfall = tileData.getRainfall(); this.rainfallColor = new Color(0.5f * rainfall, 0.5f * rainfall, rainfall, 1); final float minerals = tileData.getMinerals(); this.mineralColor = buildMineralColor(minerals); } public TileData getTileData() { return tileData; } public Color getElevationGrayscale() { return grayscaleElevationColor; } public Color getRawElevationColor() { return rawElevationColor; } public Color getElevationColor() { return interpolatedElevationColor; } public Color getTemperatureColor() { return temperatureColor; } public Color getRainfallColor() { return rainfallColor; } public Color getLatitudeColor() { return latitudeColor; } public Color getMineralColor() { return mineralColor; } public Elevation getElevation() { return elevation; } public int getX() { return x; } public int getY() { return y; } private static Color buildInterpolatedElevationColor(final float elevVal, final Elevation elevation) { final int elevationOrd = elevation.ordinal(); final Elevation[] elevations = Elevation.values(); final int numElevations = elevations.length; final Color prevColor; if (elevationOrd == 0) { prevColor = null; } else { prevColor = elevations[elevationOrd - 1].getColor(); } final Color nextColor; if (elevationOrd == (numElevations - 1)) { nextColor = null; } else { nextColor = elevations[elevationOrd + 1].getColor(); } final Color baseColor = elevation.getColor().cpy(); final Color target; final float diff; if (elevVal < 0.5f) { diff = 1.0f - (2 * elevVal); if (prevColor != null) { target = prevColor; } else { target = Color.BLACK; } } else { diff = elevVal; if (nextColor != null) { target = nextColor; } else { target = Color.WHITE; } } baseColor.lerp(target, diff); return baseColor; } private static Color buildLatitudeColor(final Latitude latitude) { final Color baseColor; switch (latitude) { case POLAR: baseColor = Color.WHITE; break; case TEMPERATE: baseColor = Color.GREEN; break; case TROPICAL: baseColor = Color.GREEN.cpy().sub(0.5f, 0.5f, 0.5f, 0); break; default: throw new UnsupportedOperationException("Unknown latitude: " + latitude); } return baseColor; } private static Color buildMineralColor(final float minerals) { if (minerals < 0.50f) { return Color.CLEAR; } final Color gold = new Color(1.000f, 0.843f, 0.000f, 1.0f); final Color silver = new Color(0.753f, 0.753f, 0.753f, 1.0f); final Color oil = new Color(0.282f, 0.239f, 0.545f, 1.0f); final Color res = new Color(gold.r * minerals, gold.g * minerals, gold.b * minerals, 1.0f); return res; } private final Elevation elevation; private final Color rawElevationColor, interpolatedElevationColor, grayscaleElevationColor; private final Color latitudeColor, rainfallColor, temperatureColor, mineralColor; private final TileData tileData; private final int x, y; }