/* Copyright (c) 2014 "Naftoreiclag" https://github.com/Naftoreiclag
 *
 * Distributed under the BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
 * See accompanying file LICENSE
 */

package naftoreiclag.village;

import naftoreiclag.village.environment.Hills;

import org.lwjgl.input.Keyboard;

public class Player
{
	public float x;
	public float y = 2.5f;
	public float z;
	
	public float direction;
	
	float spd = 0.005f;
	
	public Hills map;
	
	public void input(long delta)
	{
		if(Keyboard.isKeyDown(Keyboard.KEY_UP) || Keyboard.isKeyDown(Keyboard.KEY_W))
		{
			direction = (float) directTo(direction, 180f, 0.05f);
			z -= spd * delta;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) || Keyboard.isKeyDown(Keyboard.KEY_S))
		{
			direction = (float) directTo(direction, 0f, 0.05f);
			z += spd * delta;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_A))
		{
			direction = (float) directTo(direction, 270f, 0.05f);
			x -= spd * delta;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D))
		{
			direction = (float) directTo(direction, 90f, 0.05f);
			x += spd * delta;
		}
		
		y = getFoo(x, z);
	}

	public double directTo(double aaa, double bbb, float amnt)
	{
		// Convert to Vectors
		double ax = Math.cos(Math.toRadians(aaa));
		double ay = Math.sin(Math.toRadians(aaa));
		double bx = Math.cos(Math.toRadians(bbb));
		double by = Math.sin(Math.toRadians(bbb));

		double cx = (ax + bx) / 2d;
		double cy = (ay + by) / 2d;

		double midDir = Math.toDegrees(Math.atan2(cy, cx));

		return midDir;
	}

	
	private float getFoo(float x, float z)
	{
		if(x >= 0 && z >= 0 && x < Hills.size && z < Hills.size)
		{
			
			// M  y  D
			//
			// h  P  s
			//
			// B  g  N
			
			x /= Hills.horzu;
			z /= Hills.horzu;
			
			int xi = (int) x;
			int zi = (int) z;
			
			float m = map.map[xi][zi];
			float d = map.map[xi + 1][zi];
			float b = map.map[xi][zi + 1];
			float n = map.map[xi+1][zi+1];
			
			float xr = x - xi;
			float zr = z - zi;
			
			float md = d - m;
			float bn = n - b;
			
			float y = m + (md * xr);
			float g = b + (bn * xr);
			
			float yg = g - y;
			
			float p = y + (yg * zr);
			
			return p * Hills.vertu;
		}
		else
		{
			return 0;
		}
	}
}

@@ -230,56 +230,8 @@
 e;%0A%0A
-import naftoreiclag.village.environment.Hills;%0A%0A
 impo
@@ -259,16 +259,16 @@
 yboard;%0A
+
 %0Apublic 
@@ -303,32 +303,8 @@
  x;%0A
-%09public float y = 2.5f;%0A
 %09pub
@@ -372,29 +372,8 @@
 ;%0A%09%0A
-%09public Hills map;%0A%09%0A
 %09pub
@@ -1074,31 +1074,8 @@
 %09%09%7D%0A
-%09%09%0A%09%09y = getFoo(x, z);%0A
 %09%7D%0A%0A
@@ -1458,16 +1458,16 @@
  cx));%0A%0A
+
 %09%09return
@@ -1481,679 +1481,7 @@
 ;%0A%09%7D
-%0A%0A%09%0A%09private float getFoo(float x, float z)%0A%09%7B%0A%09%09if(x %3E= 0 && z %3E= 0 && x %3C Hills.size && z %3C Hills.size)%0A%09%09%7B%0A%09%09%09%0A%09%09%09// M  y  D%0A%09%09%09//%0A%09%09%09// h  P  s%0A%09%09%09//%0A%09%09%09// B  g  N%0A%09%09%09%0A%09%09%09x /= Hills.horzu;%0A%09%09%09z /= Hills.horzu;%0A%09%09%09%0A%09%09%09int xi = (int) x;%0A%09%09%09int zi = (int) z;%0A%09%09%09%0A%09%09%09float m = map.map%5Bxi%5D%5Bzi%5D;%0A%09%09%09float d = map.map%5Bxi + 1%5D%5Bzi%5D;%0A%09%09%09float b = map.map%5Bxi%5D%5Bzi + 1%5D;%0A%09%09%09float n = map.map%5Bxi+1%5D%5Bzi+1%5D;%0A%09%09%09%0A%09%09%09float xr = x - xi;%0A%09%09%09float zr = z - zi;%0A%09%09%09%0A%09%09%09float md = d - m;%0A%09%09%09float bn = n - b;%0A%09%09%09%0A%09%09%09float y = m + (md * xr);%0A%09%09%09float g = b + (bn * xr);%0A%09%09%09%0A%09%09%09float yg = g - y;%0A%09%09%09%0A%09%09%09float p = y + (yg * zr);%0A%09%09%09%0A%09%09%09return p * Hills.vertu;%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09return 0;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

remove hill-height finder