AreaLight2D.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using ExtensionMethods;
  4. using UnityEngine;
  5. public class AreaLight2D : BaseBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this._light = base.GetComponent<Light>();
  10. this.InitAreaLight();
  11. }
  12. private void Update()
  13. {
  14. this.UpdateLight();
  15. }
  16. private void InitAreaLight()
  17. {
  18. if (base.transform.childCount != this._lightPoints.Count)
  19. {
  20. this._lightPoints.Clear();
  21. foreach (Transform transform in base.transform.GetChildren())
  22. {
  23. this._lightPoints.Add(transform.GetComponent<LightPoint>());
  24. }
  25. this._lightPoints.Sort((LightPoint l1, LightPoint l2) => l1.transform.position.x.CompareTo(l2.transform.position.x));
  26. }
  27. }
  28. private void UpdateLight()
  29. {
  30. if (R.Player != null)
  31. {
  32. float x = R.Player.Transform.position.x;
  33. int num = this.PlayerAt(x);
  34. if (num == 0)
  35. {
  36. LightPoint lightPoint = this._lightPoints[0];
  37. this._light.color = lightPoint.color;
  38. this._light.intensity = lightPoint.intensity;
  39. }
  40. else if (num == this._lightPoints.Count)
  41. {
  42. LightPoint lightPoint2 = this._lightPoints[num - 1];
  43. this._light.color = lightPoint2.color;
  44. this._light.intensity = lightPoint2.intensity;
  45. }
  46. else
  47. {
  48. LightPoint lightPoint3 = this._lightPoints[num - 1];
  49. LightPoint lightPoint4 = this._lightPoints[num];
  50. this._light.color = Color.Lerp(lightPoint4.color, lightPoint3.color, this.getLerpPercent(lightPoint4.Position.x, lightPoint3.Position.x, x));
  51. this._light.intensity = Mathf.Lerp(lightPoint4.intensity, lightPoint3.intensity, this.getLerpPercent(lightPoint4.Position.x, lightPoint3.Position.x, x));
  52. }
  53. }
  54. else
  55. {
  56. LightPoint lightPoint5 = this._lightPoints[0];
  57. this._light.color = lightPoint5.color;
  58. this._light.intensity = lightPoint5.intensity;
  59. }
  60. this._light.intensity -= 1f - this._defaultIntensity;
  61. }
  62. private int PlayerAt(float playerPositionX)
  63. {
  64. for (int i = 0; i < this._lightPoints.Count; i++)
  65. {
  66. if (playerPositionX < this._lightPoints[i].Position.x)
  67. {
  68. return i;
  69. }
  70. }
  71. return this._lightPoints.Count;
  72. }
  73. private float getLerpPercent(float x1, float x2, float current)
  74. {
  75. return (current - x1) / (x2 - x1);
  76. }
  77. [SerializeField]
  78. [Range(0f, 2f)]
  79. private float _defaultIntensity = 0.6f;
  80. [SerializeField]
  81. private List<LightPoint> _lightPoints = new List<LightPoint>();
  82. private Light _light;
  83. }