/
expens1ve
/
project_war
Обзор
Документация
Войти
/
expens1ve
/
project_war
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
observer
Models/Unit.cs
63 строки
2 KB
Vadim
feat: observer(sound, logs), del proxy
29 апр 2026, 02:44
29 апр 2026, 02:44
b0a067e
Код
Авторство
О чём код?
using System; using project_war.Models.Interfaces; namespace project_war.Models { /// <summary> /// Базовый абстрактный класс боевой единицы. Содержит общие свойства и простую логику получения урона. /// </summary> public abstract class Unit : IUnit { public event Action<IUnit>? OnDied; public event Action<IUnit, int>? OnHealthChanged; private int _health; public int Health { get => _health; set { int oldHealth = _health; _health = value; int delta = _health - oldHealth; if (delta != 0) { OnHealthChanged?.Invoke(this, delta); } if (oldHealth > 0 && _health <= 0) { OnDied?.Invoke(this); } } } public int AttackDamage { get; } public int Defense { get; } public int Cost { get; } protected Unit(int health, int attackDamage, int defense, int cost) { Health = health; AttackDamage = attackDamage; Defense = defense; Cost = cost; } public virtual void Hit(IUnit target) { if (target == null || Health <= 0 || target.Health <= 0) return; target.TakeDamage(AttackDamage); } public virtual void TakeDamage(int damage) { if (damage <= 0) return; int actualDamage = Math.Max(0, damage - Defense); Health = Math.Max(0, Health - actualDamage); } } }