using UnityEngine;
///
/// Types of damage that can be dealt.
/// Used for damage resistance/vulnerability systems.
///
public enum DamageType
{
Physical,
Magical,
Mining,
Environmental,
True // Ignores resistances
}
///
/// Contains all information about a damage event.
///
[System.Serializable]
public struct DamageInfo
{
///
/// Amount of damage to deal.
///
public float Amount;
///
/// Type of damage being dealt.
///
public DamageType Type;
///
/// The GameObject that caused the damage (can be null).
///
public GameObject Source;
///
/// World position where the damage was applied.
///
public Vector3 HitPoint;
///
/// Direction the damage came from (for knockback, effects, etc.).
///
public Vector3 HitDirection;
///
/// Create a simple damage info with just an amount.
///
public DamageInfo(float amount)
{
Amount = amount;
Type = DamageType.Physical;
Source = null;
HitPoint = Vector3.zero;
HitDirection = Vector3.zero;
}
///
/// Create damage info with amount and type.
///
public DamageInfo(float amount, DamageType type)
{
Amount = amount;
Type = type;
Source = null;
HitPoint = Vector3.zero;
HitDirection = Vector3.zero;
}
///
/// Create full damage info with all parameters.
///
public DamageInfo(float amount, DamageType type, GameObject source,
Vector3 hitPoint = default, Vector3 hitDirection = default)
{
Amount = amount;
Type = type;
Source = source;
HitPoint = hitPoint;
HitDirection = hitDirection;
}
///
/// Create mining damage.
///
public static DamageInfo Mining(float amount, GameObject source = null)
{
return new DamageInfo(amount, DamageType.Mining, source);
}
///
/// Create physical damage with source and direction.
///
public static DamageInfo Physical(float amount, GameObject source, Vector3 hitPoint, Vector3 direction)
{
return new DamageInfo(amount, DamageType.Physical, source, hitPoint, direction);
}
}
///
/// Interface for any object that can take damage.
///
public interface IDamageable
{
///
/// Simple damage method for backwards compatibility.
///
/// Amount of damage to deal
void TakeDamage(float amount);
///
/// Enhanced damage method with full context.
/// Default implementation calls the simple TakeDamage for backwards compatibility.
///
/// Full damage information
void TakeDamage(DamageInfo damageInfo)
{
TakeDamage(damageInfo.Amount);
}
///
/// Current health value.
///
float CurrentHealth { get; }
///
/// Maximum health value.
///
float MaxHealth { get; }
///
/// Whether this object is still alive.
///
bool IsAlive { get; }
///
/// Health as a percentage (0-1).
///
float HealthPercent => MaxHealth > 0 ? CurrentHealth / MaxHealth : 0f;
}