/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "core/physic_material.h"
		#include "metafile/nml.h"
		#include "log/log.h"

		using namespace GS::Core;
		using GS::NML::Tag;


//------------------------------------------------------------------------------
bool				PhysicMaterial::FromMetaTag(Tag &tag)
{
	if	(tag.name != "PhysicMaterial")
		__ERR__(__LOG_E__ << "Could not parse physic material, incorrect root tag (" << tag.name << ").\n", false);

	static String _mss("Mass"), _damp("Damping"), _stiff("Stiffness"), _trs("Torsion"),
				   _sfr("StaticFriction"), _dfr("DynamicFriction"), _res("Restitution");

	NMLTagForeach(pt, tag)
	{
				if	(pt->name == _mss)
				mass = pt->GetReal();
		else	if	(pt->name == _sfr)
				static_friction = pt->GetReal();
		else	if	(pt->name == _dfr)
				dynamic_friction = pt->GetReal();
		else	if	(pt->name == _res)
				restitution = pt->GetReal();
		else	if	(pt->name == _damp)
				sb_damping = pt->GetReal();
		else	if	(pt->name == _stiff)
				sb_stiffness = pt->GetReal();
		else	if	(pt->name == _trs)
				sb_torsion = pt->GetBool();
	}
	return true;
}
Tag					*PhysicMaterial::AsMetaTag() const
{
	Tag *m = new Tag("PhysicMaterial");
	if	(!m)
		__ERR__(__LOG_E__ << "Could not serialize physic material. Failed to create root tag.\n", NULL);

	if	(mass != 1.f)
		m->AddChild("Mass", mass);
	m->AddChild("StaticFriction", static_friction);
	m->AddChild("DynamicFriction", dynamic_friction);
	m->AddChild("Restitution", restitution);
	if	(sb_damping != 0.99f)
		m->AddChild("Damping", sb_damping);
	if	(sb_stiffness != 1)
		m->AddChild("Stiffness", sb_stiffness);
	if	(sb_torsion != 1)
		m->AddChild("Torsion", sb_torsion);
	return m;
}
//------------------------------------------------------------------------------
