Add damage system and processing system to enemy
This commit is contained in:
10
Assets/Scripts/Components/DamageDealerComponent.cs
Normal file
10
Assets/Scripts/Components/DamageDealerComponent.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DamageDealerComponent : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
public Damage damage;
|
||||
}
|
||||
11
Assets/Scripts/Components/DamageDealerComponent.cs.meta
Normal file
11
Assets/Scripts/Components/DamageDealerComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e319f0156710fae4b86f831b853bc104
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Assets/Scripts/Components/DetectHitComponent.cs
Normal file
34
Assets/Scripts/Components/DetectHitComponent.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class DetectHitComponent : MonoBehaviour
|
||||
{
|
||||
private static float[] hitMultipliers = new float[] {
|
||||
0.5f,
|
||||
0.75f,
|
||||
1,
|
||||
1.25f,
|
||||
1.5f,
|
||||
2
|
||||
};
|
||||
|
||||
[SerializeField]
|
||||
[ValueDropdown("hitMultipliers")]
|
||||
private float hitMultiplier;
|
||||
|
||||
public OnHitEvent onHit;
|
||||
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
var gameObject = collision.gameObject;
|
||||
var damageDealer = gameObject.GetComponent<DamageDealerComponent>();
|
||||
|
||||
if (damageDealer == null) return;
|
||||
|
||||
onHit.Invoke(new(damageDealer.damage, hitMultiplier, gameObject));
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/DetectHitComponent.cs.meta
Normal file
11
Assets/Scripts/Components/DetectHitComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68d87505d90e702488bf2538540f835c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Assets/Scripts/Components/ProcessHitComponent.cs
Normal file
24
Assets/Scripts/Components/ProcessHitComponent.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class ProcessHitComponent : MonoBehaviour
|
||||
{
|
||||
private HashSet<GameObject> ignored = new HashSet<GameObject>();
|
||||
|
||||
public UnityEvent<int> onTakeDamage;
|
||||
|
||||
public void Process(OnHitEvent.Args args)
|
||||
{
|
||||
var source = args.source;
|
||||
|
||||
if (ignored.Contains(source)) return;
|
||||
|
||||
Debug.Log("Got hit");
|
||||
|
||||
onTakeDamage.Invoke(args.damage.value);
|
||||
|
||||
ignored.Add(args.source);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/ProcessHitComponent.cs.meta
Normal file
11
Assets/Scripts/Components/ProcessHitComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 647cabad96a59654cb4718c0bdf2ddd5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,6 +1,7 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class RagdollComponent : MonoBehaviour
|
||||
@@ -8,18 +9,24 @@ public class RagdollComponent : MonoBehaviour
|
||||
[Button]
|
||||
public void Ragdoll()
|
||||
{
|
||||
var rigidBodies = GetComponentsInChildren<Rigidbody>();
|
||||
|
||||
foreach (var rb in rigidBodies)
|
||||
{
|
||||
rb.isKinematic = false;
|
||||
}
|
||||
StartCoroutine(RagdollCoroutine());
|
||||
}
|
||||
|
||||
IEnumerator RagdollCoroutine()
|
||||
{
|
||||
var animators = GetComponentsInChildren<Animator>();
|
||||
|
||||
foreach (var a in animators)
|
||||
{
|
||||
a.enabled = false;
|
||||
}
|
||||
|
||||
var rigidBodies = GetComponentsInChildren<Rigidbody>();
|
||||
|
||||
foreach (var rb in rigidBodies)
|
||||
{
|
||||
rb.isKinematic = false;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user