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