Weapon.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. /// <summary>
  3. /// Describes a weapon's attack characteristics.
  4. /// All combat goes through this so we can easily add more weapon types later.
  5. /// </summary>
  6. [System.Serializable]
  7. public class Weapon
  8. {
  9. public string Name;
  10. /// <summary>Number of dice to roll for damage.</summary>
  11. public int DamageDiceCount;
  12. /// <summary>Faces on each damage die (e.g. 4 for d4).</summary>
  13. public int DamageDiceFaces;
  14. /// <summary>Flat bonus added on top of the dice roll.</summary>
  15. public int DamageBonus;
  16. /// <summary>
  17. /// Base hit-chance (0-1). Multiplied by attacker advantage when attacking.
  18. /// Default fists: 0.65 base. Can be tuned per weapon in the future.
  19. /// </summary>
  20. public float BaseHitChance;
  21. /// <summary>Melee reach in world-units.</summary>
  22. public float MeleeRange;
  23. public Weapon(string name, int diceCount, int diceFaces, int damageBonus = 0,
  24. float baseHitChance = 0.65f, float meleeRange = 1.5f)
  25. {
  26. Name = name;
  27. DamageDiceCount = diceCount;
  28. DamageDiceFaces = diceFaces;
  29. DamageBonus = damageBonus;
  30. BaseHitChance = baseHitChance;
  31. MeleeRange = meleeRange;
  32. }
  33. /// <summary>Roll the damage dice and return the result.</summary>
  34. public int RollDamage()
  35. {
  36. int total = DamageBonus;
  37. for (int i = 0; i < DamageDiceCount; i++)
  38. total += Random.Range(1, DamageDiceFaces + 1);
  39. return Mathf.Max(1, total); // Always deal at least 1 damage
  40. }
  41. /// <summary>
  42. /// Attempt to hit a target.
  43. /// <paramref name="attackerAdvantage"/> is a 0-1 multiplier on top of BaseHitChance.
  44. /// 1.0 = full chance, 0.5 = halved, 1.5 = boosted (capped at 0.95 to never guarantee a hit).
  45. /// </summary>
  46. public bool TryHit(float attackerAdvantage = 1f)
  47. {
  48. float chance = Mathf.Clamp(BaseHitChance * attackerAdvantage, 0.05f, 0.95f);
  49. return Random.value <= chance;
  50. }
  51. // ---- Preset factories ----
  52. /// <summary>Default unarmed fists: 1d4 damage, 65 % base hit chance.</summary>
  53. public static Weapon Fists() => new Weapon("Fists", 1, 4, 0, 0.65f, 1.5f);
  54. }