RedFlys

Weapon v2

Mar 22nd, 2024
726
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 bool CanFire => _bullets > 0;
  7.  
  8.     public void Fire(IDamagable target)
  9.     {
  10.         if(target == null)
  11.             throw new ArgumentNullException("target");
  12.  
  13.         if (CanFire)
  14.         {
  15.             target.TakeDamage(_damage);
  16.             _bullets--;
  17.         }
  18.     }
  19. }
  20.  
  21. class Player : IDamagable
  22. {
  23.     private int _health;
  24.  
  25.     public bool IsAlive => _health > 0;
  26.  
  27.     public void TakeDamage(int damage)
  28.     {
  29.         if (damage < 0)
  30.             throw new ArgumentOutOfRangeException();
  31.  
  32.         if (IsAlive)
  33.             _health = Math.Max(_health - damage, 0);
  34.     }
  35. }
  36.  
  37. class Bot
  38. {
  39.     private Weapon _weapon;
  40.  
  41.     public Bot(Weapon weapon)
  42.     {
  43.         if (_weapon == null)
  44.             throw new ArgumentNullException();
  45.  
  46.         _weapon = weapon;
  47.     }
  48.  
  49.     public void OnSeePlayer(Player player)
  50.     {
  51.         if (player == null)
  52.             throw new ArgumentNullException();
  53.  
  54.         if (player.IsAlive && _weapon.CanFire)
  55.             _weapon.Fire(player);
  56.     }
  57. }
  58.  
  59. interface IDamagable
  60. {
  61.     public void TakeDamage(int damage) { }
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment