//	Pickup.cpp - The collidable pickup 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 "MiscHelper.h"
#include "Bullet.h"
#include "Pickup.h"
#include "Tank.h"

#include <cassert>

//------------------------------------------------------------------------------
Pickup::Pickup(const TwoFloats& pos, const std::wstring& type):
Collidable(L"Pickup"),
position(pos),
pickupType(type),
bulletCollection(false),
collected(false),
december(false),
colour(0.1875f)
{
	
}

//------------------------------------------------------------------------------
Pickup::~Pickup()
{

}

//------------------------------------------------------------------------------
void Pickup::update(const TwoFloats& arenaTopLeft,
			    const TwoFloats& arenaBottomRight)
{
	
}

//------------------------------------------------------------------------------
void Pickup::draw(shared_ptr<Drawer>& d,
				  float timestepInterp,
				  const ThreeFloats& leftCol,
				  const ThreeFloats& rightCol,
				  const ThreeFloats& borderCol)
{
	const float interp(position.x/d->getDrawingAreaSize().x);
	ThreeFloats col(rightCol * interp);

	col += leftCol * (1.0f-interp);

	if(december)
	{
		d->drawImage(L"PresentBack",
					 TwoFloats(position.x-16.0f, position.y-16.5f),
					 1.0f,
					 borderCol);

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

		d->drawImage(L"PickupTop",
					 position-15.5f,
					 1.0f,
					 ThreeFloats(col.y, col.z, col.x));
	}
}

//------------------------------------------------------------------------------
void Pickup::setBulletCollection()
{
	bulletCollection = true;
}

//------------------------------------------------------------------------------
void Pickup::setDecember()
{
	december = true;
}

//------------------------------------------------------------------------------
bool Pickup::interestedInCollisionsWith(const std::wstring& entity) const
{
	if((bulletCollection && (entity == L"Bullet")) || (entity == L"Tank"))
		return true;
	else
		return false;
}

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

	if((bulletCollection && (n == L"Bullet")) ||
	   (n == L"Tank"))
	{
		shared_ptr<Tank> t(dynamic_pointer_cast<Tank>(other));

		if(t)
		{
			collectorColour = t->getColour();
			t->incrementHealth(0.2f);
		}
		else //It was collected by a bullet.
		{
			shared_ptr<Bullet> b(dynamic_pointer_cast<Bullet>(other));

			assert(b != 0);

			collectorColour = b->getColour();
		}

		collected = true;
	}

	return true;
}
