From 62c4a240b05cc1831d02d46e944e146ae4d78e8f Mon Sep 17 00:00:00 2001 From: djkellerman Date: Wed, 26 Feb 2025 20:17:19 -0500 Subject: [PATCH] parry and health bar pt1 --- Assets/Scripts/Block.cs | 31 ++++++++++++- Assets/Scripts/Damageable.cs | 61 ++++++++++++++++++++------ Assets/Scripts/HealthBar.cs | 18 ++++++++ Assets/Scripts/HealthBar.cs.meta | 2 + Assets/Scripts/HealthBarFollow.cs | 15 +++++++ Assets/Scripts/HealthBarFollow.cs.meta | 2 + 6 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 Assets/Scripts/HealthBar.cs create mode 100644 Assets/Scripts/HealthBar.cs.meta create mode 100644 Assets/Scripts/HealthBarFollow.cs create mode 100644 Assets/Scripts/HealthBarFollow.cs.meta diff --git a/Assets/Scripts/Block.cs b/Assets/Scripts/Block.cs index 86b4b68..e954495 100644 --- a/Assets/Scripts/Block.cs +++ b/Assets/Scripts/Block.cs @@ -6,6 +6,9 @@ public class Block : MonoBehaviour { public bool blocking = false; private InputActionAsset actions; + private float blockPressTime = 0f; + private float parryThreshold = 0.2f; + private bool isParrying = false; private void Start() { @@ -15,16 +18,40 @@ public class Block : MonoBehaviour private void Update() { InputAction blockAction = actions.FindAction("Block"); - if (blockAction.ReadValue() == 1f) { + if (!blocking) + { + blockPressTime = Time.time; + } blocking = true; } else { + if (blocking) + { + float pressDuration = Time.time - blockPressTime; + if (pressDuration <= parryThreshold) + { + Parry(); + } + else + { + isParrying = false; + } + } blocking = false; } - GetComponent().block = blocking; } + + private void Parry() + { + isParrying = true; + } + + public bool IsParrying() + { + return isParrying; + } } diff --git a/Assets/Scripts/Damageable.cs b/Assets/Scripts/Damageable.cs index e3f4714..449f2af 100644 --- a/Assets/Scripts/Damageable.cs +++ b/Assets/Scripts/Damageable.cs @@ -8,6 +8,16 @@ public class Damageable : MonoBehaviour public float force = 50f; public float damage = 0f; public float maxDamage = 1000f; + public HealthBar healthBar; + + private void Start() + { + if (healthBar != null) + { + healthBar.SetMaxHealth(maxDamage); + healthBar.SetHealth(maxDamage - damage); + } + } private void OnTriggerEnter2D(Collider2D collision) { @@ -15,30 +25,51 @@ public class Damageable : MonoBehaviour { print($"{name}: Ouch"); Damage(collision.transform.parent.gameObject); - Recoil(collision.transform.parent.gameObject); } } - private void Recoil(GameObject damageSource) - { - GetComponent().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * damage, ForceMode2D.Force); - //damageSource.transform.localScale *= 1.1f; - } - private void Damage(GameObject damageSource) { float actualForce = force; - // Recoil - GetComponent().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * damage, ForceMode2D.Force); - - if (GetComponent().blocking) + Block blockComponent = GetComponent(); + if (blockComponent != null && blockComponent.blocking) { - damageSource.GetComponent().Damage(gameObject); - actualForce /= 4; + if (blockComponent.IsParrying()) + { + damageSource.GetComponent().SuccessfulParry(gameObject, actualForce); + return; + } + else + { + actualForce /= 4; + GetComponent().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * actualForce, ForceMode2D.Force); + } + } + else + { + GetComponent().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * actualForce, ForceMode2D.Force); } damage += actualForce; damage = Mathf.Clamp(damage, 0f, maxDamage); + if (healthBar != null) + { + healthBar.SetHealth(maxDamage - damage); + } + if (damage >= maxDamage) + { + Die(); + } + } + private void SuccessfulParry(GameObject damageSource, float force) + { + GetComponent().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * force, ForceMode2D.Force); + damage += force; + damage = Mathf.Clamp(damage, 0f, maxDamage); + if (healthBar != null) + { + healthBar.SetHealth(maxDamage - damage); + } if (damage >= maxDamage) { Die(); @@ -57,6 +88,10 @@ public class Damageable : MonoBehaviour public void ResetDamage() { damage = 0f; + if (healthBar != null) + { + healthBar.SetHealth(maxDamage); + } //transform.localScale = Vector3.one; } } diff --git a/Assets/Scripts/HealthBar.cs b/Assets/Scripts/HealthBar.cs new file mode 100644 index 0000000..ae336df --- /dev/null +++ b/Assets/Scripts/HealthBar.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using UnityEngine.UI; + +public class HealthBar : MonoBehaviour +{ + public Slider slider; + + public void SetMaxHealth(float health) + { + slider.maxValue = health; + slider.value = health; + } + + public void SetHealth(float health) + { + slider.value = health; + } +} diff --git a/Assets/Scripts/HealthBar.cs.meta b/Assets/Scripts/HealthBar.cs.meta new file mode 100644 index 0000000..f8163e4 --- /dev/null +++ b/Assets/Scripts/HealthBar.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 15c35eadf0afb0e42a41e4d6f5350554 \ No newline at end of file diff --git a/Assets/Scripts/HealthBarFollow.cs b/Assets/Scripts/HealthBarFollow.cs new file mode 100644 index 0000000..22efb8a --- /dev/null +++ b/Assets/Scripts/HealthBarFollow.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +public class HealthBarFollow : MonoBehaviour +{ + public Transform target; + public Vector3 offset; + + void Update() + { + if (target != null) + { + transform.position = target.position + offset; + } + } +} diff --git a/Assets/Scripts/HealthBarFollow.cs.meta b/Assets/Scripts/HealthBarFollow.cs.meta new file mode 100644 index 0000000..205af9c --- /dev/null +++ b/Assets/Scripts/HealthBarFollow.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 4bc53792102fa104f98e8374928d0979 \ No newline at end of file