diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-12 08:49:44 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-12 08:49:44 +0800 |
| commit | b1a45c232474e99b52eadc43406ab7de15b7ee95 (patch) | |
| tree | 9b821060928ab5272ff739dfe3ba462f8f3db034 /Assets/Scripts | |
| parent | cb06c001d8c99bcf2d4fd16db4b8443f12b3cf60 (diff) | |
Add health system
Diffstat (limited to 'Assets/Scripts')
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs | 57 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs.meta (renamed from Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs.meta) | 2 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/Example.unity | 21 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/HealthData.cs | 95 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/HealthData.cs.meta | 11 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png | bin | 0 -> 349 bytes | |||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png.meta | 114 | ||||
| -rw-r--r-- | Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs | 55 |
8 files changed, 293 insertions, 62 deletions
diff --git a/Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs b/Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs new file mode 100644 index 0000000..7cba9a1 --- /dev/null +++ b/Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs @@ -0,0 +1,57 @@ +#if UNITY_EDITOR +using UnityEditor; +using UnityEngine; + +namespace DamageVolumeSystem.Editor +{ + [CustomEditor(typeof(HealthData))] + public class HealthDataEditor : UnityEditor.Editor + { + /* + * Editor page created by Mountools + * Author : WangSoul Team + * Date : 2026 / 05 / 12 - 08:30:25 + */ + + public HealthData healthData; + + private void OnEnable() + { + healthData = (HealthData) target; + } + + private void DrawHealthDataEditor() + { + if (Application.isPlaying) + { + healthData.Health = EditorGUILayout.IntSlider($"Health ({(float) healthData.Health / healthData.maxHealth * 100}%)", healthData.Health, 0, healthData.maxHealth); + } + } + + #region _ + + public override void OnInspectorGUI() + { + Undo.RecordObject(healthData, "Edit HealthData"); + EditorGUI.BeginChangeCheck(); + + DrawDefaultInspector(); + DrawHealthDataEditor(); + + if (EditorGUI.EndChangeCheck()) + { + EditorUtility.SetDirty(healthData); + Undo.FlushUndoRecordObjects(); + serializedObject.ApplyModifiedProperties(); + } + } + + private bool Property(string label, string propertyName, params GUILayoutOption[] options) + { + return EditorGUILayout.PropertyField(serializedObject.FindProperty(propertyName), new GUIContent(label), options); + } + + #endregion + } +} +#endif
\ No newline at end of file diff --git a/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs.meta b/Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs.meta index 8f062e3..763ffe3 100644 --- a/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs.meta +++ b/Assets/Scripts/DamageVolumeSystem/Editor/HealthDataEditor.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a243d9be50ad64b4f81a04072610b82c +guid: 3a763b024ebd37341a900fd60f6c71ef MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/DamageVolumeSystem/Example.unity b/Assets/Scripts/DamageVolumeSystem/Example.unity index ac25050..bab76f5 100644 --- a/Assets/Scripts/DamageVolumeSystem/Example.unity +++ b/Assets/Scripts/DamageVolumeSystem/Example.unity @@ -481,14 +481,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 34b8ce046bf25f9418c630a25cd42c83, type: 3} m_Name: m_EditorClassIdentifier: - group: 0 + group: 1 generators: [] onDamageReceived: m_PersistentCalls: m_Calls: - m_Target: {fileID: 888070365} - m_TargetAssemblyTypeName: DamageVolumeSystem.SimpleHealth, WangSoul.DamageVolumeSystem - m_MethodName: Damage + m_TargetAssemblyTypeName: DamageVolumeSystem.HealthData, WangSoul.DamageVolumeSystem + m_MethodName: Hurt m_Mode: 0 m_Arguments: m_ObjectArgument: {fileID: 0} @@ -573,11 +573,20 @@ MonoBehaviour: m_GameObject: {fileID: 888070360} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: a243d9be50ad64b4f81a04072610b82c, type: 3} + m_Script: {fileID: 11500000, guid: 7743c30e41148c74c8552573d475c29d, type: 3} m_Name: m_EditorClassIdentifier: - max: 10000 - health: 10000 + defaultHealth: 5000 + maxHealth: 5000 + onAid: + m_PersistentCalls: + m_Calls: [] + onHurt: + m_PersistentCalls: + m_Calls: [] + onDead: + m_PersistentCalls: + m_Calls: [] --- !u!1 &1341760969 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/DamageVolumeSystem/HealthData.cs b/Assets/Scripts/DamageVolumeSystem/HealthData.cs new file mode 100644 index 0000000..8da1e5f --- /dev/null +++ b/Assets/Scripts/DamageVolumeSystem/HealthData.cs @@ -0,0 +1,95 @@ +using System; +using UnityEngine; +using UnityEngine.Events; + +namespace DamageVolumeSystem +{ + [Icon("Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png")] + public class HealthData : MonoBehaviour + { + public int defaultHealth = 5000; + public int maxHealth = 5000; + + public UnityEvent<Damage> onAid = new(); + public UnityEvent<Damage> onHurt = new(); + public UnityEvent onDead = new(); + + private int _health; + + public int Health + { + get => _health; + set + { + var offset = value - _health; + var offsetAbs = Mathf.Abs(offset); + switch (offset) + { + case > 0: + Aid(new Damage + { + Group = 12, + Force = offsetAbs, + Direction = Vector3.zero + }); + return; + case < 0: + Hurt(new Damage + { + Group = 12, + Force = offsetAbs, + Direction = Vector3.zero + }); + break; + } + } + } + + private void Start() + { + _health = defaultHealth; + } + + private void Reset() + { + var newHealth = Mathf.Clamp(defaultHealth, 0, maxHealth); + SetHealth(newHealth); + } + + public void Aid(Damage damage) + { + AddHealth(damage.Force); + onAid.Invoke(damage); + } + + public void Hurt(Damage damage) + { + ReduceHealth(damage.Force); + onHurt.Invoke(damage); + + if (_health <= 0) + onDead.Invoke(); + } + + public static HealthData operator +(HealthData a, Damage b) + { + a.Aid(b); + return a; + } + + public static HealthData operator -(HealthData a, Damage b) + { + a.Hurt(b); + return a; + } + + private void SetHealth(int newHealth) + => _health = Mathf.Clamp(newHealth, 0, maxHealth); + + private void AddHealth(int newHealth) + => _health = Mathf.Clamp(_health + newHealth, 0, maxHealth); + + private void ReduceHealth(int newHealth) + => _health = Mathf.Clamp(_health - newHealth, 0, maxHealth); + } +} diff --git a/Assets/Scripts/DamageVolumeSystem/HealthData.cs.meta b/Assets/Scripts/DamageVolumeSystem/HealthData.cs.meta new file mode 100644 index 0000000..35eeecc --- /dev/null +++ b/Assets/Scripts/DamageVolumeSystem/HealthData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7743c30e41148c74c8552573d475c29d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png b/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png Binary files differnew file mode 100644 index 0000000..f3348a8 --- /dev/null +++ b/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png diff --git a/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png.meta b/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png.meta new file mode 100644 index 0000000..991b090 --- /dev/null +++ b/Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png.meta @@ -0,0 +1,114 @@ +fileFormatVersion: 2 +guid: b33f02fcd7392d14c8ac18ca1b08d4b9 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 13 + mipmaps: + mipMapMode: 0 + enableMipMap: 1 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + flipGreenChannel: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + ignoreMipmapLimit: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 0 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 0 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 0 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 2 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + swizzle: 50462976 + cookieLightType: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + nameFileIdTable: {} + mipmapLimitGroupName: + pSDRemoveMatte: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs b/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs deleted file mode 100644 index c301d4a..0000000 --- a/Assets/Scripts/DamageVolumeSystem/SimpleHealth.cs +++ /dev/null @@ -1,55 +0,0 @@ -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); - } - } -} |
