aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/ICanDie.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/ICanDie.cs')
-rw-r--r--Assets/Scripts/ICanDie.cs31
1 files changed, 31 insertions, 0 deletions
diff --git a/Assets/Scripts/ICanDie.cs b/Assets/Scripts/ICanDie.cs
new file mode 100644
index 0000000..d6ff6d1
--- /dev/null
+++ b/Assets/Scripts/ICanDie.cs
@@ -0,0 +1,31 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ICanDie : MonoBehaviour {
+ public float health = 3;
+
+ public AudioClip deathNoise;
+ public float deathNoiseVolume = 1f;
+
+ private AudioSource audio;
+
+ void Start() {
+ audio = GetComponent<AudioSource>();
+ }
+
+ public void Hit(float damage) {
+ health -= damage;
+
+ if (health <= 0) {
+ Die();
+ }
+ }
+
+ public void Die() {
+ if (deathNoise) audio.PlayOneShot(deathNoise, deathNoiseVolume);
+
+ GetComponent<SpriteRenderer>().enabled = false;
+ GetComponent<BoxCollider2D>().enabled = false;
+ }
+}