summaryrefslogtreecommitdiff
path: root/Damagable.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-03-13 11:00:58 +0800
committerchai <215380520@qq.com>2024-03-13 11:00:58 +0800
commit6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch)
treeb38119d2acf0a982cb67e381f146924b9bfc3b3f /Damagable.cs
+init
Diffstat (limited to 'Damagable.cs')
-rw-r--r--Damagable.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/Damagable.cs b/Damagable.cs
new file mode 100644
index 0000000..667cd5e
--- /dev/null
+++ b/Damagable.cs
@@ -0,0 +1,54 @@
+using UnityEngine;
+using UnityEngine.Events;
+
+public class Damagable : MonoBehaviour
+{
+ public float multiplier = 1f;
+
+ private PlayerDeath playerDeath;
+
+ public UnityEvent outOfLiveEvent;
+
+ public UnityEvent damageEvent;
+
+ public float health = 100f;
+
+ private float currentHealth;
+
+ private bool dead;
+
+ private Rigidbody rig;
+
+ private void Start()
+ {
+ rig = GetComponent<Rigidbody>();
+ currentHealth = health;
+ playerDeath = base.transform.root.GetComponent<PlayerDeath>();
+ }
+
+ private void Update()
+ {
+ }
+
+ public void TakeDamage(Vector3 damage, Vector3 hitPoint)
+ {
+ damage *= multiplier;
+ if ((bool)playerDeath)
+ {
+ playerDeath.TakeDamage(damage, hitPoint, rig);
+ return;
+ }
+ damageEvent.Invoke();
+ currentHealth -= damage.magnitude;
+ if (currentHealth < 0f && !dead)
+ {
+ dead = true;
+ outOfLiveEvent.Invoke();
+ }
+ }
+
+ public void RefillHP()
+ {
+ currentHealth = health;
+ }
+}