package coolbot;

import javax.media.opengl.GL;

import world.GameObject;

public class Map extends GameObject {
	
	private Landscape map;
	
	private double xSize;
	private double ySize;
	private double zSize;
	
	public Map(double xSize, double ySize, double zSize) {
		
		map = new Landscape();
		map.generate(120, 10, 3, .3);
		
		this.xSize = xSize;
		this.ySize = ySize;
		this.zSize = zSize;
	}
	
	public void render(GL gl) {
		map.render(gl, xSize, zSize, ySize);
	}
	
	public double getHeightAt(double xPos, double zPos) {
		
		xPos += xSize / 2;
		zPos += zSize / 2;
		
		double squareSize = xSize / 120;
		
		int x = (int) ((double) (xPos) / squareSize);
		int z = (int) ((double) (zPos) / squareSize);
		
		double height = ySize / (map.getHighestPoint() - map.getLowestPoint());
		
		double ul = map.getValueAt(x, z) * height;
		double ur = map.getValueAt(x + 1, z) * height;
		double dl = map.getValueAt(x, z + 1) * height;
		double dr = map.getValueAt(x + 1, z + 1) * height;
		
		double xf = (1 / squareSize) * (xPos % squareSize);
		double zf = (1 / squareSize) * (zPos % squareSize);
		
		ul *= (1 - xf) * (1 - zf);
		ur *= xf * (1 - zf);
		dl *= (1 - xf) * zf;
		dr *= xf * zf;

		return ((ul + ur + dl + dr));
	}
	
	public double getSeaLevel() {
		return map.getSeaLevel(ySize);
	}
	
	public double getMountainLevel() {
		return map.getMountainLevel(ySize);
	}
}