GibTreaty

Untitled

Jun 19th, 2024 (edited)
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public abstract class Source
  6. {
  7.     public string Name { get; protected set; }
  8. }
  9.  
  10. public abstract class Stat
  11. {
  12.     public string Name { get; private set; }
  13.     public int MaxValue { get; private set; }
  14.     public int CurrentValue { get; protected set; }
  15.  
  16.     public event Action<Source, int, int> OnValueChanged;
  17.  
  18.     protected Stat(string name, int maxValue)
  19.     {
  20.         Name = name;
  21.         MaxValue = maxValue;
  22.         CurrentValue = MaxValue;
  23.     }
  24.  
  25.     public virtual void TakeDamage(int damage, Source source)
  26.     {
  27.         int initialValue = CurrentValue;
  28.         CurrentValue -= damage;
  29.  
  30.         if (CurrentValue < 0)
  31.         {
  32.             CurrentValue = 0;
  33.         }
  34.  
  35.         int damageDealt = initialValue - CurrentValue;
  36.         OnValueChanged?.Invoke(source, -damageDealt, CurrentValue);
  37.         Console.WriteLine($"{Name} Value: {CurrentValue}");
  38.     }
  39.  
  40.     public virtual void Restore(int amount, Source source)
  41.     {
  42.         int initialValue = CurrentValue;
  43.         CurrentValue += amount;
  44.  
  45.         if (CurrentValue > MaxValue)
  46.         {
  47.             CurrentValue = MaxValue;
  48.         }
  49.  
  50.         int restoredAmount = CurrentValue - initialValue;
  51.         OnValueChanged?.Invoke(source, restoredAmount, CurrentValue);
  52.         Console.WriteLine($"{Name} Value: {CurrentValue}");
  53.     }
  54. }
  55.  
  56. public class HealthStat : Stat
  57. {
  58.     public HealthStat(string name, int maxValue) : base(name, maxValue) { }
  59. }
  60.  
  61. public class Shield : Stat
  62. {
  63.     public Shield(string name, int maxValue) : base(name, maxValue) { }
  64.  
  65.     public override void TakeDamage(int damage, Source source)
  66.     {
  67.         int initialValue = CurrentValue;
  68.         int shieldDamage = Math.Min(damage, CurrentValue);
  69.         CurrentValue -= shieldDamage;
  70.         damage -= shieldDamage;
  71.  
  72.         int damageDealt = initialValue - CurrentValue;
  73.         OnValueChanged?.Invoke(source, -damageDealt, CurrentValue);
  74.         Console.WriteLine($"{Name} Value: {CurrentValue}");
  75.     }
  76. }
  77.  
  78. public class Entity : Source
  79. {
  80.     private List<Stat> stats;
  81.  
  82.     public Entity(string name, params Stat[] stats)
  83.     {
  84.         Name = name;
  85.         this.stats = new List<Stat>(stats);
  86.     }
  87.  
  88.     public void TakeDamage(int damage, Source source, DamageHandler handler)
  89.     {
  90.         damage = handler.ProcessDamage(damage, source);
  91.  
  92.         foreach (var stat in stats)
  93.         {
  94.             if (damage <= 0) break;
  95.             int initialStatValue = stat.CurrentValue;
  96.             stat.TakeDamage(damage, source);
  97.             damage -= (initialStatValue - stat.CurrentValue);
  98.         }
  99.  
  100.         Console.WriteLine($"{Name} Status:");
  101.         foreach (var stat in stats)
  102.         {
  103.             Console.WriteLine($"{stat.Name} Value: {stat.CurrentValue}");
  104.         }
  105.     }
  106.  
  107.     public void Heal(string statName, int amount, Source source)
  108.     {
  109.         var stat = stats.FirstOrDefault(s => s.Name == statName);
  110.         stat?.Restore(amount, source);
  111.     }
  112. }
  113.  
  114. public class DamageHandler
  115. {
  116.     public event Func<int, Source, int> OnProcessDamage;
  117.  
  118.     public int ProcessDamage(int damage, Source source)
  119.     {
  120.         if (OnProcessDamage != null)
  121.         {
  122.             foreach (Func<int, Source, int> handler in OnProcessDamage.GetInvocationList())
  123.             {
  124.                 damage = handler(damage, source);
  125.             }
  126.         }
  127.         return damage;
  128.     }
  129. }
  130.  
  131. public class Ability : Source
  132. {
  133.     public Ability(string name)
  134.     {
  135.         Name = name;
  136.     }
  137. }
  138.  
  139. public class Weapon : Source
  140. {
  141.     public Weapon(string name)
  142.     {
  143.         Name = name;
  144.     }
  145. }
  146.  
  147. public class Program
  148. {
  149.     public static void Main()
  150.     {
  151.         DamageHandler damageHandler = new DamageHandler();
  152.         Shield playerShield = new Shield("Shield", 50);
  153.         damageHandler.OnProcessDamage += playerShield.TakeDamage;
  154.  
  155.         HealthStat normalHealth = new HealthStat("Normal Health", 100);
  156.         HealthStat armorHealth = new HealthStat("Armor", 75);
  157.         Entity player = new Entity("Player", normalHealth, armorHealth, playerShield);
  158.         Entity enemy = new Entity("Enemy", new HealthStat("Normal Health", 80));
  159.         Ability healingSpell = new Ability("Healing Spell");
  160.         Weapon sword = new Weapon("Sword");
  161.  
  162.         playerShield.OnValueChanged += (source, amount, value) =>
  163.             Console.WriteLine($"{source.Name} affected shield. Changed by {amount}. Current Value: {value}");
  164.         normalHealth.OnValueChanged += (source, amount, value) =>
  165.             Console.WriteLine($"{source.Name} affected normal health. Changed by {amount}. Current Value: {value}");
  166.         armorHealth.OnValueChanged += (source, amount, value) =>
  167.             Console.WriteLine($"{source.Name} affected armor health. Changed by {amount}. Current Value: {value}");
  168.  
  169.         Console.WriteLine("Initial State:");
  170.         Console.WriteLine($"Normal Health: {normalHealth.CurrentValue}, Armor Health: {armorHealth.CurrentValue}, Shield: {playerShield.CurrentValue}");
  171.  
  172.         player.TakeDamage(30, enemy, damageHandler);
  173.         player.Heal("Normal Health", 20, healingSpell);
  174.         player.TakeDamage(50, sword, damageHandler);
  175.         playerShield.Restore(40, healingSpell);
  176.         player.TakeDamage(80, enemy, damageHandler);
  177.         player.Heal("Normal Health", 50, healingSpell);
  178.         playerShield.Restore(30, healingSpell);
  179.  
  180.         Console.WriteLine("Final State:");
  181.         Console.WriteLine($"Normal Health: {normalHealth.CurrentValue}, Armor Health: {armorHealth.CurrentValue}, Shield: {playerShield.CurrentValue}");
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment