|
|
@@ -5,19 +5,22 @@ using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using UnityEngine;
|
|
|
|
|
|
-public class Player : EntityBase {
|
|
|
+public class Player : EntityBase
|
|
|
+{
|
|
|
|
|
|
internal override bool isPressingCrouch() => InputM.GetKeyEvent(InputM.KeyType.Crouch);
|
|
|
internal override bool isPressDownJump() => InputM.GetKeyEvent(InputM.KeyType.Jump);
|
|
|
internal override bool isPressingLeft() => InputM.GetKeyEvent(InputM.KeyType.Left);
|
|
|
internal override bool isPressingRight() => InputM.GetKeyEvent(InputM.KeyType.Right);
|
|
|
|
|
|
- // ����
|
|
|
+ // 生命
|
|
|
public RectTransform[] hearts;
|
|
|
private int _hp = 3;
|
|
|
- private int hp {
|
|
|
+ private int hp
|
|
|
+ {
|
|
|
get { return _hp; }
|
|
|
- set {
|
|
|
+ set
|
|
|
+ {
|
|
|
if (_hp == value) return;
|
|
|
_hp = value;
|
|
|
if (_hp == 0) _hp = 3;
|
|
|
@@ -25,16 +28,19 @@ public class Player : EntityBase {
|
|
|
}
|
|
|
}
|
|
|
public float knockbackForceH = 30f, knockbackForceV = 15f;
|
|
|
- internal override void OnHitBorder() {
|
|
|
+ internal override void OnHitBorder()
|
|
|
+ {
|
|
|
Hurt();
|
|
|
transform.position = new Vector3(0, 8, 0);
|
|
|
}
|
|
|
- internal override void OnHitEnemy(Collision2D collision) {
|
|
|
+ internal override void OnHitEnemy(Collision2D collision)
|
|
|
+ {
|
|
|
Hurt();
|
|
|
int face = collision.transform.position.x < transform.position.x ? 1 : -1;
|
|
|
rb.velocity = new Vector2(face * knockbackForceH, knockbackForceV);
|
|
|
}
|
|
|
- public void Hurt() {
|
|
|
+ public void Hurt()
|
|
|
+ {
|
|
|
hp--;
|
|
|
AudioM.Play("hurt");
|
|
|
rb.velocity = Vector2.zero;
|
|
|
@@ -42,46 +48,58 @@ public class Player : EntityBase {
|
|
|
Camera.current.DOShakePosition(0.3f, 0.3f, 20).SetId("ScreenShake");
|
|
|
}
|
|
|
|
|
|
- // ����
|
|
|
+ // 治疗
|
|
|
public float healTime = 4f; private float currentHealTime = 0f;
|
|
|
- private void RefreshHearts() {
|
|
|
+ private void RefreshHearts()
|
|
|
+ {
|
|
|
float totalHp = Mathf.Clamp(hp + Mathf.Pow(currentHealTime / healTime, 7f) * 0.7f + currentHealTime / healTime * 0.3f, 0, 3);
|
|
|
- for (int i = 0; i < 3; i++) {
|
|
|
+ for (int i = 0; i < 3; i++)
|
|
|
+ {
|
|
|
hearts[i].localScale = Vector3.one * Mathf.Clamp(totalHp - i, 0, 1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // ���
|
|
|
+ // 射击
|
|
|
public int maxBullet = 20, currentBullet = 20;
|
|
|
public float shootDelay = 0.15f; private float currentShootDelay = 0f;
|
|
|
public float reloadDelay = 1.5f; private float currentReloadDelay = 0f;
|
|
|
public float bulletSpeed = 50f, bulletKnockback = 1f;
|
|
|
public GameObject bullet; public TMPro.TextMeshProUGUI textBullet;
|
|
|
- internal override void OnUpdate() {
|
|
|
- // ����
|
|
|
- if (isPressingCrouch() && hp < 3 && isLand) {
|
|
|
+ internal override void OnUpdate()
|
|
|
+ {
|
|
|
+ // 治疗
|
|
|
+ if (isPressingCrouch() && hp < 3 && isLand)
|
|
|
+ {
|
|
|
currentHealTime += Time.deltaTime;
|
|
|
- if (currentHealTime > healTime) {
|
|
|
+ if (currentHealTime > healTime)
|
|
|
+ {
|
|
|
currentHealTime -= healTime;
|
|
|
hp++;
|
|
|
}
|
|
|
RefreshHearts();
|
|
|
- } else if (currentHealTime != 0f) {
|
|
|
+ }
|
|
|
+ else if (currentHealTime != 0f)
|
|
|
+ {
|
|
|
currentHealTime = 0f;
|
|
|
RefreshHearts();
|
|
|
}
|
|
|
- // װ��
|
|
|
- if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f) {
|
|
|
+ // 装填
|
|
|
+ if (InputM.GetKeyEvent(InputM.KeyType.Reload) && currentBullet < maxBullet && currentReloadDelay <= 0f)
|
|
|
+ {
|
|
|
AudioM.Play("reload");
|
|
|
currentReloadDelay = reloadDelay;
|
|
|
- } else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime) {
|
|
|
+ }
|
|
|
+ else if (currentReloadDelay > 0 && currentReloadDelay < Time.deltaTime)
|
|
|
+ {
|
|
|
currentReloadDelay = 0f; currentBullet = maxBullet;
|
|
|
- } else if (currentReloadDelay > 0) {
|
|
|
+ }
|
|
|
+ else if (currentReloadDelay > 0)
|
|
|
+ {
|
|
|
currentReloadDelay -= Time.deltaTime;
|
|
|
}
|
|
|
- // ����
|
|
|
- bool autoReload = false;
|
|
|
- if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f) {
|
|
|
+ // 开火
|
|
|
+ if (InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet > 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f)
|
|
|
+ {
|
|
|
AudioM.Play("fire");
|
|
|
currentBullet--; currentShootDelay = shootDelay;
|
|
|
Vector2 fromPos = transform.position + Vector3.up * 0.55f;
|
|
|
@@ -90,23 +108,18 @@ public class Player : EntityBase {
|
|
|
GameObject newBullet = Instantiate(bullet, fromPos, Quaternion.identity);
|
|
|
newBullet.GetComponent<Rigidbody2D>().velocity = bulletSpeed * shootVector;
|
|
|
newBullet.transform.eulerAngles = (shootVector.x > 0 ? -1 : 1) * Vector2.Angle(shootVector, Vector2.up) * Vector3.forward;
|
|
|
- // ������
|
|
|
+ // 后座力
|
|
|
rb.velocity += bulletKnockback * -shootVector;
|
|
|
- } else if (currentShootDelay > 0) {
|
|
|
- currentShootDelay -= Time.deltaTime;
|
|
|
- }else if (autoReload && InputM.GetKeyEvent(InputM.KeyType.Fire) && currentBullet == 0 && currentShootDelay <= 0f && currentReloadDelay <= 0f)
|
|
|
- {
|
|
|
- AudioM.Play("reload");
|
|
|
- currentReloadDelay = reloadDelay;
|
|
|
}
|
|
|
+ else if (currentShootDelay > 0)
|
|
|
{
|
|
|
-
|
|
|
+ currentShootDelay -= Time.deltaTime;
|
|
|
}
|
|
|
- // ���� UI
|
|
|
+ // 更新 UI
|
|
|
textBullet.text = currentReloadDelay > 0f ?
|
|
|
- "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
|
|
|
+ "".PadLeft(maxBullet - Mathf.RoundToInt(currentReloadDelay / reloadDelay * maxBullet), "|".ToCharArray().First()) :
|
|
|
"".PadLeft(currentBullet, "|".ToCharArray().First());
|
|
|
textBullet.color = currentReloadDelay > 0f ? new Color(0.6f, 0.6f, 0.6f) : new Color(0.6588235f, 1f, 0.6941177f);
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|