#include "DefenseWall.h"
#include "Game.h"
#include "functions.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;

DefenseWall::DefenseWall(int nameNumer)
{
	mEnt = Game::mOgreSceneManager->createEntity("DefenseWall"+Ogre::StringConverter::toString(nameNumer), "DoubleBlockWall.mesh");
	mNode = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNode->attachObject(mEnt);

	mNode->setVisible(false);
	mActive = 0;

	mPosition = Vector3(0,0,0);
	mSpeed = Vector3::ZERO;

	mWidth = 12;
	mHeight = 24;
	onGround = false;

	mHealth = 0;
	mBurningCounter = 0;
	mType = 1;
}

void DefenseWall::Update()
{
	if(mActive == 2)
	{
		if(!onGround)
		{
			mSpeed.y -= 0.04f;
		}
		else
		{
			mSpeed.y = 0;
			mSpeed.x = 0;
		}

		mSpeed *= 0.97f;

		onGround = false;

		mPosition += mSpeed;
		UpdateGroundCollision();

		if(mBurningCounter > 0)
		{
			mBurningCounter++;
			mHealth -= 51.0f/2000.0f;
			if(mBurningCounter >= 1500)
			{
				mBurningCounter = -1;
				mEnt->setMaterialName("DieMauerBurned");
			}
		}

		if(mHealth <= 0)
		{
			mHealth = 0;
			mEnt->setVisible(false);
			mActive = 0;
			mBurningCounter = 0;
		}
	}
	mNode->setPosition(mPosition);
}

void DefenseWall::DoDamage(float aDamage)
{
	mHealth -= aDamage;

	if(mHealth <= 0)
	{
		mHealth = 0;
		mEnt->setVisible(false);
		mActive = 0;
		mBurningCounter = 0;
	}
}