Advertisement
Guest User

Motor old

a guest
Feb 17th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.06 KB | None | 0 0
  1. ///-----------------------------------------------------------------
  2. ///   Author : Sebastien RAYMONDAUD                    
  3. ///   Date   : 15/10/2019 16:55
  4. ///-----------------------------------------------------------------
  5.  
  6. using Convoy.DataScriptables.Locomotive;
  7. using Convoy.Train.Modules;
  8. using FMODUnity;
  9. using UnityEngine;
  10.  
  11. namespace Convoy.Locomotive
  12. {
  13.     public enum DirectionMove { Forward, Backward}
  14.     [SelectionBase]
  15.     public class Motor : MonoBehaviour
  16.     {
  17.         #region PROPERTIES
  18.         [SerializeField] private StudioEventEmitter m_motorSound = null;
  19.         [SerializeField] private StudioEventEmitter m_brakeSound = null;
  20.  
  21.         [SerializeField] private MotorData m_motorData = null;
  22.         [SerializeField] private FuelData m_fuelData = null;
  23.  
  24.         private float currentSpeed = 0;
  25.         private float additionnalSpeed = 0;
  26.         private bool isStop = false;
  27.         private bool isSlowdown = false;
  28.         private bool isBrake = false;
  29.         private bool isSlowByGrass = false;
  30.         private float maxSpeed = 0;
  31.  
  32.         private bool onAccelerate = false;
  33.         private bool onDecelerate = false;
  34.         private float elapsedTime = 0;
  35.         private bool waitOwner = false;
  36.  
  37.         private float timeLeftSlow = 0;
  38.         private float targetSpeedSlowGrass = 0;
  39.  
  40.         private string paramFMODMusicSpeed = "Vitesse";
  41.  
  42.         private DirectionMove directionMove = DirectionMove.Forward;
  43.  
  44.  
  45.         private System.Collections.Generic.Dictionary<int, float> draisines = new System.Collections.Generic.Dictionary<int, float>();
  46.         public float CurrentSpeed {
  47.             get {
  48.                 //float max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
  49.                 //float speed = Mathf.Min(max, (currentSpeed + additionnalSpeed));
  50.                 //
  51.                 //if (timeLeftSlow > 0)
  52.                 //{
  53.                 //    timeLeftSlow -= Time.deltaTime;
  54.                 //    slowSpeed = Mathf.Lerp(initialSpeed, targetSpeed, timeLeftSlow / initialTimeLeft);
  55.                 //    speed = Mathf.Min(speed, slowSpeed);
  56.                 //    Debug.LogError("=> " + speed + " [" + initialTimeLeft + " to " + targetSpeed + "]");
  57.                 //}
  58.                 //
  59.                 //return speed;
  60.                 return currentSpeed;
  61.             }
  62.         }
  63.         public float CurrentSpeedWithBrake { get { return Mathf.Min(isBrake ? m_motorData.SpeedBrake : maxSpeed, (currentSpeed + additionnalSpeed)); } }
  64.         public float MaxSpeed { get { return m_motorData.SpeedMaxWithFuel; } }
  65.         public bool IsBrake { get { return isBrake; } }
  66.         public float SpeedGoBack { get { return m_motorData.SpeedGoBack; } }
  67.         public bool WaitOwner { get { return waitOwner; } }
  68.         #endregion
  69.  
  70.         #region EVENTS
  71.         public delegate void Event_Movement(bool move);
  72.         public Event_Movement OnMovement = null;
  73.         public delegate void Event_UpdateSpeed(float currentSpeed);
  74.         public Event_UpdateSpeed OnUpdateSpeed = null;
  75.  
  76.         public delegate void Event_MusicChange(string param, float value);
  77.         public static Event_MusicChange OnUpdateMusic = null;
  78.  
  79.         public delegate void Event_NecessiteDraisine(bool necessite);
  80.         public Event_NecessiteDraisine OnNecessiteDraisine;
  81.         #endregion
  82.  
  83.         #region UNITY_METHODS
  84.         private void Awake()
  85.         {
  86.             m_motorData.SetMotor(this);
  87.         }
  88.  
  89.         private void Start()
  90.         {
  91.             if (m_motorSound != null)
  92.                 m_motorSound.Play();
  93.             m_fuelData.Init();
  94.             firstPos = transform.position;
  95.             isStop = true;
  96.  
  97.             onAccelerate = false;
  98.             onDecelerate = false;
  99.  
  100.             maxSpeed = m_motorData.SpeedMaxWithFuel;
  101.             Draisine.OnAddPower += Draisine_OnAddPower;
  102.         }
  103.  
  104.         private void Update ()
  105.         {
  106.             if (directionMove == DirectionMove.Backward)
  107.             {
  108.                 OnUpdateSpeed?.Invoke(-maxSpeed);
  109.                 OnUpdateMusic?.Invoke(paramFMODMusicSpeed, 0);
  110.                 return;
  111.             }
  112.  
  113.             UpdateSpeedValue();
  114.             Movement();
  115.             if (!waitOwner)
  116.             {
  117.                 if (additionnalSpeed > 0)
  118.                 {
  119.                     isSlowdown = false;
  120.                     isStop = false;
  121.                     OnMovement?.Invoke(true);
  122.  
  123.                     if (m_fuelData.Fuel > 0)
  124.                     {
  125.                         SwitchSpeed(true);
  126.                     }
  127.                 }
  128.                 else if(isStop && m_fuelData.Fuel > 0)
  129.                 {
  130.                     isStop = false;
  131.                     isSlowdown = false;
  132.                     OnMovement?.Invoke(true);
  133.                     SwitchSpeed(true);
  134.                 }
  135.                 else if (m_fuelData.Fuel <= 0 && !isSlowdown)
  136.                 {
  137.                     isSlowdown = true;
  138.                     SwitchSpeed(false);
  139.                 }
  140.             }
  141.  
  142.             if (isSlowdown && m_fuelData.Fuel > 0)
  143.             {
  144.                 isSlowdown = false;
  145.                 SwitchSpeed(true);
  146.             }
  147.  
  148.             if (isSlowdown && currentSpeed + additionnalSpeed <= 0)
  149.             {
  150.                 isStop = true;
  151.                 OnMovement?.Invoke(false);
  152.             }
  153.  
  154.             if (onAccelerate)
  155.             {
  156.                 Accelerate(true, m_motorData.AccelerationTime, m_motorData.AccelerationCurve);
  157.             }
  158.             else if (onDecelerate)
  159.             {
  160.                 Accelerate(false, m_motorData.DecelerationTime, m_motorData.DecelerationCurve);
  161.             }
  162.         }
  163.         #endregion
  164.  
  165.         #region METHODS
  166.         private void UpdateSpeedValue()
  167.         {
  168.             float max = maxSpeed;
  169.             if (isBrake)
  170.                 max = m_motorData.SpeedBrake;
  171.  
  172.             float speed = Mathf.Min(currentSpeed + additionnalSpeed, maxSpeed);
  173.            
  174.             if (isSlowByGrass)
  175.             {
  176.                 if (timeLeftSlow > 0)
  177.                 {
  178.                     timeLeftSlow -= Time.deltaTime;
  179.                     max = targetSpeedSlowGrass;
  180.                 }
  181.                 else
  182.                 {
  183.                     isSlowByGrass = false;
  184.                     max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
  185.                 }
  186.             }
  187.  
  188.             if (m_fuelData.Fuel <= 0)
  189.                 max = 0;
  190.  
  191.             if (onAccelerate || onDecelerate)
  192.                 max = currentSpeed;
  193.  
  194.             currentSpeed = Mathf.Lerp(speed, max, 3 * Time.deltaTime);
  195.         }
  196.  
  197.         public void StopConsumeFuel()
  198.         {
  199.             m_fuelData.consumeEnable = false;
  200.         }
  201.  
  202.         public void StopMovementAndWaitDraisine()
  203.         {
  204.             waitOwner = true;
  205.             isStop = true;
  206.  
  207.             onAccelerate = false;
  208.             onDecelerate = false;
  209.  
  210.             currentSpeed = 0;
  211.             additionnalSpeed = 0;
  212.             OnMovement?.Invoke(false);
  213.             OnNecessiteDraisine?.Invoke(true);
  214.         }
  215.  
  216.         public void SetDirection(DirectionMove directionMove)
  217.         {
  218.             this.directionMove = directionMove;
  219.  
  220.             if (directionMove == DirectionMove.Backward)
  221.             {
  222.                 isStop = true;
  223.                 if (m_motorSound != null)
  224.                     m_motorSound.SetParameter(paramFMODMusicSpeed, 0);
  225.             }
  226.         }
  227.  
  228.         public void Brake(bool brake = true)
  229.         {
  230.             isBrake = brake;
  231.         }
  232.  
  233.         public void LoseSpeed(float percentLost)
  234.         {
  235.             currentSpeed *= percentLost;
  236.             onAccelerate = m_fuelData.Fuel>0;        
  237.         }
  238.  
  239.         public void SlowSpeed(float time, float percentSpeed)
  240.         {
  241.             isSlowByGrass = true;
  242.             timeLeftSlow = time;
  243.             targetSpeedSlowGrass = maxSpeed * percentSpeed;
  244.         }
  245.  
  246.         private void Movement()
  247.         {
  248.             if (waitOwner)
  249.             {
  250.                 OnUpdateSpeed?.Invoke(0);
  251.                 return;
  252.             }
  253.             /*
  254.             float max = isBrake ? m_motorData.SpeedBrake : maxSpeed;
  255.             float speed = Mathf.Min(max, (currentSpeed + additionnalSpeed));
  256.  
  257.             if (timeLeftSlow > 0)
  258.             {
  259.                 timeLeftSlow -= Time.deltaTime;
  260.                 speed = speed * percentSpeed;
  261.             }
  262.             */
  263.             OnUpdateSpeed?.Invoke(CurrentSpeed);
  264.             transform.Translate(transform.forward * CurrentSpeed * Time.smoothDeltaTime, Space.World);
  265.  
  266.             if (m_motorSound != null)
  267.                 m_motorSound.SetParameter(paramFMODMusicSpeed, CurrentSpeed / m_motorData.SpeedMaxWithFuel);
  268.  
  269.             OnUpdateMusic?.Invoke(paramFMODMusicSpeed, (CurrentSpeed / m_motorData.SpeedMaxWithFuel) * 2);
  270.         }
  271.  
  272.         private void SwitchSpeed(bool accelerate)
  273.         {
  274.             elapsedTime = 0;
  275.             onAccelerate = accelerate;
  276.             onDecelerate = !accelerate;
  277.         }
  278.  
  279.         private void Accelerate(bool move, float t, AnimationCurve curve)
  280.         {
  281.             if (move)
  282.                 elapsedTime = (currentSpeed / maxSpeed) * t;
  283.             else
  284.                 elapsedTime = (1 - (currentSpeed / maxSpeed)) * t;
  285.  
  286.  
  287.             elapsedTime += Time.deltaTime;
  288.             float ratio = elapsedTime / t;
  289.  
  290.             currentSpeed = curve.Evaluate(ratio) * (maxSpeed + additionnalSpeed);
  291.             currentSpeed = Mathf.Clamp(currentSpeed, 0, maxSpeed);
  292.  
  293.             if (ratio >= 1)
  294.             {
  295.                 onAccelerate = false;
  296.                 onDecelerate = false;
  297.             }
  298.         }
  299.  
  300.         public void StopSound()
  301.         {
  302.             if (m_motorSound != null)
  303.             {
  304.                 m_motorSound.SetParameter(paramFMODMusicSpeed, 0);
  305.                 m_motorSound.Stop();
  306.             }
  307.         }
  308.  
  309.         public void Draisine_OnAddPower(int id, float powerAdd)
  310.         {
  311.             if (!draisines.ContainsKey(id))
  312.                 draisines.Add(id, powerAdd);
  313.  
  314.             draisines[id] = powerAdd;
  315.  
  316.             additionnalSpeed = 0;
  317.             foreach (var item in draisines)
  318.             {
  319.                 additionnalSpeed += item.Value;
  320.             }
  321.  
  322.             additionnalSpeed = Mathf.Clamp(additionnalSpeed, 0, m_motorData.SpeedMaxWithFuel);
  323.         }
  324.  
  325.         public void StopWaitOwner()
  326.         {
  327.             waitOwner = false;
  328.             OnNecessiteDraisine?.Invoke(false);
  329.             m_fuelData.consumeEnable = true;
  330.             Debug.Log("Allez on repart chacal");
  331.         }
  332.  
  333.         public void Death()
  334.         {
  335.             currentSpeed = 0;
  336.             additionnalSpeed = 0;
  337.         }
  338.         #endregion
  339.  
  340.         #region GIZMOS
  341.         private Vector3 firstPos;
  342.         private void OnDrawGizmos()
  343.         {
  344.             float distanceAcceleration = CalculateDistanceCurve(m_motorData.AccelerationTime,
  345.                                                                 m_motorData.SpeedMaxWithFuel,
  346.                                                                 m_motorData.AccelerationCurve);
  347.  
  348.             float distanceDeceleration = CalculateDistanceCurve(m_motorData.DecelerationTime,
  349.                                                                 m_motorData.SpeedMaxWithFuel,
  350.                                                                 m_motorData.DecelerationCurve);
  351.  
  352.             float distanceWithMaxFuel = CalculateDistanceWithFuel(m_fuelData.FuelMax - (m_fuelData.FuelLossPerSecond * m_motorData.AccelerationTime),
  353.                                                                     m_fuelData.FuelLossPerSecond,
  354.                                                                     m_motorData.SpeedMaxWithFuel);
  355.  
  356.             Vector3 posStartLine = firstPos + Vector3.up * 2;
  357.             Vector3 nextPos = posStartLine + (transform.forward * distanceAcceleration);
  358.             Gizmos.color = Color.red;
  359.             Gizmos.DrawLine(posStartLine, nextPos);
  360.  
  361.             posStartLine = nextPos;
  362.             nextPos = posStartLine + (transform.forward * distanceWithMaxFuel);
  363.             Gizmos.color = Color.blue;
  364.             Gizmos.DrawLine(posStartLine, nextPos);
  365.  
  366.             posStartLine = nextPos;
  367.             nextPos = posStartLine + (transform.forward * distanceDeceleration);
  368.             Gizmos.color = Color.green;
  369.             Gizmos.DrawLine(posStartLine, nextPos);
  370.         }
  371.  
  372.         private float CalculateDistanceCurve(float time, float speedMax, AnimationCurve curve, float initialValueCurve = 0)
  373.         {
  374.             float delta = 1 / 1000f;
  375.             float r = initialValueCurve;
  376.             float dist = 0;
  377.             while (r < 1)
  378.             {
  379.                 dist += ((curve.Evaluate(r) * speedMax) * delta) * time;
  380.                 r += delta;
  381.             }
  382.  
  383.             return dist;
  384.         }
  385.  
  386.         private float CalculateDistanceWithFuel(float fuel, float fuelLoss, float speedMax)
  387.         {
  388.             float timeForLossAllFuel = fuel / fuelLoss;
  389.  
  390.             return speedMax * timeForLossAllFuel;
  391.         }
  392.         #endregion
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement