Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FuckingDelegates
  4. {
  5.  
  6.     internal sealed class Player
  7.     {
  8.         private int _health;
  9.         private string _name;
  10.  
  11.         public event EventHandler PlayerDeath;
  12.         public event Action<Player,Player,int> PlayerTakeDamage;
  13.  
  14.         #region переменные
  15.         public string Name
  16.         {
  17.             get
  18.             {
  19.                 return _name;
  20.             }
  21.             private set
  22.             {
  23.                 _name = value;
  24.             }
  25.         }
  26.  
  27.         public int Health
  28.         {
  29.             get
  30.             {
  31.                 return _health;
  32.             }
  33.             private set
  34.             {
  35.                 if (value < 0 || value > 100)
  36.                     throw new ArgumentException();
  37.                 _health = value;
  38.             }
  39.         }
  40.  
  41.         public Player(string name)
  42.         {
  43.             Name = name;
  44.             Health = 100;
  45.             this.PlayerDeath += OnPlayerDeath;
  46.             this.PlayerTakeDamage += OnPlayerTakeDamage;
  47.         }
  48.  
  49.         public void Kill()
  50.         {
  51.             Health = 0;
  52.             PlayerDeath?.Invoke(this, new EventArgs());
  53.         }
  54.  
  55.         public bool IsAlive()
  56.         {
  57.             return (Health == 0 ? false : true);
  58.         }
  59.        
  60.         #endregion
  61.         public static void OnPlayerDeath(object sender, EventArgs args)
  62.         {
  63.             Player p = sender as Player;
  64.             if (p != null)
  65.             {
  66.                 Console.WriteLine($"Игрок {p.Name} был убит");
  67.             }
  68.         }
  69.  
  70.         public void TakeDamage(Player other, Int32 damage)
  71.         {
  72.             if (other == null) return;
  73.             if (damage < 0) return;
  74.            
  75.             PlayerTakeDamage?.Invoke(this, other, damage);
  76.         }
  77.  
  78.         public static void OnPlayerTakeDamage(Player taker, Player other, Int32 damage)
  79.         {
  80.             if (taker.Health <= damage)
  81.             {
  82.                 taker.Kill();
  83.                 return;
  84.             }
  85.             else
  86.             {
  87.                 taker.Health -= damage;
  88.             }
  89.             Console.WriteLine($"Игрок {taker.Name} получил пизды от {other.Name} в размере {damage} DMG");
  90.         }
  91.     }
  92.  
  93.     internal sealed class Program
  94.     {
  95.         static void Main(string[] args)
  96.         {
  97.  
  98.             Player alex = new Player("Alex");            
  99.             Player mike = new Player("Mike");
  100.  
  101.             //Alex получил дамаг от mike
  102.             alex.TakeDamage(mike, 10);
  103.             alex.TakeDamage(mike, 80);
  104.             alex.TakeDamage(mike, 20);
  105.  
  106.  
  107.             Console.ReadKey();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement