#include "Fire.h"
#include "Game.h"
#include "functions.h"

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

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

FireBall::FireBall(int nameNumer)
{
	Entity* mEnt = Game::mOgreSceneManager->createEntity("Fire"+Ogre::StringConverter::toString(nameNumer), "flame.mesh");
	mNode = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNode->attachObject(mEnt);

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

	mPosition = Vector3(0,0,Rand(0.1f,-0.1f));
	mSpeed = Vector3::ZERO;
	mWidth = mHeight = 5;
	onGround = false;
	mNode->setVisible(false);
	mActive = 0;
	mType = 2;
}

void FireBall::Update()
{
	if(mActive) //this bullet is moving
	{
		mActive--;
		if(!mActive)
		{
			mNode->setVisible(false);
		}

		if(onGround)
		{
			mSpeed.y = mSpeed.y*-0.6f;
			mSpeed.x *= 0.6f;
			onGround = false;
		}
		else
		{
			mSpeed.y -= 0.04f;
		}

		mSpeed *= 0.99f;
		mPosition += mSpeed;

		UpdateGroundCollision();
		mNode->setPosition(mPosition);
	}
}

FireArray::FireArray(int aLength)
{
	mBullets = new FireBall*[aLength];

	mLength = aLength;

	//init bullets
	for(int i = 0; i < aLength;i++)
	{
		mBullets[i] = new FireBall(i);
	}
}

void FireArray::Update()
{
	//update bullets
	for (int i = 0; i < mLength; i++)
	{
		mBullets[i]->Update();
	}
}

void FireArray::AddBullet(Vector3 aPos, Vector3 aSpeed)
{
	for (int i = 0; i < mLength; i++)
	{
		if(!mBullets[i]->mActive) //we have an empty spot
		{
			mBullets[i]->mPosition = aPos;
			mBullets[i]->mSpeed = aSpeed;
			mBullets[i]->mActive = 150;
			mBullets[i]->onGround = false;
			mBullets[i]->mNode->setVisible(true);
			break;
		}
	}
}