EntityBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Cinemachine;
  2. using DG.Tweening;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public abstract class EntityBase : MonoBehaviour {
  7. internal Rigidbody2D rb;
  8. internal SpriteRenderer sprite;
  9. internal BoxCollider2D coll;
  10. public Sprite spriteNormal, spriteDown;
  11. void Start() {
  12. rb = GetComponent<Rigidbody2D>();
  13. sprite = GetComponent<SpriteRenderer>();
  14. coll = GetComponent<BoxCollider2D>();
  15. }
  16. public bool canCrouch = false;
  17. public float movementSpeed = 10f, jumpForce = 20f, horizontalF = 0.9f, horizontalA = 0.3f, jumpThreshold = 0.1f, downA = 0.3f, fallA = 0.1f, filpSpeed = 0.3f;
  18. public float jumpBufferTime = 0.2f, jumpWolfTime = 0.2f; internal float jumpBuffer = 0f, jumpWolf = 0f, jumping = 0f;
  19. internal bool towardsRight = true, isLand = false;
  20. void Update() {
  21. OnUpdate();
  22. // 获取当前情况
  23. float speedX = rb.velocity.x, speedY = rb.velocity.y;
  24. bool isCrouch = isPressingCrouch();
  25. isLand = Physics2D.OverlapCircle(transform.position + Vector3.left * 0.1f, jumpThreshold, LayerMask.GetMask("Tilemap")) ||
  26. Physics2D.OverlapCircle(transform.position + Vector3.left * -0.1f, jumpThreshold, LayerMask.GetMask("Tilemap"));
  27. float moveHorizontal = (isCrouch && isLand ? 0.35f : 1) * // 下蹲减速
  28. (isPressingLeft() == isPressingRight() ? 0 : // 没有同时按住左右
  29. (isPressingLeft() ? -1 : 1)); // 获取方向
  30. // 判断跳跃
  31. jumpBuffer -= Time.deltaTime; jumpWolf -= Time.deltaTime; jumping -= Time.deltaTime;
  32. if (isPressDownJump()) jumpBuffer = jumpBufferTime;
  33. if (isLand) jumpWolf = jumpWolfTime;
  34. if (jumpBuffer > 0 && jumpWolf > 0 && jumping < 0) {
  35. // 播放音效
  36. AudioM.Play("jump");
  37. speedY = jumpForce;
  38. jumpBuffer = 0;
  39. jumping = 0.5f;
  40. } else if (isCrouch) {
  41. speedY -= downA;
  42. }
  43. // 加速下落
  44. if (speedY < 0) speedY -= fallA;
  45. // 横向移动
  46. if (moveHorizontal != 0) {
  47. speedX = speedX * (1 - horizontalA) + movementSpeed * horizontalA * moveHorizontal;
  48. }
  49. speedX *= horizontalF;
  50. // 蹲下
  51. if (canCrouch) {
  52. if (isCrouch) {
  53. sprite.sprite = spriteDown;
  54. coll.size = new Vector2(coll.size.x, 0.4f);
  55. coll.offset = new Vector2(0, 0.3f);
  56. } else {
  57. sprite.sprite = spriteNormal;
  58. coll.size = new Vector2(coll.size.x, 1.2f);
  59. coll.offset = new Vector2(0, 0.7f);
  60. }
  61. }
  62. // 设置
  63. rb.velocity = new Vector2(speedX, speedY);
  64. if (towardsRight != (Mathf.Abs(speedX) > filpSpeed ? speedX > 0 : towardsRight)) {
  65. // 反向
  66. towardsRight = !towardsRight;
  67. transform.DOScaleX(towardsRight ? 1 : -1, 0.1f);
  68. }
  69. }
  70. internal abstract bool isPressingLeft();
  71. internal abstract bool isPressingRight();
  72. internal abstract bool isPressDownJump();
  73. internal abstract bool isPressingCrouch();
  74. internal virtual void OnUpdate() {}
  75. // 碰撞检测
  76. private void OnCollisionEnter2D(Collision2D collision) {
  77. if (collision.gameObject.CompareTag("Border")) OnHitBorder();
  78. if (collision.gameObject.CompareTag("Bullet")) OnHitBullet();
  79. if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("Player")) OnHitEnemy(collision);
  80. if (collision.gameObject.CompareTag("Player") && gameObject.CompareTag("Enemy")) OnHitEnemy(collision);
  81. }
  82. internal abstract void OnHitBorder();
  83. internal virtual void OnHitBullet() {}
  84. internal abstract void OnHitEnemy(Collision2D collision);
  85. }