ShadowControl.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using ExtensionMethods;
  3. using UnityEngine;
  4. public class ShadowControl : BaseBehaviour
  5. {
  6. private void Update()
  7. {
  8. Vector3 position = this._target.position;
  9. if (!this._targetGameObject.activeSelf || this.EnemyDie())
  10. {
  11. UnityEngine.Object.Destroy(base.gameObject);
  12. return;
  13. }
  14. this.GetGroundHeight(position);
  15. this.SetShadowTransform(position);
  16. this.SetShadowColor(position);
  17. }
  18. public void SetTarget(Transform target)
  19. {
  20. this._target = target;
  21. this._targetGameObject = target.gameObject;
  22. BoxCollider2D component = target.GetComponent<BoxCollider2D>();
  23. this._eAttribute = target.GetComponent<EnemyAttribute>();
  24. this._isEnemy = (this._eAttribute != null);
  25. this._offset = ((!(component == null)) ? component.offset : Vector2.zero);
  26. this._size = ((!(component == null)) ? component.size : Vector2.one);
  27. this._shadowColor = this.sprite.color;
  28. this._layerValue = this.layer.value;
  29. }
  30. private void GetGroundHeight(Vector3 targetPos)
  31. {
  32. RaycastHit2D raycastHit2D = Physics2D.Raycast(new Vector2(targetPos.x + this._offset.x + this._size.x / 3f, targetPos.y), Vector2.down, 100f, this._layerValue);
  33. RaycastHit2D raycastHit2D2 = Physics2D.Raycast(new Vector2(targetPos.x + this._offset.x - this._size.x / 3f, targetPos.y), Vector2.down, 100f, this._layerValue);
  34. this._noGround = (raycastHit2D.collider == null || raycastHit2D2.collider == null);
  35. this._groundY = Mathf.Max(raycastHit2D2.point.y, raycastHit2D.point.y);
  36. }
  37. private void SetShadowTransform(Vector3 targetPosition)
  38. {
  39. base.transform.position = new Vector3(targetPosition.x, this._groundY, targetPosition.z);
  40. if (this._noGround)
  41. {
  42. base.transform.localScale = new Vector3(0f, 0f, 1f);
  43. return;
  44. }
  45. float num = Mathf.Lerp(1f, 0f, (targetPosition.y - this._groundY) / 6f);
  46. base.transform.localScale = new Vector3(num, num, 1f);
  47. }
  48. private void SetShadowColor(Vector3 targetPosition)
  49. {
  50. Color color = this._shadowColor.SetAlpha(Mathf.Lerp(0.5f, 0f, (targetPosition.y - this._groundY) / 8f));
  51. if (Math.Abs(this._shadowColor.a - color.a) > 1.401298E-45f)
  52. {
  53. this._shadowColor = color;
  54. this.sprite.color = color;
  55. }
  56. }
  57. private bool EnemyDie()
  58. {
  59. return this._isEnemy && this._eAttribute.currentHp == 0;
  60. }
  61. private float _groundY;
  62. [SerializeField]
  63. private SpriteRenderer sprite;
  64. private Transform _target;
  65. private GameObject _targetGameObject;
  66. private EnemyAttribute _eAttribute;
  67. private bool _isEnemy;
  68. public LayerMask layer;
  69. private bool _noGround;
  70. private Vector2 _offset;
  71. private Vector2 _size;
  72. private Color _shadowColor;
  73. private int _layerValue;
  74. }