/*
	Trig Wars.
	Copyright (c) 2008, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		This is a simple extension of the ordinary bullet manager that caches a reference to the player ship
///				so that enemy bullets can quickly check for collisions with it.
//=====================================================================================================================

#include "Precompiled.h"
#include "Game\EnemyBulletManager.h"
#include "Game\GameCore.h"
#include "GameObjects\PlayerShip.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Creates the bullet manager
/// @param		ai_manager - ai manager the bullet manager belongs to
/// @param		bullet_count - number of bullets to make
/// @param		stack - Stack that holds a pointer for each bullet. This function expects the list of 
///				bullet pointers to be at the very top of the stack.
/// @param		bullet_color - color of each bullet when rendering
/// @param		bullet_length - how long the bullets should be when drawing
/// @pre		AI manager given must not be null
/// @pre		Bullet count must be 1 or more
/// @pre		The game must exist
/// @pre		The game must have a texture manager
/// @remarks	The manager takes ownership of bullet memory after this function is called, and is 
///				reponsible for deleting the created bullets.
//=====================================================================================================================

EnemyBulletManager::EnemyBulletManager( AIManager * ai_manager , unsigned bullet_count , Stack & stack , Color bullet_color , float bullet_length ) 
: 
BulletManager
(
	bullet_count,
	stack,
	bullet_color,
	bullet_length
)
{
	// Make sure ok:
	
	ENSURE( ai_manager != 0									, "AI manager given must not be null"		)
	ENSURE( bullet_count >= 1								, "Bullet count must be 1 or more"			)
	ENSURE( GameCore::GetSingleton()						, "The game must exist"						)
	ENSURE( GameCore::GetSingleton()->GetTextureManager()	, "The game must have a texture manager"	)
	
	// Save this
	
	m_ai_manager = ai_manager;

	// We haven't found the player yet

	m_player = 0;

	// Load our particle texture
	
	m_particle_texture = GameCore::GetSingleton()->GetTextureManager()->GetTexture("Textures\\GlowParticle.dds");
}

//=====================================================================================================================
/// @brief		Destructor. Frees resources
//=====================================================================================================================

EnemyBulletManager::~EnemyBulletManager()
{
	// Free reference to the player:
	
	if ( m_player ) m_player->RemoveReference();
}

//=====================================================================================================================
/// @brief		Update all function: updates reference to the player ship as well as doing base class logic
/// @pre		The game must exist
/// @pre		The game must have a game object manager
/// @post		All cached AI object managers will be of the correct type
//=====================================================================================================================

void EnemyBulletManager::UpdateAll( float time_delta )
{
	// Make sure ok:
	
	ENSURE( GameCore::GetSingleton()						, "The game must exist"							);
	ENSURE( GameCore::GetSingleton()->GetObjectManager()	, "The game must have a game object manager"	);

	// See if we have a player ship:
	
	if ( m_player )
	{
		// See if now out of the game: if so then free
		
		if ( m_player->HasManager() == false )
		{
			m_player->RemoveReference(); m_player = 0;
		}
	}
	else
	{
		// Try to find the player
		
		m_player = GameCore::GetSingleton()->GetObjectManager()->FindByType<PlayerShip>("PlayerShip");
		
		// Add a reference to it if found
		
		if ( m_player ) m_player->AddReference();
	}

	// Do base class update:
	
	BulletManager::UpdateAll(time_delta);
}

//=====================================================================================================================
/// @brief		Annouces the type name for this object.
/// @returns	Type name of this object
//=====================================================================================================================

const char * EnemyBulletManager::GetTypeName() const
{
	return "EnemyBulletManager";
}
