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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/Scripts/Data.meta
Normal file
8
Assets/Scripts/Data.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc4c5b74b2090dd498e5f6b54edae302
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
41
Assets/Scripts/Data/Damage.cs
Normal file
41
Assets/Scripts/Data/Damage.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class Damage
|
||||
{
|
||||
public int value;
|
||||
|
||||
public static Type[] damageTypes = Type.all;
|
||||
|
||||
[ValueDropdown("damageTypes")]
|
||||
public Type type;
|
||||
|
||||
[Serializable]
|
||||
public class Type
|
||||
{
|
||||
private string name;
|
||||
|
||||
private Type(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Type normal = new("normal");
|
||||
|
||||
public static Type poison = new("poison");
|
||||
|
||||
public static Type[] all = new Type[] {
|
||||
normal,
|
||||
poison
|
||||
};
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Data/Damage.cs.meta
Normal file
11
Assets/Scripts/Data/Damage.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10150f370f18d5c47882ce685f073e43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Events.meta
Normal file
8
Assets/Scripts/Events.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4eb811cec162c154385f4126165b6cb1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Assets/Scripts/Events/OnHitEvent.cs
Normal file
24
Assets/Scripts/Events/OnHitEvent.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using static OnHitEvent;
|
||||
|
||||
[System.Serializable]
|
||||
public class OnHitEvent : UnityEvent<Args>
|
||||
{
|
||||
public class Args
|
||||
{
|
||||
public float hitMultiplier;
|
||||
public Damage damage;
|
||||
public GameObject source;
|
||||
|
||||
|
||||
public Args(Damage damage, float hitMultiplier, GameObject source)
|
||||
{
|
||||
this.damage = damage;
|
||||
this.hitMultiplier = hitMultiplier;
|
||||
this.source = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Events/OnHitEvent.cs.meta
Normal file
11
Assets/Scripts/Events/OnHitEvent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a10378074ffe5fa48aadc459fe8c38ef
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user