EntityBase.cs 3.8 KB

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