#include "Entity.h"
#include "globals.h"
#include <math.h>

int Entity::entityCount = 0;

Entity::Entity(scene::ISceneManager* smgr, core::vector2df pos)
	: pos(pos), oldPos(pos), repulsiveForce(100), attracts(false),
	radius(40), canLandOn(false), id(entityCount++)
{
	sceneNode = smgr->addCubeSceneNode(100, NULL, -1, core::vector3df(pos.X, pos.Y, 0));
	sceneNode->setMaterialFlag(video::EMF_COLOR_MATERIAL, true);
	sceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
	sceneNode->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
}

Entity::~Entity()
{
	sceneNode->remove();
}

core::vector2df Entity::calcNewPos(float deltaTime)
{
	// Verlet integration
	core::vector2df newPos = 2.0f*pos - oldPos + accel*deltaTime*deltaTime;
	oldPos = pos;
	return newPos;
}

void Entity::setPos(core::vector2df newPos)
{
	// Apply update
	pos = newPos;
	sceneNode->setPosition(core::vector3df(pos.X, pos.Y, 0));

	// Rotate to match heading
	core::vector2df rot = pos - oldPos;
	rot.normalize();
	sceneNode->setRotation(core::vector3df(0, 0, 90.0f + atan2(rot.Y, rot.X) * 180.0f / PI));
}
