MoveAreaLimit.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using GameWorld;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody2D))]
  5. public class MoveAreaLimit : BaseBehaviour
  6. {
  7. private void Start()
  8. {
  9. if (base.GetComponent<PlayerAction>() != null)
  10. {
  11. this.type = MoveAreaLimit.LimitType.Player;
  12. this.bodySize = base.GetComponent<Collider2D>().bounds.size;
  13. }
  14. this.body = base.GetComponent<Rigidbody2D>();
  15. }
  16. private void Update()
  17. {
  18. if (this._frameCount < 1)
  19. {
  20. this._frameCount++;
  21. return;
  22. }
  23. this._frameCount = 0;
  24. float num = 0.05f;
  25. Vector3 position = base.transform.position;
  26. Vector3 v = this.body.velocity;
  27. if (this.type == MoveAreaLimit.LimitType.Player)
  28. {
  29. float num2 = GameArea.PlayerRange.xMin + this.bodySize.x / 2f;
  30. float num3 = GameArea.PlayerRange.xMax - this.bodySize.x / 2f;
  31. if (position.x > num3 + num || position.x < num2 - num)
  32. {
  33. EventManager.PostEvent<MoveAreaLimit, HitWallArgs>("HitWall", this, new HitWallArgs(base.gameObject));
  34. v.x = 0f;
  35. this.body.velocity = v;
  36. this.body.position = new Vector2(Mathf.Clamp(position.x, num2, num3), Mathf.Clamp(position.y, GameArea.PlayerRange.yMin, GameArea.PlayerRange.yMax));
  37. }
  38. if (position.y > GameArea.PlayerRange.yMax + num)
  39. {
  40. v.y = 0f;
  41. this.body.velocity = v;
  42. this.body.position = new Vector2(Mathf.Clamp(position.x, num2, num3), Mathf.Clamp(position.y, GameArea.PlayerRange.yMin, GameArea.PlayerRange.yMax));
  43. }
  44. }
  45. float yMax = GameArea.MapRange.yMax;
  46. float yMin = GameArea.MapRange.yMin;
  47. Vector3 position2 = base.transform.position;
  48. if (yMin > position2.y)
  49. {
  50. position2.y = yMax;
  51. base.transform.position = position2;
  52. Log.Error(base.transform.name + "击穿了地面");
  53. }
  54. }
  55. public MoveAreaLimit.LimitType type = MoveAreaLimit.LimitType.Enemy;
  56. public Vector2 bodySize = new Vector2(0.5f, 0f);
  57. public int battleZoneId = 1;
  58. private EnemyAttribute eAttr;
  59. private Rigidbody2D body;
  60. private const int Freq = 1;
  61. private int _frameCount;
  62. public enum LimitType
  63. {
  64. Player,
  65. Enemy,
  66. Map
  67. }
  68. }