EngineController.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Assets.Scripts.Engine
  4. {
  5. internal sealed class EngineController : MonoBehaviour
  6. {
  7. private const float SpeedIntencity = 1;
  8. public ParticleSystem[] EngineParticleSystems;
  9. public float[] MaxParticleSystemsAlpha;
  10. private readonly List<Material> _enginePsMaterials = new List<Material>();
  11. private float _speed;
  12. private bool _isButtonHold;
  13. private void Awake()
  14. {
  15. foreach (var engineParticleSystem in EngineParticleSystems)
  16. _enginePsMaterials.Add(engineParticleSystem.GetComponent<Renderer>().material);
  17. UpdateColorBySpeed();
  18. }
  19. private void Update()
  20. {
  21. var speed = SpeedIntencity * Time.deltaTime;
  22. if (Input.GetKeyDown(KeyCode.W))
  23. _isButtonHold = true;
  24. else if (Input.GetKeyUp(KeyCode.W))
  25. _isButtonHold = false;
  26. if (_isButtonHold)
  27. _speed += speed;
  28. else
  29. _speed -= speed;
  30. _speed = Mathf.Clamp01(_speed);
  31. UpdateColorBySpeed();
  32. }
  33. private void UpdateColorBySpeed()
  34. {
  35. for (int i = 0; i < _enginePsMaterials.Count; i++)
  36. {
  37. var tintColor = _enginePsMaterials[i].GetColor("_TintColor");
  38. tintColor.a = Mathf.Clamp(_speed, 0, MaxParticleSystemsAlpha[i]);
  39. // ReSharper disable once CompareOfFloatsByEqualityOperator
  40. // sparks
  41. if (_enginePsMaterials[i].HasProperty("_ColorEdge"))
  42. {
  43. tintColor.r = Mathf.Clamp(_speed, 0, MaxParticleSystemsAlpha[i]);
  44. tintColor.g = Mathf.Clamp(_speed, 0, MaxParticleSystemsAlpha[i]);
  45. tintColor.b = Mathf.Clamp(_speed, 0, MaxParticleSystemsAlpha[i]);
  46. }
  47. _enginePsMaterials[i].SetColor("_TintColor", tintColor);
  48. }
  49. }
  50. }
  51. }