parry and health bar pt1

This commit is contained in:
djkellerman
2025-02-26 20:17:19 -05:00
parent 710df1c8d4
commit 62c4a240b0
6 changed files with 114 additions and 15 deletions

View File

@@ -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<float>() == 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<AnimationPlayer>().block = blocking;
}
private void Parry()
{
isParrying = true;
}
public bool IsParrying()
{
return isParrying;
}
}

View File

@@ -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<Rigidbody2D>().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<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * damage, ForceMode2D.Force);
if (GetComponent<Block>().blocking)
Block blockComponent = GetComponent<Block>();
if (blockComponent != null && blockComponent.blocking)
{
damageSource.GetComponent<Damageable>().Damage(gameObject);
actualForce /= 4;
if (blockComponent.IsParrying())
{
damageSource.GetComponent<Damageable>().SuccessfulParry(gameObject, actualForce);
return;
}
else
{
actualForce /= 4;
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * actualForce, ForceMode2D.Force);
}
}
else
{
GetComponent<Rigidbody2D>().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<Rigidbody2D>().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;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 15c35eadf0afb0e42a41e4d6f5350554

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4bc53792102fa104f98e8374928d0979