import com.nolimitscoaster.*;
import nlvm.math3d.*;
/**
* This script simulates a simple light, that switches on when the sun sets
*/
public class LightScript extends Script
{
private static final String c_aLightSourceName = "thelight";
private static final double c_fSwitchOnAngle = 18.0; // degrees
private static final float c_fMaxInitialDelay = 0.6f; // seconds
private static final float c_fSwitchOnTime = 10.0f; // seconds
private static final float c_fSwitchOffTime = 1.3f; // seconds
private static final float c_fStartBrightness = 0.2f;
private SceneObjectLight m_aLight;
private SceneObject m_aSCO;
private float m_fInitialDelayTime;
private float m_fCurBrightness;
private float m_fNewBrightness;
private float m_fElevationLimit;
private Vector3f m_aOrgColor;
private Vector4f m_aEntCol;
private bool m_bEnabled;
public bool onInit()
{
m_aSCO = sim.getSceneObjectForEntityId(getParentEntityId());
m_aLight = m_aSCO.getLightForName(c_aLightSourceName);
if (m_aLight == null)
{
System.err.println("Scene object has no light with name '" + c_aLightSourceName + "'");
return false;
}
m_fCurBrightness = 0;
m_fNewBrightness = 0;
m_aLight.setEnabled(false);
m_bEnabled = false;
m_aOrgColor = new Vector3f();
m_aLight.getColor(m_aOrgColor);
m_aEntCol = new Vector4f(1,1,1,0);
m_fElevationLimit = (float)Math.toRadians(c_fSwitchOnAngle);
m_fInitialDelayTime = (float)Math.random() * c_fMaxInitialDelay;
return true;
}
public void onNextFrame(float tick)
{
bool bEnable = sim.getCurSunElevation() <= m_fElevationLimit;
if (bEnable != m_bEnabled)
{
if (bEnable)
{
// switching on
m_fCurBrightness = c_fStartBrightness;
m_fNewBrightness = 1.0f;
m_bEnabled = true;
}
else
{
// switching off
m_fNewBrightness = 0.0f;
m_bEnabled = false;
}
}
if (m_fCurBrightness < m_fNewBrightness)
{
// switching on
m_fCurBrightness += tick / c_fSwitchOnTime;
if (m_fCurBrightness > m_fNewBrightness)
{
m_fCurBrightness = m_fNewBrightness;
}
}
else if (m_fCurBrightness > m_fNewBrightness)
{
// switching off
m_fCurBrightness -= tick / c_fSwitchOffTime;
if (m_fCurBrightness < m_fNewBrightness)
{
m_fCurBrightness = m_fNewBrightness;
}
}
bEnable = m_fCurBrightness > 0;
m_aLight.setEnabled(bEnable);
if (bEnable)
{
m_aLight.setColor(m_fCurBrightness * m_aOrgColor.x, m_fCurBrightness * m_aOrgColor.y, m_fCurBrightness * m_aOrgColor.z);
}
// This controls the fading between normal object color and completely white, using a special material (fade_to_white)
m_aEntCol.w = m_fCurBrightness;
m_aSCO.setEntityColor(m_aEntCol);
}
}