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