//	Mine.cpp - The mine class.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "drawing/Drawer.h"
#include "Bullet.h"
#include "Mine.h"
#include "Tank.h"

//------------------------------------------------------------------------------
Mine::Mine(const TwoFloats& pos,
		   const std::wstring& parent,
		   const ThreeFloats& col):
Collidable(L"Mine"),
position(pos),
owner(parent),
colour(col),
radius(10.0f),
flash(0),
active(120),
dead(false),
triggeredByBullets(false),
camouflage(false),
december(false)
{

}

//------------------------------------------------------------------------------
Mine::~Mine()
{

}

//------------------------------------------------------------------------------
bool Mine::update()
{
	if(active > 0)
		--active;

	++flash;
	flash %= 60;

	return dead;
}

//------------------------------------------------------------------------------
void Mine::draw(shared_ptr<Drawer>& d, float timestepInterp)
{
	if(camouflage)
	{
		if(december)
		{
			d->drawImage(L"PresentBack",
						 TwoFloats(position.x-16.0f, position.y-16.5f),
						 1.0f,
						 0.1875f);

			d->drawImage(L"PresentTop",
						 TwoFloats(position.x-16.0f, position.y-16.5f),
						 1.0f,
						 colour);
		}
		else
		{
			d->drawImage(L"PickupBack", position-13.5f, 1.0f, 0.1875f);

			d->drawImage(L"PickupTop",
						 position-15.5f,
						 1.0f,
						 colour);
		}
	}
	else
	{
		float alpha = 0.5f;
		ThreeFloats col(colour);

		if(flash > 30)
		{
			col = 1.0f;
			alpha = 0.1f;
		}

		d->drawImage(L"MineBack", position-6.0f, 1.0f, 1.0f, 0.1f);
		d->drawImage(L"MineTop", position-3.5f, 1.0f, col, alpha);
	}
}

//------------------------------------------------------------------------------
void Mine::setTriggeredByBullets()
{
	triggeredByBullets = true;
}

//------------------------------------------------------------------------------
void Mine::setCamouflage(bool present)
{
	camouflage = true;
	december = present;
	radius = 14.0f;
}

//------------------------------------------------------------------------------
bool Mine::interestedInCollisionsWith(const std::wstring& entity) const
{
	bool retval = false;

	if((entity == L"Tank") ||
	   (entity == L"Zombie") ||
	   (triggeredByBullets && (entity == L"Bullet")))
	{
		retval = true;
	}

	return retval;
}

//------------------------------------------------------------------------------
bool Mine::collide(const shared_ptr<Collidable>& other, GamePlayState& state)
{
	bool retval = false;
	const std::wstring& n(other->getName());

	if(active > 0)
		return retval;

	if((n == L"Tank") ||
	   (n == L"Zombie") ||
	   (triggeredByBullets && (n == L"Bullet")))
	{
		state.addShockwave(position, colour);
		dead = true;
		retval = true;
	}

	return retval;
}
