#include "Enemy.h"
#include "Game.h"
#include "functions.h"
#include "Player.h"

#include <OgreRoot.h>
#include <OgreSceneManager.h>
#include <OgreEntity.h>
#include <OIS/OIS.h>

#include <Windows.h>

using namespace Engine;
using namespace Ogre;
using namespace OIS;

Enemy::Enemy(int nameNumer, int aEnemyType) //0 = ninja,  1 = fireman, 2 = ogre
{
	mEnemyType = aEnemyType;

	int rot = 0;
	mScaleMult = 1;

	if(mEnemyType == 0)
	{
		mEnt = Game::mOgreSceneManager->createEntity("Enemy"+Ogre::StringConverter::toString(nameNumer), "Ninja01.mesh");
		mAccel = Vector3(0.02f,0.02f,0);
		mMaxSpeed = Vector3(3.0f,3.0f,1);
		mWidth = 12;
		mHeight = 8;
		DeathCounter = 0;
		mScaleMult = 1.5f;
		rot = 90;
	}
	if(mEnemyType == 1)
	{
		mEnt = Game::mOgreSceneManager->createEntity("Enemy"+Ogre::StringConverter::toString(nameNumer), "FireFighter.mesh");
		mAccel = Vector3(0.01f,0.02f,0);
		mMaxSpeed = Vector3(3.0f,3.0f,1);
		mWidth = 15;
		mHeight = 15;
		DeathCounter = -15;
		mScaleMult = 0.5f;
	}
	if(mEnemyType == 2)
	{
		mEnt = Game::mOgreSceneManager->createEntity("Enemy"+Ogre::StringConverter::toString(nameNumer), "Ogre.mesh");
		mAccel = Vector3(0.005f,0.02f,0);
		mMaxSpeed = Vector3(3.0f,3.0f,1);
		mWidth = 20;
		mHeight = 23;
		DeathCounter = -40;
		rot = 90;
		mScaleMult = 1.3f;
	}
	mNode = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNode->attachObject(mEnt);

	mNode->rotate(Vector3(1,0,0),Degree(rot));

	mNode->setVisible(false);

	mPosition = Vector3(0,-10000,Rand(0.1f,-0.1f));
	mSpeed = Vector3::ZERO;
	mDir = 1;
	onGround = false;
	mActive = false;
	mType = 4;
}

void Enemy::Update()
{
	if(mActive)
	{
		mSpeed += Vector3(mAccel.x*mDir*Rand(0.8f,1.2f),0,0);

		if(mSpeed.x > mMaxSpeed.x) mSpeed.x = mMaxSpeed.x;
		if(mSpeed.x < -mMaxSpeed.x) mSpeed.x = -mMaxSpeed.x;

		mNode->setScale(mDir*mScaleMult,mScaleMult,mScaleMult);

		if(onGround)
		{
			if(mDir)
			{
				mSpeed.y = mSpeed.y*-0.6f;
				if(mSpeed.y < 0.8f) mSpeed.y = 0.8f*Rand(0.7f,1.3f);
				onGround = false;
			}
			else
			{
				mSpeed.y = 0;
				mSpeed.x *= 0.9f;
			}
		}
		else
		{
			mSpeed.y -= 0.04f;
		}

		mSpeed.x *= 0.95f;
		mPosition += mSpeed;

		UpdateCollision();
		if(UpdateFireCollision() && DeathCounter <= 0)
		{
			DeathCounter++;
		}

		if(CheckBaseCollision())
		{
			Die();
			Game::mPlayer->mHealth -= 5;
		}
		

		if(DeathCounter > 0)
		{
			DeathCounter++;
			if(DeathCounter == 3)
			{
				if(mEnemyType == 0)
					mEnt->setMaterialName("Ninja01Burning");
				else if(mEnemyType == 1)
					mEnt->setMaterialName("FireFighterBurning");
				else if(mEnemyType == 2)
					mEnt->setMaterialName("Ogre");
			}
			if(DeathCounter >= 100)
			{
				Die();
			}
		}

		mNode->setPosition(mPosition);
	}
}

void Enemy::Die()
{
	mActive = false;
	mNode->setVisible(false);

	if(mEnemyType == 0)
	{
		mEnt->setMaterialName("Ninja01");
		DeathCounter = 0;
	}
	else if(mEnemyType == 1)
	{
		mEnt->setMaterialName("FireFighter");
		DeathCounter = -15;
	}
	else if(mEnemyType == 2)
	{
		mEnt->setMaterialName("OgreBurning");
		DeathCounter = -40;
	}
}