/*********************************************************************//**
*	3D General sounds.
*	V tomto souboru jsou definovany Pooly casto pouzivanych zvuku.
*
*	author: Michal Jirous
*	date: 09.04..2009
*	file: general_sounds.cpp
**********************************************************************/

#include "general_sounds.h"
#include "error_handler.h"
general_sounds::GeneralSounds generalSounds;

using namespace general_sounds;

GeneralSounds::GeneralSounds()
{

}
	



void GeneralSounds::init()	//add sounds to pool
{
	CONSOLE_PRINT( "Loading general sounds..." );
	for( int i = 0; i < STEP_TOTAL_COUNT; ++i )
		m_StepSounds.addSound( STEP_SOUNDS[i] );

	for( int i = 0; i < HURT_TOTAL_COUNT; ++i )
		m_HurtSounds.addSound( HURT_SOUNDS[i] );

	for( int i = 0; i < DESTROY_TOTAL_COUNT; ++i )
		m_DestroySounds.addSound( DESTROY_SOUNDS[i] );

	CONSOLE_PRINT_LN( "done" );
}

void GeneralSounds::playStepSound( const unsigned int type, SoundElement &elem )
{
	if( type < STEP_SOUNDS_COUNT )
	{
		unsigned index = rand() % STEP_ELEM_COUNT[type] + STEP_ELEM_START[type];
		m_StepSounds.use( index, elem );
		elem.play();
	}
}

void GeneralSounds::playHurtSound( const unsigned int type, SoundElement &elem )
{
	if( type < HURT_SOUNDS_COUNT && HURT_ELEM_COUNT[type] != 0 )
	{
		unsigned index = rand() % HURT_ELEM_COUNT[type] + HURT_ELEM_START[type];
		m_HurtSounds.use( index, elem );
		elem.play();
	}
}

void GeneralSounds::playDestroySound( const unsigned int type, SoundElement &elem )
{
	if( type < DESTROY_SOUNDS_COUNT && DESTROY_ELEM_COUNT[type] != 0 )
	{
		unsigned index = rand() % DESTROY_ELEM_COUNT[type] + DESTROY_ELEM_START[type];
		m_DestroySounds.use( index, elem );
		elem.play();
	}
}

void GeneralSounds::destroy()
{
	m_StepSounds.destroy();
	m_HurtSounds.destroy();
}

#include "mathematic.h"

bool GeneralSounds::setLockSound( const unsigned type, SoundElement &elem )
{
	if( soundLibrary.loadSound( elem, LOCKED_SOUNDS[ getFromRange( type, 0u, (unsigned)LOCKED_SOUNDS_COUNT) ] ) )
		return true;
	return false;
}

bool GeneralSounds::setUnLockSound( const unsigned type, SoundElement &elem )
{
	if( soundLibrary.loadSound( elem, UNLOCKED_SOUNDS[ getFromRange( type, 0u, (unsigned)UNLOCKED_SOUNDS_COUNT) ] ) )
		return true;
	return false;
}
