RedFlys

Weapon

Mar 20th, 2024
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. class Weapon
  2. {
  3.     private readonly int _damage;
  4.     private int _bullets;
  5.  
  6.     public bool HaveBullets => _bullets > 0;
  7.  
  8.     public void Fire(IDamagable target)
  9.     {
  10.         if (HaveBullets)
  11.         {
  12.             target.TakeDamage(_damage);
  13.             _bullets--;
  14.         }
  15.     }
  16. }
  17.  
  18. class Player : IDamagable
  19. {
  20.     private int _health;
  21.  
  22.     public bool IsAlive => _health > 0;
  23.  
  24.     public void TakeDamage(int damage)
  25.     {
  26.         if (IsAlive)
  27.             _health = _health > damage ? _health - damage : 0;        
  28.     }
  29. }
  30.  
  31. class Bot
  32. {
  33.     private Weapon _weapon;
  34.  
  35.     public void OnSeePlayer(Player player)
  36.     {
  37.         if (player.IsAlive && _weapon.HaveBullets)  
  38.             _weapon.Fire(player);
  39.     }
  40. }
  41.  
  42. interface IDamagable
  43. {
  44.     public void TakeDamage(int damage) { }
  45. }
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment