//	ColourBarrier.cpp - Blocks anything not of the same colour.
//  ----------------------------------------------------------------------------
//	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 "ColourBarrier.h"
#include "GamePlayState.h"
#include "MiscHelper.h"
#include "Zombie.h"
#include "Puddle.h"
#include "Bullet.h"
#include "Tank.h"
#include "Mine.h"

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
PuddleCollisionCreator<ColourBarrier> ColourBarrier::creator;

//------------------------------------------------------------------------------
ColourBarrier::ColourBarrier(const shared_ptr<Mine>& mine):
PuddleCollisionModifier(mine)
{
	colour = mine->getColour();
}

//------------------------------------------------------------------------------
ColourBarrier::~ColourBarrier()
{

}

//------------------------------------------------------------------------------
void ColourBarrier::collideTank(Puddle& puddle,
								Tank& tank,
								GamePlayState& state)
{
	if(tank.getColour() != colour)
	{
		float offset;
		const TwoFloats origin;
		TwoFloats diff(puddle.getPosition() - tank.getPosition());
		float dist = origin.getDistance(diff);
		float rad = puddle.getRadius() + tank.getRadius();

		diff = diff.normal();
		offset = (rad - dist);
		tank.setPosition(tank.getPosition() - (diff*offset), false);
	}
}

//------------------------------------------------------------------------------
void ColourBarrier::collideBullet(Puddle& puddle,
								  Bullet& bullet,
								  GamePlayState& state)
{
	if(bullet.getColour() != colour)
		bullet.setDead();
}

//------------------------------------------------------------------------------
void ColourBarrier::collideZombie(Puddle& puddle,
								  Zombie& zombie,
								  GamePlayState& state)
{
	if(zombie.getColour() != colour)
	{
		float offset;
		const TwoFloats origin;
		TwoFloats diff(puddle.getPosition() - zombie.getPosition());
		float dist = origin.getDistance(diff);
		float rad = puddle.getRadius() + zombie.getRadius();

		diff = diff.normal();
		offset = (rad - dist);
		zombie.setPosition(zombie.getPosition() - (diff*offset));
	}
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void ColourBarrierCreator::apply(GamePlayState& state)
{
	state.registerPuddleCollisionMod(&ColourBarrier::creator);
}
