From 1d74f3204692bb69aabf3c57fbdf4601a31a311f Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Sat, 16 Dec 2023 18:32:11 +0000
Subject: [PATCH] Added audio manager + sound effects

---
 Assets/Scripts/Damageable.cs |   62 +++++++++++++++++++++++++++++--
 1 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/Assets/Scripts/Damageable.cs b/Assets/Scripts/Damageable.cs
index 7901a04..09fa72f 100644
--- a/Assets/Scripts/Damageable.cs
+++ b/Assets/Scripts/Damageable.cs
@@ -1,21 +1,31 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Damageable : MonoBehaviour
 {
+#if (UNITY_EDITOR)
+    // ONLY FOR DEBUG USE
+    [SerializeField]
+    private bool selfDamage = false;
+#endif
     Animator animator;
 
     [SerializeField]
     private int _maxHealth = 100;
     [SerializeField]
-    private int _health;
+    private int _health = 100;
     [SerializeField]
     private bool _isAlive = true;
     [SerializeField]
     private bool isInvincible = false;
     private float timeSinceHit = 0f;
     public float invincibilityTime = 0.25f;
+
+    public AudioClip hitSound;
+    public AudioClip healSound;
+    public AudioClip deathSound;
 
     public int MaxHealth
     {
@@ -27,10 +37,21 @@
         get { return _health; }
         set
         {
-            _health = value;
+            if (value > MaxHealth)
+            {
+                _health = MaxHealth;
+            }
+            else
+            {
+                _health = value;
+            }
             if (value <= 0)
             {
                 IsAlive = false;
+                if (deathSound != null)
+                {
+                    SoundManager.instance.PlaySoundAtPoint(gameObject, deathSound.name);
+                }
             }
         }
     }
@@ -46,7 +67,7 @@
 
     private void Awake()
     {
-        Health = MaxHealth;
+        //Health = MaxHealth;
         animator = GetComponent<Animator>();
     }
     private void Update()
@@ -63,14 +84,47 @@
                 timeSinceHit += Time.deltaTime;
             }
         }
+#if (UNITY_EDITOR)
+        if (selfDamage)
+        {
+            Hit(10);
+        }
+#endif
     }
 
     public void Hit(int damage)
     {
         if (IsAlive && !isInvincible)
         {
-            Health -= damage;
+            int actualDamageAmount = Mathf.Min(damage, Health);
+            Health -= actualDamageAmount;
             isInvincible = true;
+
+            CharacterEvents.characterDamaged.Invoke(gameObject, actualDamageAmount);
+            if (hitSound != null)
+            {
+                SoundManager.instance.PlaySoundAtPoint(gameObject, hitSound.name);
+            }
         }
     }
+
+    public bool Heal(int healAmount)
+    {
+        bool result = false;
+        if (IsAlive && Health < MaxHealth)
+        {
+            int actualHealAmount = Mathf.Min(healAmount, MaxHealth - Health);
+            Health += actualHealAmount;
+
+            CharacterEvents.characterHealed.Invoke(gameObject, actualHealAmount);
+
+            result = true;
+            if (healSound != null)
+            {
+                SoundManager.instance.PlaySoundAtPoint(gameObject, healSound.name);
+            }
+
+        }
+        return result;
+    }
 }

--
Gitblit v1.9.3