EntityBase.cs 3.4 KB

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