Player.cs 4.7 KB

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