Player.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. public static Player instance;
  9. private void Awake() {
  10. instance = this;
  11. }
  12. internal override bool isPressingCrouch() => InputM.GetKeyEvent(InputM.KeyType.Crouch);
  13. internal override bool isPressDownJump() => InputM.GetKeyEvent(InputM.KeyType.Jump);
  14. internal override bool isPressingLeft() => InputM.GetKeyEvent(InputM.KeyType.Left);
  15. internal override bool isPressingRight() => InputM.GetKeyEvent(InputM.KeyType.Right);
  16. // 生命
  17. public RectTransform[] hearts;
  18. private static int _hp = 3;
  19. public static int hp {
  20. get { return _hp; }
  21. set {
  22. if (_hp == value) return;
  23. _hp = value;
  24. if (_hp == 0) _hp = 3;
  25. instance.RefreshHearts();
  26. }
  27. }
  28. public float knockbackForceH = 30f, knockbackForceV = 15f;
  29. internal override void OnHitBorder() {
  30. Hurt();
  31. resistanceTime = resistanceOnHurt * 1.5f;
  32. transform.position = new Vector3(-6, 10, 0);
  33. }
  34. internal override void OnHitEnemy(Collision2D collision) {
  35. if (Hurt()) {
  36. int face = collision.transform.position.x < transform.position.x ? 1 : -1;
  37. rb.velocity = new Vector2(face * knockbackForceH, knockbackForceV);
  38. }
  39. }
  40. public float resistanceOnHurt = 1f; private float resistanceTime = 0f;
  41. public bool Hurt() {
  42. if (resistanceTime <= 0.001f) {
  43. resistanceTime = resistanceOnHurt;
  44. hp--;
  45. AudioM.Play("hurt");
  46. rb.velocity = Vector2.zero;
  47. DOTween.Kill("ScreenShake");
  48. Camera.current.DOShakePosition(0.4f, 0.5f, 25).SetId("ScreenShake");
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54. // 治疗
  55. public float healTime = 4f; private float currentHealTime = 0f;
  56. private void RefreshHearts() {
  57. float totalHp = Mathf.Clamp(hp + Mathf.Pow(currentHealTime / healTime, 7f) * 0.7f + currentHealTime / healTime * 0.3f, 0, 3);
  58. for (int i = 0; i < 3; i++) {
  59. hearts[i].localScale = Vector3.one * Mathf.Clamp(totalHp - i, 0, 1);
  60. }
  61. }
  62. // 射击
  63. public int maxBullet = 20, currentBullet = 20;
  64. public float shootDelay = 0.15f; private float currentShootDelay = 0f;
  65. public float reloadDelay = 1.5f; private float currentReloadDelay = 0f;
  66. public float bulletSpeed = 50f, bulletKnockback = 1f;
  67. public GameObject bullet; public TMPro.TextMeshProUGUI textBullet;
  68. internal override void OnUpdate() {
  69. // 无敌
  70. if (resistanceTime > 0f) resistanceTime -= Time.deltaTime;
  71. // 治疗
  72. if (isPressingCrouch() && hp < 3 && isLand) {
  73. currentHealTime += Time.deltaTime;
  74. if (currentHealTime > healTime) {
  75. currentHealTime -= healTime;
  76. hp++;
  77. }
  78. RefreshHearts();
  79. } else if (currentHealTime != 0f) {
  80. currentHealTime = 0f;
  81. RefreshHearts();
  82. }
  83. // 装填
  84. if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f) {
  85. AudioM.Play("reload");
  86. currentReloadDelay = reloadDelay;
  87. } else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime) {
  88. currentReloadDelay = 0f; currentBullet = maxBullet;
  89. } else if (currentReloadDelay > 0) {
  90. currentReloadDelay -= Time.deltaTime;
  91. }
  92. // 开火
  93. if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f) {
  94. AudioM.Play("fire");
  95. currentBullet--; currentShootDelay = shootDelay;
  96. Vector2 fromPos = transform.position + Vector3.up * 0.55f;
  97. Vector2 toPos = AspectUtility.cam.ScreenToWorldPoint(Input.mousePosition);
  98. Vector2 shootVector = (toPos - fromPos).normalized;
  99. GameObject newBullet = Instantiate(bullet, fromPos, Quaternion.identity);
  100. newBullet.GetComponent<Rigidbody2D>().velocity = bulletSpeed * shootVector;
  101. newBullet.transform.eulerAngles = (shootVector.x > 0 ? -1 : 1) * Vector2.Angle(shootVector, Vector2.up) * Vector3.forward;
  102. // 后座力
  103. rb.velocity += bulletKnockback * -shootVector;
  104. } else if (currentShootDelay > 0) {
  105. currentShootDelay -= Time.deltaTime;
  106. }
  107. // 更新 UI
  108. textBullet.text = currentReloadDelay > 0f ?
  109. "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
  110. "".PadLeft(currentBullet, "|".ToCharArray().First());
  111. textBullet.color = currentReloadDelay > 0f ? new Color(0.6f, 0.6f, 0.6f) : new Color(0.9686275f, 0.5568628f, 0.1098039f);
  112. }
  113. }