2025-01-17 19:46:17 -05:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
|
|
|
[RequireComponent(typeof(Collider2D))]
|
|
|
|
|
[RequireComponent(typeof(RespawnOnTriggerEnter))]
|
|
|
|
|
public class Damageable : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public float force = 50f;
|
|
|
|
|
public float damage = 0f;
|
2025-02-08 18:54:21 -05:00
|
|
|
public float maxDamage = 1000f;
|
2025-01-17 19:46:17 -05:00
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
|
{
|
|
|
|
|
if (collision.gameObject.CompareTag("Punch Hurtbox"))
|
|
|
|
|
{
|
|
|
|
|
print($"{name}: Ouch");
|
2025-02-09 17:18:51 -05:00
|
|
|
Damage(collision.transform.parent.gameObject);
|
2025-01-17 19:46:17 -05:00
|
|
|
Recoil(collision.transform.parent.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Recoil(GameObject damageSource)
|
|
|
|
|
{
|
2025-02-08 18:54:21 -05:00
|
|
|
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * damage, ForceMode2D.Force);
|
2025-01-17 20:11:04 -05:00
|
|
|
//damageSource.transform.localScale *= 1.1f;
|
2025-01-17 19:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 17:18:51 -05:00
|
|
|
private void Damage(GameObject damageSource)
|
2025-01-17 19:46:17 -05:00
|
|
|
{
|
2025-02-09 17:18:51 -05:00
|
|
|
float actualForce = force;
|
|
|
|
|
// Recoil
|
|
|
|
|
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * damage, ForceMode2D.Force);
|
|
|
|
|
|
|
|
|
|
if (GetComponent<Block>().blocking)
|
|
|
|
|
{
|
|
|
|
|
damageSource.GetComponent<Damageable>().Damage(gameObject);
|
|
|
|
|
actualForce /= 4;
|
|
|
|
|
}
|
|
|
|
|
damage += actualForce;
|
2025-02-08 18:54:21 -05:00
|
|
|
damage = Mathf.Clamp(damage, 0f, maxDamage);
|
2025-01-17 19:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetDamage()
|
|
|
|
|
{
|
|
|
|
|
damage = 0f;
|
2025-01-17 20:11:04 -05:00
|
|
|
//transform.localScale = Vector3.one;
|
2025-01-17 19:46:17 -05:00
|
|
|
}
|
|
|
|
|
}
|