EntityBase.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. OnUpdate();
  22. // 获取当前情况
  23. float speedX = rb.velocity.x, speedY = rb.velocity.y;
  24. bool isCrouch = isPressingCrouch();
  25. bool 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. speedY = jumpForce;
  36. jumpBuffer = 0;
  37. jumping = 0.5f;
  38. } else if (isCrouch) {
  39. speedY -= downA;
  40. }
  41. // 加速下落
  42. if (speedY < 0) speedY -= fallA;
  43. // 横向移动
  44. if (moveHorizontal != 0) {
  45. speedX = speedX * (1 - horizontalA) + movementSpeed * horizontalA * moveHorizontal;
  46. }
  47. speedX *= horizontalF;
  48. // 蹲下
  49. if (canCrouch) {
  50. if (isCrouch) {
  51. sprite.sprite = spriteDown;
  52. coll.size = new Vector2(coll.size.x, 0.4f);
  53. coll.offset = new Vector2(0, 0.3f);
  54. } else {
  55. sprite.sprite = spriteNormal;
  56. coll.size = new Vector2(coll.size.x, 1.2f);
  57. coll.offset = new Vector2(0, 0.7f);
  58. }
  59. }
  60. // 设置
  61. rb.velocity = new Vector2(speedX, speedY);
  62. if (towardsRight != (Mathf.Abs(speedX) > 0.01 ? speedX > 0 : towardsRight)) {
  63. // 反向
  64. towardsRight = !towardsRight;
  65. transform.DOScaleX(towardsRight ? 1 : -1, 0.1f);
  66. }
  67. }
  68. internal abstract bool isPressingLeft();
  69. internal abstract bool isPressingRight();
  70. internal abstract bool isPressDownJump();
  71. internal abstract bool isPressingCrouch();
  72. internal virtual void OnUpdate() {}
  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. }