/*********************************************************************//**
*	Tlacitko
*	Implementace objektu hrace.
*	
*	author: Michal Jirous
*	date: 23.04.2009
*	file: ent_buttontarget.cpp
**********************************************************************/

#include "ent_simple_functions.h"

ButtonTarget::ButtonTarget() : CBaseFunction()
{
	m_sClassName = "button_target";
	m_iProperties = ENT_PARM_RUNNABLE | ENT_PARM_COLLIDING | ENT_PARM_RENDER | ENT_PARM_INTERACTIVE;

	m_bStore_UseActivate = false;
	m_bStore_StartOn = false;
	m_bStore_Toggle = false;
	m_uiResetDelay = 0;
	m_uiResetTime = TRIGGER_INFINITE;
	m_TargetList = NULL;
	m_bStarted = false;
	m_uiPlaySound = 0;
}



void ButtonTarget::passFlags( int flags )
{
	if( flags & USE_ACTIVATES )
		m_bStore_UseActivate = true;
	if( flags & START_ON )
		m_bStore_StartOn = true;
	if( flags & TOGGLE )
		m_bStore_Toggle = true;
}

void ButtonTarget::setParameters( parameters_t &parametersMap )
{
	CBaseFunction::setParameters( parametersMap );
	
	std::string value = getParameterValue( parametersMap, "returntime" );
	if( !value.empty() )
		m_uiResetDelay = atoi( value.c_str() );

	value = getParameterValue( parametersMap, "pushsound" );
	if( !value.empty() )
	{
		m_uiPlaySound = atoi( value.c_str() );
		if( m_uiPlaySound != 0 )	//0 je zadny zvuk
		{
			if( m_uiPlaySound <= SND_BUTTON_COUNT )
			{
				soundLibrary.loadSound( m_PushSound, BUTTON_SOUNDS[ m_uiPlaySound - 1 ] );
				m_PushSound.setPosition( origin.x, origin.y, origin.z );		
			}
		}
	}
}

void ButtonTarget::decompile()
{
	if( m_uiPlaySound )
		soundLibrary.unloadSound( m_PushSound );
}


void ButtonTarget::onUsage( const RayTraceData &rayData )
{
	if( ( !m_bSwitched || m_bStore_Toggle ) && m_bStore_UseActivate )
		push();
}

void ButtonTarget::onTarget( int target_type )
{
	if( ( !m_bSwitched || m_bStore_Toggle ) && !m_bStore_UseActivate )
		push();
}

void ButtonTarget::restart()
{
	CBaseFunction::restart();
	m_uiResetTime = TRIGGER_INFINITE;
	if( m_bStore_StartOn )
		push();
	m_bStarted = false;
}

#include "game.h"

void ButtonTarget::update( float seconds )
{
	CBaseFunction::update(seconds);
	if( !m_bStarted && m_bStore_StartOn )
	{
		m_uiResetTime = game.getGameTime();
		m_bStarted = true;
		push();
	}


	if( !m_bStore_Toggle && m_bSwitched && m_uiResetTime != TRIGGER_INFINITE && m_uiResetDelay != TRIGGER_INFINITE && game.getGameTime() >= m_uiResetTime + m_uiResetDelay )
	{
		switchTextures();
		m_uiResetTime = TRIGGER_INFINITE;
	}
}

void ButtonTarget::push()
{
	if( !m_bSwitched && !m_bStore_Toggle || m_bStore_Toggle )
	{
		if( m_uiResetDelay != TRIGGER_INFINITE )
			m_uiResetTime = game.getGameTime();

		trigger();

		switchTextures();

	}
}

void ButtonTarget::trigger()
{
	if( m_uiPlaySound )
		m_PushSound.play();

	m_BackTimeData.push_front( BackTimeData() );
	m_BackTimeData.front().event_time = game.getGameTime();
	m_BackTimeData.front().type = EVENT_TRIGGER;

	if( m_TargetList )
		for( entityList_t::iterator iter = m_TargetList->begin(); iter != m_TargetList->end(); ++iter )
			(*iter)->onTarget( 0 );

	
}

#include "level_loader.h"

void ButtonTarget::compile()
{
	CBaseFunction::compile();
}

void ButtonTarget::passStoredEvent( BackTimeData &backData )
{
	if( backData.type == EVENT_TRIGGER )
		trigger();
	else
		CBaseFunction::passStoredEvent( backData );

}

