RedMoney.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class RedMoney : BaseBehaviour
  5. {
  6. private float deltaTime
  7. {
  8. get
  9. {
  10. return Time.time - this.startTime;
  11. }
  12. }
  13. private void Awake()
  14. {
  15. this._rigidbody2D = base.GetComponent<Rigidbody2D>();
  16. }
  17. private void Start()
  18. {
  19. this.player = R.Player.Transform;
  20. }
  21. private void FixedUpdate()
  22. {
  23. if (!this.isOnGround)
  24. {
  25. RaycastHit2D raycastHit2D = Physics2D.Raycast(base.transform.position, Vector2.down, 10f, LayerManager.GroundMask);
  26. if (raycastHit2D.distance < 0.5f || base.transform.position.y < raycastHit2D.point.y)
  27. {
  28. this._rigidbody2D.gravityScale = 0f;
  29. this._rigidbody2D.velocity = Vector2.zero;
  30. this.isOnGround = true;
  31. }
  32. }
  33. }
  34. private void OnTriggerStay2D(Collider2D other)
  35. {
  36. if (!this.hasFly && other.name == "PlayerHurtBox" && this.coinValue != -1)
  37. {
  38. this._rigidbody2D.gravityScale = 0f;
  39. this._rigidbody2D.velocity = Vector2.zero;
  40. R.Audio.PlayEffect(193, new Vector3?(this.player.position));
  41. this.hasFly = true;
  42. this.startTime = Time.time;
  43. base.StartCoroutine(this.Fly());
  44. }
  45. }
  46. private IEnumerator Fly()
  47. {
  48. float randomX = (float)((UnityEngine.Random.Range(-1f, 1f) <= 0f) ? -1 : 1) * UnityEngine.Random.Range(-2f, -1f);
  49. float randomY = ((UnityEngine.Random.Range(-1f, 1f) <= 0f) ? -1f : 0.5f) * UnityEngine.Random.Range(-1f, -0.5f);
  50. Vector2 randomFore = new Vector2(randomX, randomY);
  51. Vector3 moneyPos = base.transform.position;
  52. while (this.deltaTime < this.flyingTime && Vector3.Distance(moneyPos, base.transform.position) < 5f)
  53. {
  54. this._rigidbody2D.AddForce(randomFore * 25f);
  55. yield return new WaitForFixedUpdate();
  56. }
  57. this._rigidbody2D.AddForce(new Vector2(0f, 0f));
  58. yield return new WaitForSeconds(0.3f);
  59. for (;;)
  60. {
  61. base.transform.position = Vector3.MoveTowards(base.transform.position, this.player.position + this.repairPosition, 0.7f * this.speed * this.deltaTime);
  62. if (Vector3.Distance(base.transform.position, this.player.position + this.repairPosition) < 0.2f)
  63. {
  64. R.Equipment.CoinNum += this.coinValue;
  65. R.Effect.Generate(159, this.player, this.repairPosition, default(Vector3), default(Vector3), true);
  66. UnityEngine.Object.Destroy(base.gameObject);
  67. }
  68. yield return new WaitForFixedUpdate();
  69. }
  70. yield break;
  71. }
  72. private bool hasFly;
  73. private bool isOnGround;
  74. public float speed = 1f;
  75. public int coinValue = -1;
  76. public float flyingTime = 0.1f;
  77. private float startTime;
  78. private Transform player;
  79. private Vector3 repairPosition = new Vector3(0f, 1.2f, -0.1f);
  80. private Rigidbody2D _rigidbody2D;
  81. }