Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public abstract class Source
- {
- public string Name { get; protected set; }
- }
- public abstract class Stat
- {
- public string Name { get; private set; }
- public int MaxValue { get; private set; }
- public int CurrentValue { get; protected set; }
- public event Action<Source, int, int> OnValueChanged;
- protected Stat(string name, int maxValue)
- {
- Name = name;
- MaxValue = maxValue;
- CurrentValue = MaxValue;
- }
- public virtual void TakeDamage(int damage, Source source)
- {
- int initialValue = CurrentValue;
- CurrentValue -= damage;
- if (CurrentValue < 0)
- {
- CurrentValue = 0;
- }
- int damageDealt = initialValue - CurrentValue;
- OnValueChanged?.Invoke(source, -damageDealt, CurrentValue);
- Console.WriteLine($"{Name} Value: {CurrentValue}");
- }
- public virtual void Restore(int amount, Source source)
- {
- int initialValue = CurrentValue;
- CurrentValue += amount;
- if (CurrentValue > MaxValue)
- {
- CurrentValue = MaxValue;
- }
- int restoredAmount = CurrentValue - initialValue;
- OnValueChanged?.Invoke(source, restoredAmount, CurrentValue);
- Console.WriteLine($"{Name} Value: {CurrentValue}");
- }
- }
- public class HealthStat : Stat
- {
- public HealthStat(string name, int maxValue) : base(name, maxValue) { }
- }
- public class Shield : Stat
- {
- public Shield(string name, int maxValue) : base(name, maxValue) { }
- public override void TakeDamage(int damage, Source source)
- {
- int initialValue = CurrentValue;
- int shieldDamage = Math.Min(damage, CurrentValue);
- CurrentValue -= shieldDamage;
- damage -= shieldDamage;
- int damageDealt = initialValue - CurrentValue;
- OnValueChanged?.Invoke(source, -damageDealt, CurrentValue);
- Console.WriteLine($"{Name} Value: {CurrentValue}");
- }
- }
- public class Entity : Source
- {
- private List<Stat> stats;
- public Entity(string name, params Stat[] stats)
- {
- Name = name;
- this.stats = new List<Stat>(stats);
- }
- public void TakeDamage(int damage, Source source, DamageHandler handler)
- {
- damage = handler.ProcessDamage(damage, source);
- foreach (var stat in stats)
- {
- if (damage <= 0) break;
- int initialStatValue = stat.CurrentValue;
- stat.TakeDamage(damage, source);
- damage -= (initialStatValue - stat.CurrentValue);
- }
- Console.WriteLine($"{Name} Status:");
- foreach (var stat in stats)
- {
- Console.WriteLine($"{stat.Name} Value: {stat.CurrentValue}");
- }
- }
- public void Heal(string statName, int amount, Source source)
- {
- var stat = stats.FirstOrDefault(s => s.Name == statName);
- stat?.Restore(amount, source);
- }
- }
- public class DamageHandler
- {
- public event Func<int, Source, int> OnProcessDamage;
- public int ProcessDamage(int damage, Source source)
- {
- if (OnProcessDamage != null)
- {
- foreach (Func<int, Source, int> handler in OnProcessDamage.GetInvocationList())
- {
- damage = handler(damage, source);
- }
- }
- return damage;
- }
- }
- public class Ability : Source
- {
- public Ability(string name)
- {
- Name = name;
- }
- }
- public class Weapon : Source
- {
- public Weapon(string name)
- {
- Name = name;
- }
- }
- public class Program
- {
- public static void Main()
- {
- DamageHandler damageHandler = new DamageHandler();
- Shield playerShield = new Shield("Shield", 50);
- damageHandler.OnProcessDamage += playerShield.TakeDamage;
- HealthStat normalHealth = new HealthStat("Normal Health", 100);
- HealthStat armorHealth = new HealthStat("Armor", 75);
- Entity player = new Entity("Player", normalHealth, armorHealth, playerShield);
- Entity enemy = new Entity("Enemy", new HealthStat("Normal Health", 80));
- Ability healingSpell = new Ability("Healing Spell");
- Weapon sword = new Weapon("Sword");
- playerShield.OnValueChanged += (source, amount, value) =>
- Console.WriteLine($"{source.Name} affected shield. Changed by {amount}. Current Value: {value}");
- normalHealth.OnValueChanged += (source, amount, value) =>
- Console.WriteLine($"{source.Name} affected normal health. Changed by {amount}. Current Value: {value}");
- armorHealth.OnValueChanged += (source, amount, value) =>
- Console.WriteLine($"{source.Name} affected armor health. Changed by {amount}. Current Value: {value}");
- Console.WriteLine("Initial State:");
- Console.WriteLine($"Normal Health: {normalHealth.CurrentValue}, Armor Health: {armorHealth.CurrentValue}, Shield: {playerShield.CurrentValue}");
- player.TakeDamage(30, enemy, damageHandler);
- player.Heal("Normal Health", 20, healingSpell);
- player.TakeDamage(50, sword, damageHandler);
- playerShield.Restore(40, healingSpell);
- player.TakeDamage(80, enemy, damageHandler);
- player.Heal("Normal Health", 50, healingSpell);
- playerShield.Restore(30, healingSpell);
- Console.WriteLine("Final State:");
- Console.WriteLine($"Normal Health: {normalHealth.CurrentValue}, Armor Health: {armorHealth.CurrentValue}, Shield: {playerShield.CurrentValue}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment