#include <iostream>
#include <fstream>

#include "Model.h"
#include "Physics.h"

#include "EntityFactory.h"

#include <assert.h>

namespace asteroids {

static Physics* PhysicsInstance = NULL;

/*
 * Inherited Module functions
 */
int Physics::OnInit (int argc, char* argv[]) {
	Engine::PhysicsBase::OnInit (argc, argv);
	
	Engine::LogMessage ("Physics Initialization!");

	PhysicsInstance = this;

	mWorldWidth = 16;
	mWorldHeight = 16;

	return 0;
}

int Physics::Simulate (float msec, Engine::ModelBase *model) {
	int result = Engine::PhysicsBase::Simulate (msec, model);

	Engine::EntityPhysicState* entity = NULL;
	std::map<unsigned int, Engine::EntityPhysicState*>::iterator entity_iter;

	for (entity_iter = mEntities.begin ();
			entity_iter != mEntities.end();
			entity_iter++) {
		entity = entity_iter->second;

		if (entity->mPosition[0] > mWorldWidth * 0.5)
			entity->mPosition[0] -= mWorldWidth;
		if (entity->mPosition[0] < - mWorldWidth * 0.5)
			entity->mPosition[0] += mWorldWidth;

		if (entity->mPosition[2] > mWorldHeight * 0.5)
			entity->mPosition[2] -= mWorldHeight;
		if (entity->mPosition[2] < - mWorldHeight * 0.5)
			entity->mPosition[2] += mWorldHeight;
	}

	return result;
}

}


