EntityBase.cs 3.6 KB

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