#include <cmath>
#include "world_height_map.h"
void WorldHeightMap::_get_property_list(List<PropertyInfo> *p_list) const
{
	p_list->push_back(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"));
	p_list->push_back(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"));
}
bool WorldHeightMap::_get(const StringName &p_name, Variant &r_ret) const
{
	if (p_name == "noise") {
		r_ret = noise;
		return true;
	} else if (p_name == "curve") {
		r_ret = curve;
		return true;
	}

	const String pv = p_name.operator String();
	return false;
}

bool WorldHeightMap::_set(const StringName &p_name, const Variant &p_value)
{
	bool update = false;
	if (p_name == "noise") {
		noise = p_value;
		update = true;
	} else if (p_name == "curve") {
		curve = p_value;
		update = true;
	}
	if (update) {
		update_all();
		_change_notify();
	}

	return update;
}

float WorldHeightMap::get_base_steepness(float x, float y)
{
	float tstep = 10.0f;
	float xp = ceil(x / tstep) * tstep;
	float xm = floor(x / tstep) * tstep;
	float yp = ceil(y / tstep) * tstep;
	float ym = floor(y / tstep) * tstep;
	float hp = curve->interpolate_baked((noise->get_noise_2d(xp, yp) + 1.0f) * 0.5f);
	float hm = curve->interpolate_baked((noise->get_noise_2d(xp, yp) + 1.0f) * 0.5f);
	Vector3 d = Vector3(xp, hp, yp) - Vector3(xm, hm, ym);
	return fabs(cosf(d.angle_to(Vector3(0.0f, 1.0f, 0.0f))));
}
float WorldHeightMap::get_surface_height(float x, float y)
{
	float s = get_base_steepness(x, y);
	if (s > 0.5f) {
		float n = (noise->get_noise_2d(x, y) + 1.0f) * 0.5f;
		float d = curve->interpolate_baked(n);
		return d;
	} else {
		float tstep = 10.0f;
		float xp = ceil(x / tstep) * tstep;
		float xm = floor(x / tstep) * tstep;
		float yp = ceil(y / tstep) * tstep;
		float ym = floor(y / tstep) * tstep;
		float hp = curve->interpolate_baked((noise->get_noise_2d(xp, yp) + 1.0f) * 0.5f);
		float hm = curve->interpolate_baked((noise->get_noise_2d(xp, yp) + 1.0f) * 0.5f);
		float d = Vector2(xm, ym).distance_to(Vector2(x, y)) / Vector2(xm, ym).distance_to(Vector2(xp, yp));
		return hp * d + hm * (1.0f - d);
	}
}
float WorldHeightMap::get_base_height(float x, float y)
{
	float n = noise->get_noise_2d(x, y); 
	return n;
}

void WorldHeightMap::update_all()
{
}

WorldHeightMap::WorldHeightMap()
{
}

WorldHeightMap::~WorldHeightMap()
{
}

void WorldHeightMap::_bind_methods()
{
	ClassDB::bind_method(D_METHOD("get_surface_height", "x", "y"), &WorldHeightMap::get_surface_height);
	ClassDB::bind_method(D_METHOD("draw_height_map", "draw", "draw_rect", "world_rect"), &WorldHeightMap::draw_height_map);
}
void WorldHeightMap::draw_height_map(Node *draw, const Rect2 &draw_rect, const Rect2 &world_rect)
{
}

