//
// C++ Implementation: timer
//
// Description:
//
//
// Author: Messenger of death aka Denis A. Birukov <messengerofdeath@gmail.com>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "timer.h"
#include "config_classes.h"
#include "config.h"

ObjectFabric<Timer> TimerFabric;

Timer::Timer()
		: SceneObject(),
		INIT_SLOT ( Timer, this, _on_new_cycle )
{
}


Timer::~Timer()
{
}

bool Timer::load ( std::string storage_name, std::string storage_prefix, std::string scene_name, std::string object_name )
{
	bool state = SceneObject::load ( storage_name, storage_prefix, scene_name, object_name );
	if ( !state ) return state;
	bool ret = SceneObject::load ( storage_name, storage_prefix, scene_name, object_name );
	if ( !ret ) return ret;
	Config_Interface *intf = config_get_interface ( storage_name );
	storage_prefix+="Timer->";
	_repeat_ = ( intf->_get_value ( storage_prefix + "repeat", "yes" ) == "yes" ) ?true:false;
	_dt_ = atof ( intf->_get_value ( storage_prefix + "time", "1" ).c_str() );
	_time_end_.set_name ( scene_name+"->"+_object_name_+"->time_out" );
        register_slot(_scene_name_ + "->" + _object_name_ + "->" + "on_new_cycle", &_on_new_cycle );
	return true;
}

DEFINE_SLOT ( Timer, _on_new_cycle )
{
	float dt = _timer_.get_elapsed_time_noreset();
	if ( dt>=_dt_ )
	{
		_timer_.reset();
		_time_end_.clear_params();
		_time_end_.add_params ( dt );
		_time_end_.rise();
	}
        return false;
}


