aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs
blob: c301d4a413ad5e0886c218793d00170780675b4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using UnityEngine;

namespace DamageVolumeSystem
{
    [RequireComponent(typeof(Rigidbody))]
    public class SimpleHealth : MonoBehaviour
    {
        public int max = 10000;
        public int health = 10000;
        
        private Rigidbody _rigidbody;

        private void Start()
        {
            _rigidbody = GetComponent<Rigidbody>();
        }

        public void Damage(Damage amount)
        {
            health -= amount.Force;
            health = Mathf.Clamp(health, 0, max);
            
            _rigidbody.AddForce(amount.Direction * amount.Force / 200);
        }

        public void Heal(Damage amount)
        {
            health += amount.Force;
            health = Mathf.Clamp(health, 0, max);
        }
        
        private void OnDrawGizmos()
        {
            var t = transform;
            var size = (t.lossyScale.x + t.lossyScale.y + t.lossyScale.z) / 3;
            
            Gizmos.matrix = t.localToWorldMatrix;
            
            Gizmos.DrawWireCube(new Vector3(0, size, 0), new Vector3(
                2 / t.lossyScale.x, 
                0.5f / t.lossyScale.y, 
                0.5f / t.lossyScale.z
                ) * size);

            Gizmos.color = new Color(1f, 0.44f, 0.37f);
            
            Gizmos.DrawCube(new Vector3(0, size, 0), new Vector3(
                2 / t.lossyScale.x * ((float) health / max), 
                0.5f / t.lossyScale.y, 
                0.5f / t.lossyScale.z
            ) * size * 0.8f);
        }
    }
}