EntityBase.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. speedY = jumpForce;
  34. jumpBuffer = 0;
  35. jumping = 0.5f;
  36. } else if (isCrouch) {
  37. speedY -= downA;
  38. if (speedY > 0) speedY = 0;
  39. }
  40. // 加速下落
  41. if (speedY < 0) speedY -= fallA;
  42. // 横向移动
  43. if (moveHorizontal != 0) {
  44. speedX = speedX * (1 - horizontalA) + movementSpeed * horizontalA * moveHorizontal;
  45. }
  46. speedX *= horizontalF;
  47. // 蹲下
  48. if (canCrouch) {
  49. if (isCrouch) {
  50. sprite.sprite = spriteDown;
  51. coll.size = new Vector2(coll.size.x, 0.4f);
  52. coll.offset = new Vector2(0, 0.3f);
  53. } else {
  54. sprite.sprite = spriteNormal;
  55. coll.size = new Vector2(coll.size.x, 1.2f);
  56. coll.offset = new Vector2(0, 0.7f);
  57. }
  58. }
  59. // 设置
  60. rb.velocity = new Vector2(speedX, speedY);
  61. if (towardsRight != (Mathf.Abs(speedX) > 0.01 ? speedX > 0 : towardsRight)) {
  62. // 反向
  63. towardsRight = !towardsRight;
  64. transform.DOScaleX(towardsRight ? 1 : -1, 0.1f);
  65. }
  66. }
  67. internal abstract bool isPressingLeft();
  68. internal abstract bool isPressingRight();
  69. internal abstract bool isPressDownJump();
  70. internal abstract bool isPressingCrouch();
  71. // 碰撞检测
  72. private void OnCollisionEnter2D(Collision2D collision) {
  73. if (collision.gameObject.CompareTag("Border")) OnHitBorder();
  74. if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("Player")) OnHitEnemy(collision);
  75. if (collision.gameObject.CompareTag("Player") && gameObject.CompareTag("Enemy")) OnHitEnemy(collision);
  76. }
  77. internal abstract void OnHitBorder();
  78. internal abstract void OnHitEnemy(Collision2D collision);
  79. }