RedFlys

Weapon v3

Mar 23rd, 2024
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Weapon
  2. {
  3.     private readonly int _damage;
  4.     private int _bullets;
  5.  
  6.     public Weapon() { }
  7.  
  8.     public Weapon(int damage, int bullets)
  9.     {
  10.         if (damage < 0)
  11.             throw new ArgumentOutOfRangeException();
  12.  
  13.         if (bullets < 0)
  14.             throw new ArgumentOutOfRangeException();
  15.  
  16.         _damage = damage;
  17.         _bullets = bullets;
  18.     }
  19.  
  20.     public bool CanFire => _bullets > 0;
  21.  
  22.     public void Fire(IDamagable target)
  23.     {
  24.         if (target == null)
  25.             throw new ArgumentNullException("target");
  26.  
  27.         if (CanFire)
  28.         {
  29.             target.TakeDamage(_damage);
  30.             _bullets--;
  31.         }
  32.     }
  33. }
  34.  
  35. class Player : IDamagable
  36. {
  37.     private int _health;
  38.  
  39.     public Player() { }
  40.  
  41.     public Player(int health)
  42.     {
  43.         if (health < 0)
  44.             throw new ArgumentOutOfRangeException();
  45.  
  46.         _health = health;
  47.     }
  48.  
  49.     public bool IsAlive => _health > 0;
  50.  
  51.     public void TakeDamage(int damage)
  52.     {
  53.         if (damage < 0)
  54.             throw new ArgumentOutOfRangeException();
  55.  
  56.         if (IsAlive)
  57.             _health = Math.Max(_health - damage, 0);
  58.     }
  59. }
  60.  
  61. class Bot
  62. {
  63.     private Weapon _weapon;
  64.  
  65.     public Bot() { }
  66.  
  67.     public Bot(Weapon weapon)
  68.     {
  69.         if (_weapon == null)
  70.             throw new ArgumentNullException();
  71.  
  72.         _weapon = weapon;
  73.     }
  74.  
  75.     public void OnSeePlayer(Player player)
  76.     {
  77.         if (player == null)
  78.             throw new ArgumentNullException();
  79.  
  80.         if (player.IsAlive && _weapon.CanFire)
  81.             _weapon.Fire(player);
  82.     }
  83. }
  84.  
  85. interface IDamagable
  86. {
  87.     public void TakeDamage(int damage) { }
  88. }
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment