Player.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 if (currentHealTime != 0f) {
  65. currentHealTime = 0f;
  66. RefreshHearts();
  67. }
  68. // װ��
  69. if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f) {
  70. AudioM.Play("reload");
  71. currentReloadDelay = reloadDelay;
  72. } else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime) {
  73. currentReloadDelay = 0f; currentBullet = maxBullet;
  74. } else if (currentReloadDelay > 0) {
  75. currentReloadDelay -= Time.deltaTime;
  76. }
  77. // ����
  78. bool autoReload = false;
  79. if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f) {
  80. AudioM.Play("fire");
  81. currentBullet--; currentShootDelay = shootDelay;
  82. Vector2 fromPos = transform.position + Vector3.up * 0.55f;
  83. Vector2 toPos = Camera.allCameras[0].ScreenToWorldPoint(Input.mousePosition);
  84. Vector2 shootVector = (toPos - fromPos).normalized;
  85. GameObject newBullet = Instantiate(bullet, fromPos, Quaternion.identity);
  86. newBullet.GetComponent<Rigidbody2D>().velocity = bulletSpeed * shootVector;
  87. newBullet.transform.eulerAngles = (shootVector.x > 0 ? -1 : 1) * Vector2.Angle(shootVector, Vector2.up) * Vector3.forward;
  88. // ������
  89. rb.velocity += bulletKnockback * -shootVector;
  90. } else if (currentShootDelay > 0) {
  91. currentShootDelay -= Time.deltaTime;
  92. }else if (autoReload && InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet == 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f)
  93. {
  94. AudioM.Play("reload");
  95. currentReloadDelay = reloadDelay;
  96. }
  97. {
  98. }
  99. // ���� UI
  100. textBullet.text = currentReloadDelay > 0f ?
  101. "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
  102. "".PadLeft(currentBullet, "|".ToCharArray().First());
  103. textBullet.color = currentReloadDelay > 0f ? new Color(0.6f, 0.6f, 0.6f) : new Color(0.6588235f, 1f, 0.6941177f);
  104. }
  105. }