123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- enum EBoatSpeed
- {
- UNSET,
- SLOWEST,
- SLOW,
- MEDIUM,
- FAST
- }
- class EffectBoatWaterFront : EffectBoatWaterBase
- {
- protected const int EMITORS_FAST = 9; // emitors below this ID only play when fast enough
-
- void EffectBoatWaterFront()
- {
- m_EmitorCount = 13;
- m_SpeedSlow = 5;
- m_SpeedMedium = 20;
- m_SpeedFast = 40;
-
- SetParticleState( ParticleList.BOAT_WATER_FRONT );
- }
-
- override protected void UpdateSpeedState(Particle ptc, float speed)
- {
- EBoatSpeed speedStatePast = m_SpeedState;
-
- if (speed > m_SpeedFast)
- m_SpeedState = EBoatSpeed.FAST;
- else if (speed > m_SpeedMedium)
- m_SpeedState = EBoatSpeed.MEDIUM;
- else if (speed > m_SpeedSlow)
- m_SpeedState = EBoatSpeed.SLOW;
- else
- m_SpeedState = EBoatSpeed.SLOWEST;
-
- if (m_SpeedState == speedStatePast)
- return;
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i == 5 || i == 6)
- {
- if (m_SpeedState <= EBoatSpeed.MEDIUM) // fast drops
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
- else if (i == 9 || i == 10) // slow drops
- {
- if (m_SpeedState != EBoatSpeed.MEDIUM)
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
- else if (i < 5 || i == 7 || i == 8) // rest of front ptc
- {
- if (m_SpeedState <= EBoatSpeed.SLOW)
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
- }
- }
-
- override void Update(float timeSlice = 0)
- {
- if (m_Boat.GetCurrentGear() == 0)
- {
- if (IsPlaying())
- {
- GetParticle().SetParticleParam(EmitorParam.BIRTH_RATE, 0);
- GetParticle().SetParticleParam(EmitorParam.BIRTH_RATE_RND, 0);
- Stop();
- }
-
- return;
- }
-
- super.Update(timeSlice);
-
- Particle ptc = GetParticle();
- float lerp1, lerp2;
-
- vector velocity = dBodyGetVelocityAt(m_Boat, m_Boat.CoordToParent(GetLocalPosition()));
- float speed = velocity.Normalize() * 3.6; // to km/h
- UpdateSpeedState(ptc, speed);
-
- UpdatePosToSeaLevel(timeSlice, -0.2);
-
- lerp1 = Math.InverseLerp(0, m_SpeedFast, speed);
- lerp2 = Math.InverseLerp(m_SpeedMedium - 5, m_SpeedFast + 10, speed);
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i == 11 || i == 12) // waves
- {
- ptc.SetParameter(i, EmitorParam.VELOCITY, ptc.GetParameterOriginal(i, EmitorParam.VELOCITY) * lerp1);
- }
- else if (i < 5 || i == 7 || i == 8) // splashes
- {
- if (m_SpeedState > EBoatSpeed.SLOW)
- {
- if (GetGame().GetWaterDepth(m_Boat.CoordToParent(m_MemPointPos)) < -0.1) // when the front of the boat is above water, disable the emitor
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
-
- ptc.SetParameter(i, EmitorParam.SIZE, ptc.GetParameterOriginal(i, EmitorParam.SIZE) * lerp2);
- }
- }
- }
- }
- }
- class EffectBoatWaterBack : EffectBoatWaterBase
- {
- protected const int EMITORS_FAST = 5; // emitors below this ID only play when fast enough
-
- void EffectBoatWaterBack()
- {
- m_EmitorCount = 10;
- m_SpeedSlow = 25;
- m_SpeedMedium = 50;
- m_SpeedFast = 150;
-
- SetParticleState( ParticleList.BOAT_WATER_BACK );
- }
-
- override protected void UpdateSpeedState(Particle ptc, float speed)
- {
- EBoatSpeed speedStatePast = m_SpeedState;
-
- if (speed > m_SpeedFast)
- m_SpeedState = EBoatSpeed.FAST;
- else if (speed > m_SpeedMedium)
- m_SpeedState = EBoatSpeed.MEDIUM;
- else if (speed > m_SpeedSlow)
- m_SpeedState = EBoatSpeed.SLOW;
- else
- m_SpeedState = EBoatSpeed.SLOWEST;
-
- if (m_SpeedState == speedStatePast)
- return;
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i < EMITORS_FAST) // big splash
- {
- if (m_SpeedState <= EBoatSpeed.SLOW)
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
-
- if (i >= EMITORS_FAST && i != 9)
- {
- if (m_SpeedState == EBoatSpeed.SLOWEST)
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
- }
- }
-
- override void Update(float timeSlice = 0)
- {
- if (m_Boat.GetCurrentGear() == 0)
- {
- if (IsPlaying())
- {
- GetParticle().SetParticleParam(EmitorParam.BIRTH_RATE, 0);
- GetParticle().SetParticleParam(EmitorParam.BIRTH_RATE_RND, 0);
- Stop();
- };
-
- return;
- }
-
- super.Update(timeSlice);
- Particle ptc = GetParticle();
-
- float speed = m_Boat.PropellerGetAngularVelocity();
- UpdateSpeedState(ptc, speed);
-
- float lerp = Math.InverseLerp(0, m_SpeedFast, speed);
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i < EMITORS_FAST && m_SpeedState > EBoatSpeed.SLOW)
- {
- ptc.SetParameter(i, EmitorParam.SIZE, ptc.GetParameterOriginal(i, EmitorParam.SIZE) * lerp);
- ptc.SetParameter(i, EmitorParam.BIRTH_RATE, ptc.GetParameterOriginal(i, EmitorParam.BIRTH_RATE) * lerp);
- }
- }
- }
- }
- class EffectBoatWaterSide : EffectBoatWaterBase
- {
- protected const int EMITORS_FAST = 3; // emitors below this ID only play when fast enough
-
- void EffectBoatWaterSide()
- {
- m_EmitorCount = 5;
- m_SpeedSlow = 5;
- m_SpeedMedium = 20;
- m_SpeedFast = 40;
-
- SetParticleState( ParticleList.BOAT_WATER_SIDE );
- }
-
- override protected void UpdateSpeedState(Particle ptc, float speed)
- {
- EBoatSpeed speedStatePast = m_SpeedState;
-
- if (speed > m_SpeedFast)
- m_SpeedState = EBoatSpeed.FAST;
- else if (speed > m_SpeedMedium)
- m_SpeedState = EBoatSpeed.MEDIUM;
- else if (speed > m_SpeedSlow)
- m_SpeedState = EBoatSpeed.SLOW;
- else
- m_SpeedState = EBoatSpeed.SLOWEST;
-
- if (m_SpeedState == speedStatePast)
- return;
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i < EMITORS_FAST) // splashes
- {
- if (m_SpeedState <= EBoatSpeed.SLOW)
- EnableEmitor(ptc, i, false);
- else
- EnableEmitor(ptc, i, true);
- }
- }
- }
-
- override void Update(float timeSlice = 0)
- {
- super.Update(timeSlice);
-
- Particle ptc = GetParticle();
-
- vector velocity = dBodyGetVelocityAt(m_Boat, m_Boat.CoordToParent(GetLocalPosition()));
- float speed = velocity.Normalize() * 3.6; // to km/h
- UpdateSpeedState(ptc, speed);
-
- UpdatePosToSeaLevel(timeSlice, -0.2);
-
- float lerp = Math.InverseLerp(0, m_SpeedFast, speed);
-
- for (int i = 0; i < m_EmitorCount; i++)
- {
- if (i < EMITORS_FAST) // splashes
- {
- if (m_SpeedState > EBoatSpeed.SLOW)
- ptc.SetParameter(i, EmitorParam.SIZE, ptc.GetParameterOriginal(i, EmitorParam.SIZE) * lerp);
- }
- else // waves
- {
- ptc.SetParameter(i, EmitorParam.SIZE, ptc.GetParameterOriginal(i, EmitorParam.SIZE) * lerp);
- ptc.SetParameter(i, EmitorParam.VELOCITY, ptc.GetParameterOriginal(i, EmitorParam.VELOCITY) * lerp);
- }
- }
- }
- }
- class EffectBoatWaterBase : EffectParticle
- {
- const float POS_UPDATE_THROTTLE = 0.2; // seconds, controls how often can boat update somewhat expensive particle local reposition
-
- protected int m_EmitorCount;
- protected int m_SpeedSlow;
- protected int m_SpeedMedium;
- protected int m_SpeedFast;
- protected float m_PosUpdateTimer;
-
- protected EBoatSpeed m_SpeedState;
- protected BoatScript m_Boat;
- protected vector m_MemPointPos;
-
- override void AttachTo(Object obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_rotation_to_world = false)
- {
- m_Boat = BoatScript.Cast(obj);
- m_MemPointPos = local_pos;
- super.AttachTo(obj, local_pos, local_ori, force_rotation_to_world);
- }
-
- void SetParticleState(int state)
- {
- bool was_playing = IsPlaying();
-
- Stop();
-
- SetParticleID(state);
-
- if (was_playing)
- {
- Start(); // resume effect
- }
- }
-
- protected void EnableEmitor(Particle ptc, int id, bool enable)
- {
- if (enable)
- {
- ptc.SetParameter(id, EmitorParam.BIRTH_RATE, ptc.GetParameterOriginal(id, EmitorParam.BIRTH_RATE));
- ptc.SetParameter(id, EmitorParam.BIRTH_RATE_RND, ptc.GetParameterOriginal(id, EmitorParam.BIRTH_RATE_RND));
- }
- else
- {
- ptc.SetParameter(id, EmitorParam.BIRTH_RATE, 0);
- ptc.SetParameter(id, EmitorParam.BIRTH_RATE_RND, 0);
- }
- }
-
- // Update on state change
- protected void UpdateSpeedState(Particle ptc, float speed)
- {}
-
- // Update runs every frame
- void Update(float timeSlice = 0)
- {
- if (!IsPlaying())
- {
- m_SpeedState = EBoatSpeed.UNSET; // reinit
- Start();
- }
- };
-
- // Adjust position to sea level
- protected void UpdatePosToSeaLevel(float timeSlice = 0, float surfaceOffset = 0)
- {
- m_PosUpdateTimer += timeSlice;
- if (m_PosUpdateTimer > POS_UPDATE_THROTTLE)
- {
- m_PosUpdateTimer = 0;
-
- vector posAdjusted = m_Boat.CoordToParent(m_MemPointPos);
- posAdjusted[1] = GetGame().SurfaceGetSeaLevel() + surfaceOffset;
- posAdjusted = m_Boat.CoordToLocal(posAdjusted);
- SetCurrentLocalPosition(posAdjusted);
- }
- }
- }
|