using UnityEngine;
/// <summary>
/// GAME 1303 Tier 2 — "This is where I can be hit."
///
/// A Hurtbox is a trigger collider that forwards damage to a Health component.
/// Keeping it separate from the body collider lets you make the hurtbox smaller
/// than the sprite (fairer hits) and lets a boss have several (head = weak point).
///
/// SETUP
/// 1. Make an empty CHILD of the character called "Hurtbox".
/// 2. Add a BoxCollider2D (Is Trigger ON) sized to the body, and this script.
/// 3. Set Team. Player = Player, enemies = Enemy, crates = Neutral.
/// 4. Health is found automatically on the parent. Damage Multiplier lets a
/// weak-point hurtbox take double damage (2) or armor take half (0.5).
/// 5. Put the child on a layer called "Hurtbox" and, in Project Settings ->
/// Physics 2D -> Layer Collision Matrix, make "Hurtbox" collide only with
/// "Hitbox" — that keeps hurtboxes from bumping into the ground.
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class Hurtbox : MonoBehaviour
{
public Team team = Team.Enemy;
[Tooltip("2 = weak point, 0.5 = armored.")]
public float damageMultiplier = 1f;
public Health Health { get; private set; }
public Knockback Knockback { get; private set; }
void Awake()
{
Health = GetComponentInParent<Health>();
Knockback = GetComponentInParent<Knockback>();
GetComponent<Collider2D>().isTrigger = true;
if (Health == null) Debug.LogWarning($"Hurtbox on {name}: no Health found on parent.");
}
/// <summary>Returns true if damage was actually applied (false if invulnerable/dead).</summary>
public bool ReceiveHit(int damage, Vector2 fromPosition, float knockbackForce, float knockbackUp)
{
if (Health == null) return false;
int final = Mathf.Max(1, Mathf.RoundToInt(damage * damageMultiplier));
bool applied = Health.TakeDamage(final);
if (applied && Knockback != null && knockbackForce > 0f)
Knockback.Apply(fromPosition, knockbackForce, knockbackUp);
return applied;
}
}