Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FuckingDelegates
- {
- internal sealed class Player
- {
- private int _health;
- private string _name;
- public event EventHandler PlayerDeath;
- public event Action<Player,Player,int> PlayerTakeDamage;
- #region переменные
- public string Name
- {
- get
- {
- return _name;
- }
- private set
- {
- _name = value;
- }
- }
- public int Health
- {
- get
- {
- return _health;
- }
- private set
- {
- if (value < 0 || value > 100)
- throw new ArgumentException();
- _health = value;
- }
- }
- public Player(string name)
- {
- Name = name;
- Health = 100;
- this.PlayerDeath += OnPlayerDeath;
- this.PlayerTakeDamage += OnPlayerTakeDamage;
- }
- public void Kill()
- {
- Health = 0;
- PlayerDeath?.Invoke(this, new EventArgs());
- }
- public bool IsAlive()
- {
- return (Health == 0 ? false : true);
- }
- #endregion
- public static void OnPlayerDeath(object sender, EventArgs args)
- {
- Player p = sender as Player;
- if (p != null)
- {
- Console.WriteLine($"Игрок {p.Name} был убит");
- }
- }
- public void TakeDamage(Player other, Int32 damage)
- {
- if (other == null) return;
- if (damage < 0) return;
- PlayerTakeDamage?.Invoke(this, other, damage);
- }
- public static void OnPlayerTakeDamage(Player taker, Player other, Int32 damage)
- {
- if (taker.Health <= damage)
- {
- taker.Kill();
- return;
- }
- else
- {
- taker.Health -= damage;
- }
- Console.WriteLine($"Игрок {taker.Name} получил пизды от {other.Name} в размере {damage} DMG");
- }
- }
- internal sealed class Program
- {
- static void Main(string[] args)
- {
- Player alex = new Player("Alex");
- Player mike = new Player("Mike");
- //Alex получил дамаг от mike
- alex.TakeDamage(mike, 10);
- alex.TakeDamage(mike, 80);
- alex.TakeDamage(mike, 20);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement