From ec464e48e1c68737a820f70e5d40a87818ce2175 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Fri, 15 Dec 2023 23:45:56 +0000
Subject: [PATCH] Added pickups

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

diff --git a/Assets/Scripts/Damageable.cs b/Assets/Scripts/Damageable.cs
index 7901a04..b738468 100644
--- a/Assets/Scripts/Damageable.cs
+++ b/Assets/Scripts/Damageable.cs
@@ -1,15 +1,19 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Damageable : MonoBehaviour
 {
+    // ONLY FOR DEBUG USE
+    [SerializeField]
+    private bool selfDamage = false;
     Animator animator;
 
     [SerializeField]
     private int _maxHealth = 100;
     [SerializeField]
-    private int _health;
+    private int _health = 100;
     [SerializeField]
     private bool _isAlive = true;
     [SerializeField]
@@ -27,7 +31,14 @@
         get { return _health; }
         set
         {
-            _health = value;
+            if (value > MaxHealth)
+            {
+                _health = MaxHealth;
+            }
+            else
+            {
+                _health = value;
+            }
             if (value <= 0)
             {
                 IsAlive = false;
@@ -46,7 +57,7 @@
 
     private void Awake()
     {
-        Health = MaxHealth;
+        //Health = MaxHealth;
         animator = GetComponent<Animator>();
     }
     private void Update()
@@ -63,14 +74,37 @@
                 timeSinceHit += Time.deltaTime;
             }
         }
+        if (selfDamage)
+        {
+            Hit(10);
+        }
     }
 
     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);
         }
     }
+
+    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;
+
+        }
+        return result;
+    }
 }

--
Gitblit v1.9.3