EntityBase.cs 3.0 KB

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