| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using UnityEngine;
- /// <summary>
- /// Describes a weapon's attack characteristics.
- /// All combat goes through this so we can easily add more weapon types later.
- /// </summary>
- [System.Serializable]
- public class Weapon
- {
- public string Name;
- /// <summary>Number of dice to roll for damage.</summary>
- public int DamageDiceCount;
- /// <summary>Faces on each damage die (e.g. 4 for d4).</summary>
- public int DamageDiceFaces;
- /// <summary>Flat bonus added on top of the dice roll.</summary>
- public int DamageBonus;
- /// <summary>
- /// Base hit-chance (0-1). Multiplied by attacker advantage when attacking.
- /// Default fists: 0.65 base. Can be tuned per weapon in the future.
- /// </summary>
- public float BaseHitChance;
- /// <summary>Melee reach in world-units.</summary>
- public float MeleeRange;
- public Weapon(string name, int diceCount, int diceFaces, int damageBonus = 0,
- float baseHitChance = 0.65f, float meleeRange = 1.5f)
- {
- Name = name;
- DamageDiceCount = diceCount;
- DamageDiceFaces = diceFaces;
- DamageBonus = damageBonus;
- BaseHitChance = baseHitChance;
- MeleeRange = meleeRange;
- }
- /// <summary>Roll the damage dice and return the result.</summary>
- public int RollDamage()
- {
- int total = DamageBonus;
- for (int i = 0; i < DamageDiceCount; i++)
- total += Random.Range(1, DamageDiceFaces + 1);
- return Mathf.Max(1, total); // Always deal at least 1 damage
- }
- /// <summary>
- /// Attempt to hit a target.
- /// <paramref name="attackerAdvantage"/> is a 0-1 multiplier on top of BaseHitChance.
- /// 1.0 = full chance, 0.5 = halved, 1.5 = boosted (capped at 0.95 to never guarantee a hit).
- /// </summary>
- public bool TryHit(float attackerAdvantage = 1f)
- {
- float chance = Mathf.Clamp(BaseHitChance * attackerAdvantage, 0.05f, 0.95f);
- return Random.value <= chance;
- }
- // ---- Preset factories ----
- /// <summary>Default unarmed fists: 1d4 damage, 65 % base hit chance.</summary>
- public static Weapon Fists() => new Weapon("Fists", 1, 4, 0, 0.65f, 1.5f);
- }
|