/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/input/rumble_state.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/input/input.hpp"


CSGXRumbleState::CSGXRumbleState() {
	m_pProfile = NULL;
	m_bDurationValid = FALSE;
}

void CSGXRumbleState::update(const tREAL32 telaps) {
	m_fTimecum += telaps;

	if (m_pProfile && m_bDurationValid) {
		if (m_fTimecum > m_fDuration) {
			stop();
		}
	}
}

tREAL32 CSGXRumbleState::getIntensity(const sgxVector3 &fromPosition) const {
	tREAL32 intensity = 0;

	if (isOn()) {
		intensity += m_fStrength * m_pProfile->getRumbleOutput(m_fTimecum);
		if (m_bPositionValid) {
			tREAL32 distance = m_vPosition.getDistanceSquared(fromPosition);
			tREAL32 innerCone = m_pProfile->getInnerCone();
			tREAL32 outerCone = m_pProfile->getOuterCone();

			innerCone *= innerCone;
			outerCone *= outerCone;

			if (distance < innerCone)  {
				// NULL. it's a multiple by 1
			} else if (distance > outerCone) {
				intensity = 0;
			} else {
				intensity *= sgxRescale<tREAL32>(distance, innerCone, outerCone, 1.0f, 0);
			}
		}
	}

	return intensity;
}

tBOOL CSGXRumbleState::isOn() const {
	return m_pProfile ? TRUE : FALSE;
}


void CSGXRumbleState::stop() {
	m_pProfile = NULL;
}

