Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///-----------------------------------------------------------------
- /// Author : Sebastien RAYMONDAUD
- /// Date : 15/10/2019 16:55
- ///-----------------------------------------------------------------
- using Convoy.DataScriptables.Locomotive;
- using Convoy.Train.Modules;
- using FMODUnity;
- using UnityEngine;
- namespace Convoy.Locomotive
- {
- public enum DirectionMove { Forward, Backward}
- [SelectionBase]
- public class Motor : MonoBehaviour
- {
- #region PROPERTIES
- [SerializeField] private StudioEventEmitter m_motorSound = null;
- [SerializeField] private StudioEventEmitter m_brakeSound = null;
- [SerializeField] private MotorData m_motorData = null;
- [SerializeField] private FuelData m_fuelData = null;
- private float currentSpeed = 0;
- private float additionnalSpeed = 0;
- private bool isStop = false;
- private bool isSlowdown = false;
- private bool isBrake = false;
- private bool isSlowByGrass = false;
- private float maxSpeed = 0;
- private bool onAccelerate = false;
- private bool onDecelerate = false;
- private float elapsedTime = 0;
- private bool waitOwner = false;
- private float timeLeftSlow = 0;
- private float targetSpeedSlowGrass = 0;
- private string paramFMODMusicSpeed = "Vitesse";
- private DirectionMove directionMove = DirectionMove.Forward;
- private System.Collections.Generic.Dictionary<int, float> draisines = new System.Collections.Generic.Dictionary<int, float>();
- public float CurrentSpeed {
- get {
- //float max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
- //float speed = Mathf.Min(max, (currentSpeed + additionnalSpeed));
- //
- //if (timeLeftSlow > 0)
- //{
- // timeLeftSlow -= Time.deltaTime;
- // slowSpeed = Mathf.Lerp(initialSpeed, targetSpeed, timeLeftSlow / initialTimeLeft);
- // speed = Mathf.Min(speed, slowSpeed);
- // Debug.LogError("=> " + speed + " [" + initialTimeLeft + " to " + targetSpeed + "]");
- //}
- //
- //return speed;
- return currentSpeed;
- }
- }
- public float CurrentSpeedWithBrake { get { return Mathf.Min(isBrake ? m_motorData.SpeedBrake : maxSpeed, (currentSpeed + additionnalSpeed)); } }
- public float MaxSpeed { get { return m_motorData.SpeedMaxWithFuel; } }
- public bool IsBrake { get { return isBrake; } }
- public float SpeedGoBack { get { return m_motorData.SpeedGoBack; } }
- public bool WaitOwner { get { return waitOwner; } }
- #endregion
- #region EVENTS
- public delegate void Event_Movement(bool move);
- public Event_Movement OnMovement = null;
- public delegate void Event_UpdateSpeed(float currentSpeed);
- public Event_UpdateSpeed OnUpdateSpeed = null;
- public delegate void Event_MusicChange(string param, float value);
- public static Event_MusicChange OnUpdateMusic = null;
- public delegate void Event_NecessiteDraisine(bool necessite);
- public Event_NecessiteDraisine OnNecessiteDraisine;
- #endregion
- #region UNITY_METHODS
- private void Awake()
- {
- m_motorData.SetMotor(this);
- }
- private void Start()
- {
- if (m_motorSound != null)
- m_motorSound.Play();
- m_fuelData.Init();
- firstPos = transform.position;
- isStop = true;
- onAccelerate = false;
- onDecelerate = false;
- maxSpeed = m_motorData.SpeedMaxWithFuel;
- Draisine.OnAddPower += Draisine_OnAddPower;
- }
- private void Update ()
- {
- if (directionMove == DirectionMove.Backward)
- {
- OnUpdateSpeed?.Invoke(-maxSpeed);
- OnUpdateMusic?.Invoke(paramFMODMusicSpeed, 0);
- return;
- }
- UpdateSpeedValue();
- Movement();
- if (!waitOwner)
- {
- if (additionnalSpeed > 0)
- {
- isSlowdown = false;
- isStop = false;
- OnMovement?.Invoke(true);
- if (m_fuelData.Fuel > 0)
- {
- SwitchSpeed(true);
- }
- }
- else if(isStop && m_fuelData.Fuel > 0)
- {
- isStop = false;
- isSlowdown = false;
- OnMovement?.Invoke(true);
- SwitchSpeed(true);
- }
- else if (m_fuelData.Fuel <= 0 && !isSlowdown)
- {
- isSlowdown = true;
- SwitchSpeed(false);
- }
- }
- if (isSlowdown && m_fuelData.Fuel > 0)
- {
- isSlowdown = false;
- SwitchSpeed(true);
- }
- if (isSlowdown && currentSpeed + additionnalSpeed <= 0)
- {
- isStop = true;
- OnMovement?.Invoke(false);
- }
- if (onAccelerate)
- {
- Accelerate(true, m_motorData.AccelerationTime, m_motorData.AccelerationCurve);
- }
- else if (onDecelerate)
- {
- Accelerate(false, m_motorData.DecelerationTime, m_motorData.DecelerationCurve);
- }
- }
- #endregion
- #region METHODS
- private void UpdateSpeedValue()
- {
- float max = maxSpeed;
- if (isBrake)
- max = m_motorData.SpeedBrake;
- float speed = Mathf.Min(currentSpeed + additionnalSpeed, maxSpeed);
- if (isSlowByGrass)
- {
- if (timeLeftSlow > 0)
- {
- timeLeftSlow -= Time.deltaTime;
- max = targetSpeedSlowGrass;
- }
- else
- {
- isSlowByGrass = false;
- max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
- }
- }
- if (m_fuelData.Fuel <= 0)
- max = 0;
- if (onAccelerate || onDecelerate)
- max = currentSpeed;
- currentSpeed = Mathf.Lerp(speed, max, 3 * Time.deltaTime);
- }
- public void StopConsumeFuel()
- {
- m_fuelData.consumeEnable = false;
- }
- public void StopMovementAndWaitDraisine()
- {
- waitOwner = true;
- isStop = true;
- onAccelerate = false;
- onDecelerate = false;
- currentSpeed = 0;
- additionnalSpeed = 0;
- OnMovement?.Invoke(false);
- OnNecessiteDraisine?.Invoke(true);
- }
- public void SetDirection(DirectionMove directionMove)
- {
- this.directionMove = directionMove;
- if (directionMove == DirectionMove.Backward)
- {
- isStop = true;
- if (m_motorSound != null)
- m_motorSound.SetParameter(paramFMODMusicSpeed, 0);
- }
- }
- public void Brake(bool brake = true)
- {
- isBrake = brake;
- }
- public void LoseSpeed(float percentLost)
- {
- currentSpeed *= percentLost;
- onAccelerate = m_fuelData.Fuel>0;
- }
- public void SlowSpeed(float time, float percentSpeed)
- {
- isSlowByGrass = true;
- timeLeftSlow = time;
- targetSpeedSlowGrass = maxSpeed * percentSpeed;
- }
- private void Movement()
- {
- if (waitOwner)
- {
- OnUpdateSpeed?.Invoke(0);
- return;
- }
- /*
- float max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
- float speed = Mathf.Min(max, (currentSpeed + additionnalSpeed));
- if (timeLeftSlow > 0)
- {
- timeLeftSlow -= Time.deltaTime;
- speed = speed * percentSpeed;
- }
- */
- OnUpdateSpeed?.Invoke(CurrentSpeed);
- transform.Translate(transform.forward * CurrentSpeed * Time.smoothDeltaTime, Space.World);
- if (m_motorSound != null)
- m_motorSound.SetParameter(paramFMODMusicSpeed, CurrentSpeed / m_motorData.SpeedMaxWithFuel);
- OnUpdateMusic?.Invoke(paramFMODMusicSpeed, (CurrentSpeed / m_motorData.SpeedMaxWithFuel) * 2);
- }
- private void SwitchSpeed(bool accelerate)
- {
- elapsedTime = 0;
- onAccelerate = accelerate;
- onDecelerate = !accelerate;
- }
- private void Accelerate(bool move, float t, AnimationCurve curve)
- {
- if (move)
- elapsedTime = (currentSpeed / maxSpeed) * t;
- else
- elapsedTime = (1 - (currentSpeed / maxSpeed)) * t;
- elapsedTime += Time.deltaTime;
- float ratio = elapsedTime / t;
- currentSpeed = curve.Evaluate(ratio) * (maxSpeed + additionnalSpeed);
- currentSpeed = Mathf.Clamp(currentSpeed, 0, maxSpeed);
- if (ratio >= 1)
- {
- onAccelerate = false;
- onDecelerate = false;
- }
- }
- public void StopSound()
- {
- if (m_motorSound != null)
- {
- m_motorSound.SetParameter(paramFMODMusicSpeed, 0);
- m_motorSound.Stop();
- }
- }
- public void Draisine_OnAddPower(int id, float powerAdd)
- {
- if (!draisines.ContainsKey(id))
- draisines.Add(id, powerAdd);
- draisines[id] = powerAdd;
- additionnalSpeed = 0;
- foreach (var item in draisines)
- {
- additionnalSpeed += item.Value;
- }
- additionnalSpeed = Mathf.Clamp(additionnalSpeed, 0, m_motorData.SpeedMaxWithFuel);
- }
- public void StopWaitOwner()
- {
- waitOwner = false;
- OnNecessiteDraisine?.Invoke(false);
- m_fuelData.consumeEnable = true;
- Debug.Log("Allez on repart chacal");
- }
- public void Death()
- {
- currentSpeed = 0;
- additionnalSpeed = 0;
- }
- #endregion
- #region GIZMOS
- private Vector3 firstPos;
- private void OnDrawGizmos()
- {
- float distanceAcceleration = CalculateDistanceCurve(m_motorData.AccelerationTime,
- m_motorData.SpeedMaxWithFuel,
- m_motorData.AccelerationCurve);
- float distanceDeceleration = CalculateDistanceCurve(m_motorData.DecelerationTime,
- m_motorData.SpeedMaxWithFuel,
- m_motorData.DecelerationCurve);
- float distanceWithMaxFuel = CalculateDistanceWithFuel(m_fuelData.FuelMax - (m_fuelData.FuelLossPerSecond * m_motorData.AccelerationTime),
- m_fuelData.FuelLossPerSecond,
- m_motorData.SpeedMaxWithFuel);
- Vector3 posStartLine = firstPos + Vector3.up * 2;
- Vector3 nextPos = posStartLine + (transform.forward * distanceAcceleration);
- Gizmos.color = Color.red;
- Gizmos.DrawLine(posStartLine, nextPos);
- posStartLine = nextPos;
- nextPos = posStartLine + (transform.forward * distanceWithMaxFuel);
- Gizmos.color = Color.blue;
- Gizmos.DrawLine(posStartLine, nextPos);
- posStartLine = nextPos;
- nextPos = posStartLine + (transform.forward * distanceDeceleration);
- Gizmos.color = Color.green;
- Gizmos.DrawLine(posStartLine, nextPos);
- }
- private float CalculateDistanceCurve(float time, float speedMax, AnimationCurve curve, float initialValueCurve = 0)
- {
- float delta = 1 / 1000f;
- float r = initialValueCurve;
- float dist = 0;
- while (r < 1)
- {
- dist += ((curve.Evaluate(r) * speedMax) * delta) * time;
- r += delta;
- }
- return dist;
- }
- private float CalculateDistanceWithFuel(float fuel, float fuelLoss, float speedMax)
- {
- float timeForLossAllFuel = fuel / fuelLoss;
- return speedMax * timeForLossAllFuel;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement