#include "Engine.h"

#include "ModelBase.h"

#include "AsteroidEntity.h"
#include "Controller.h"
#include "Model.h"
#include "EntityBase.h"

#include "coll2d.h"

namespace asteroids {

bool AsteroidEntity::CollisionEvent (Engine::EntityBase* entity_state, vector3d point, vector3d normal) {
	GameEntityType other_type = (GameEntityType) entity_state->mType;

	if (other_type == GameEntityTypeRocket) {
		// First remove the rocket
		Engine::KillEntity (entity_state->mId);

		Engine::LogMessage ("You killed an Asteroid!");

		int i;
		for (i = 0; i < mSubAsteroidsCount; i++) {
			AsteroidEntity *asteroid = (AsteroidEntity*) Engine::CreateEntity (GameEntityTypeAsteroid);
			vector3d position (rand()/float(RAND_MAX) * 1. - 0.5, 0., rand()/float(RAND_MAX) * 1. - 0.5);
			asteroid->mSubAsteroidsCount = 0;
			asteroid->mPhysicState->mRadius = 2. * mPhysicState->mRadius / mSubAsteroidsCount;
			static_cast<coll2d::Sphere*>(asteroid->mPhysicState->mShape)->setRadius (asteroid->mPhysicState->mRadius);
			asteroid->mPhysicState->mPosition = mPhysicState->mPosition + position;
			asteroid->mPhysicState->mVelocity = position;
			asteroid->mPhysicState->mAngleVelocity = mPhysicState->mAngleVelocity + (rand()/float(RAND_MAX) * 2. - 1) * mPhysicState->mAngleVelocity;
		}
		
		Engine::KillEntity (mId);

		return true;
	} else if (other_type == GameEntityTypeRocket) {
		return false;
	}

	return false;
}

}
