Player.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. _hp = 3;
  12. }
  13. internal override bool isPressingCrouch() => InputM.GetKeyEvent(InputM.KeyType.Crouch);
  14. internal override bool isPressDownJump() => InputM.GetKeyEvent(InputM.KeyType.Jump);
  15. internal override bool isPressingLeft() => InputM.GetKeyEvent(InputM.KeyType.Left);
  16. internal override bool isPressingRight() => InputM.GetKeyEvent(InputM.KeyType.Right);
  17. // 生命
  18. public RectTransform[] hearts;
  19. private static int _hp;
  20. public static int hp {
  21. get { return _hp; }
  22. set {
  23. if (_hp == value) return;
  24. _hp = value;
  25. instance.RefreshHearts();
  26. }
  27. }
  28. public float knockbackForceH = 30f, knockbackForceV = 15f;
  29. internal override void OnHitBorder() {
  30. Hurt();
  31. resistanceTime = resistanceOnHurt;
  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", 0.6f);
  46. rb.velocity = Vector2.zero;
  47. Camera.current.DOShakePosition(0.6f, 2f, 100).SetId("ScreenShake");
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. // 治疗
  54. public float healTime = 4f; private float currentHealTime = 0f;
  55. private void RefreshHearts() {
  56. float totalHp = Mathf.Clamp(hp + Mathf.Pow(currentHealTime / healTime, 7f) * 0.7f + currentHealTime / healTime * 0.3f, 0, 3);
  57. for (int i = 0; i < 3; i++) {
  58. hearts[i].localScale = Vector3.one * Mathf.Clamp(totalHp - i, 0, 1);
  59. }
  60. }
  61. // 射击
  62. public int maxBullet = 20, currentBullet = 20;
  63. public float shootDelay = 0.15f; private float currentShootDelay = 0f;
  64. public float reloadDelay = 1.5f; private float currentReloadDelay = 0f;
  65. public float bulletSpeed = 50f, bulletKnockback = 1f;
  66. public GameObject bullet; public TMPro.TextMeshProUGUI textBullet;
  67. internal override void OnUpdate() {
  68. // 反向
  69. if ((towardsRight && isPressingLeft() && !isPressingRight()) || (!towardsRight && isPressingRight() && !isPressingLeft())) {
  70. towardsRight = !towardsRight;
  71. transform.DOScaleX(towardsRight ? 1 : -1, 0.15f);
  72. }
  73. // 无敌
  74. if (resistanceTime > 0f) resistanceTime -= Time.deltaTime;
  75. // 治疗
  76. if (isPressingCrouch() && hp < 3 && isLand) {
  77. currentHealTime += Time.deltaTime;
  78. if (currentHealTime > healTime) {
  79. currentHealTime -= healTime;
  80. hp++;
  81. }
  82. RefreshHearts();
  83. } else if (currentHealTime != 0f) {
  84. currentHealTime = 0f;
  85. RefreshHearts();
  86. }
  87. // 装填
  88. if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f) {
  89. AudioM.Play("reload", 1f);
  90. currentReloadDelay = reloadDelay;
  91. } else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime) {
  92. currentReloadDelay = 0f; currentBullet = maxBullet;
  93. } else if (currentReloadDelay > 0) {
  94. currentReloadDelay -= Time.deltaTime;
  95. }
  96. // 开火
  97. if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f) {
  98. AudioM.Play("fire", 0.65f);
  99. currentBullet--; currentShootDelay = shootDelay;
  100. Vector2 fromPos = transform.position + Vector3.up * ((canCrouch && isPressingCrouch()) ? 0.55f : 1.3f);
  101. Vector2 toPos = AspectUtility.cam.ScreenToWorldPoint(Input.mousePosition);
  102. Vector2 shootVector = (toPos - fromPos).normalized;
  103. // 随机散布
  104. shootVector.x *= 1f + (float) (Modules.randomDefault.NextDouble() * 0.4f - 0.2f);
  105. shootVector.y *= 1f + (float) (Modules.randomDefault.NextDouble() * 0.4f - 0.2f);
  106. shootVector.Normalize();
  107. // 生成
  108. GameObject newBullet = Instantiate(bullet, fromPos, Quaternion.identity);
  109. newBullet.GetComponent<Rigidbody2D>().velocity = bulletSpeed * shootVector;
  110. newBullet.transform.eulerAngles = (shootVector.x > 0 ? -1 : 1) * Vector2.Angle(shootVector, Vector2.up) * Vector3.forward;
  111. // 后座力
  112. rb.velocity += bulletKnockback * -shootVector;
  113. // 屏幕抖动
  114. Camera.current.DOShakePosition(0.1f, 0.08f, 100).SetId("ScreenShake");
  115. } else if (currentShootDelay > 0) {
  116. currentShootDelay -= Time.deltaTime;
  117. }
  118. string fireKey = InputM.GetKeyRaw(InputM.KeyType.Fire);
  119. bool isFireDown = fireKey == "l" ? Input.GetMouseButtonDown(0) : (fireKey == "r" ? Input.GetMouseButtonDown(1) : Input.GetKeyDown(fireKey));
  120. if (isFireDown && currentBullet == 0 && currentReloadDelay <= 0f) {
  121. AudioM.Play("bullet_empty", 0.5f);
  122. }
  123. // 更新 UI
  124. textBullet.text = currentReloadDelay > 0f ?
  125. "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
  126. "".PadLeft(currentBullet, "|".ToCharArray().First());
  127. textBullet.color = currentReloadDelay > 0f ? new Color(0.6f, 0.6f, 0.6f) : new Color(0.9686275f, 0.5568628f, 0.1098039f);
  128. }
  129. }