Player.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Cinemachine;
  2. using DG.Tweening;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. public class Player : EntityBase {
  8. internal override bool isPressingCrouch() => InputM.GetKeyEvent(InputM.KeyType.Crouch);
  9. internal override bool isPressDownJump() => InputM.GetKeyEvent(InputM.KeyType.Jump);
  10. internal override bool isPressingLeft() => InputM.GetKeyEvent(InputM.KeyType.Left);
  11. internal override bool isPressingRight() => InputM.GetKeyEvent(InputM.KeyType.Right);
  12. // 生命
  13. public RectTransform[] hearts;
  14. private int _hp = 3;
  15. private int hp {
  16. get { return _hp; }
  17. set {
  18. if (_hp == value) return;
  19. _hp = value;
  20. if (_hp == 0) _hp = 3;
  21. RefreshHearts();
  22. }
  23. }
  24. public float knockbackForceH = 30f, knockbackForceV = 15f;
  25. internal override void OnHitBorder() {
  26. Hurt();
  27. transform.position = new Vector3(0, 8, 0);
  28. }
  29. internal override void OnHitEnemy(Collision2D collision) {
  30. Hurt();
  31. int face = collision.transform.position.x < transform.position.x ? 1 : -1;
  32. rb.velocity = new Vector2(face * knockbackForceH, knockbackForceV);
  33. }
  34. public void Hurt() {
  35. hp--;
  36. AudioM.Play("hurt");
  37. rb.velocity = Vector2.zero;
  38. DOTween.Kill("ScreenShake");
  39. Camera.current.DOShakePosition(0.3f, 0.3f, 20).SetId("ScreenShake");
  40. }
  41. // 治疗
  42. public float healTime = 4f; private float currentHealTime = 0f;
  43. private void RefreshHearts() {
  44. float totalHp = Mathf.Clamp(hp + Mathf.Pow(currentHealTime / healTime, 7f) * 0.7f + currentHealTime / healTime * 0.3f, 0, 3);
  45. for (int i = 0; i < 3; i++) {
  46. hearts[i].localScale = Vector3.one * Mathf.Clamp(totalHp - i, 0, 1);
  47. }
  48. }
  49. // 射击
  50. public int maxBullet = 20, currentBullet = 20;
  51. public float shootDelay = 0.15f; private float currentShootDelay = 0f;
  52. public float reloadDelay = 1.5f; private float currentReloadDelay = 0f;
  53. public float bulletSpeed = 50f, bulletKnockback = 1f;
  54. public GameObject bullet; public TMPro.TextMeshProUGUI textBullet;
  55. internal override void OnUpdate() {
  56. // 治疗
  57. if (isPressingCrouch() && hp < 3 && isLand) {
  58. currentHealTime += Time.deltaTime;
  59. if (currentHealTime > healTime) {
  60. currentHealTime -= healTime;
  61. hp++;
  62. }
  63. RefreshHearts();
  64. } else {
  65. currentHealTime = 0f;
  66. }
  67. // 装填
  68. if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f) {
  69. // TODO: 装填音效(总时长为 reloadDelay,看 Unity 里的 Player 右边栏,这里的数字可能是过期的)
  70. currentReloadDelay = reloadDelay;
  71. } else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime) {
  72. currentReloadDelay = 0f; currentBullet = maxBullet;
  73. } else if (currentReloadDelay > 0) {
  74. currentReloadDelay -= Time.deltaTime;
  75. }
  76. // 开火
  77. if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f) {
  78. // TODO: 开火音效
  79. currentBullet--; currentShootDelay = shootDelay;
  80. Vector2 fromPos = transform.position + Vector3.up * 0.55f;
  81. Vector2 toPos = Camera.allCameras[0].ScreenToWorldPoint(Input.mousePosition);
  82. Vector2 shootVector = (toPos - fromPos).normalized;
  83. GameObject newBullet = Instantiate(bullet, fromPos, Quaternion.identity);
  84. newBullet.GetComponent<Rigidbody2D>().velocity = bulletSpeed * shootVector;
  85. newBullet.transform.eulerAngles = (shootVector.x > 0 ? -1 : 1) * Vector2.Angle(shootVector, Vector2.up) * Vector3.forward;
  86. // 后座力
  87. rb.velocity += bulletKnockback * -shootVector;
  88. } else if (currentShootDelay > 0) {
  89. currentShootDelay -= Time.deltaTime;
  90. }
  91. // 更新 UI
  92. textBullet.text = currentReloadDelay > 0f ?
  93. "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
  94. "".PadLeft(currentBullet, "|".ToCharArray().First());
  95. textBullet.color = currentReloadDelay > 0f ? new Color(0.6f, 0.6f, 0.6f) : new Color(1, 1, 0.5f);
  96. }
  97. }