Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Threading;
- namespace IMJunior
- {
- class Program
- {
- static void Main()
- {
- Unit[] units =
- {
- new Unit(100),
- new Unit(100),
- };
- HealthBarV2[] healthbars = new HealthBarV2[units.Length];
- foreach (var unit in units)
- {
- unit.Health.OnChangeHealth += ChangeHealthHandler;
- }
- for (int i = 0; i < units.Length; i++)
- {
- healthbars[i] = HealthbarFactory.CreateHealthbar(units[i].Health, 0, i);
- }
- Thread.Sleep(1000);
- units[0].Health.TryDamage(50);
- Thread.Sleep(1000);
- units[0].Health.TryDamage(25);
- Thread.Sleep(1000);
- units[0].Health.TryDamage(15);
- Console.SetCursorPosition(0, 10);
- }
- static void ChangeHealthHandler(float normalize)
- {
- Console.Beep();
- }
- }
- delegate void DamageHandler(float normalize);
- class Health
- {
- public float MaxHealth { get; private set; }
- public float CurrentHealth { get; protected set; }
- public float NormalizeValue
- {
- get
- {
- return CurrentHealth / MaxHealth;
- }
- }
- public DamageHandler OnChangeHealth;
- public Health(float maxHealth)
- {
- OnChangeHealth = (v) => { };
- MaxHealth = maxHealth;
- CurrentHealth = MaxHealth;
- }
- public virtual void TryDamage(float damage)
- {
- CurrentHealth -= damage;
- if(OnChangeHealth != null)
- {
- OnChangeHealth(NormalizeValue);
- }
- }
- }
- class ArmoredHealth : Health
- {
- public ArmoredHealth(float maxHealth) : base(maxHealth)
- {
- }
- public override void TryDamage(float damage)
- {
- CurrentHealth -= damage * 0.5f;
- }
- }
- class Building
- {
- public readonly Health Health;
- public Building(Health health)
- {
- Health = health;
- }
- }
- class Unit
- {
- public readonly Health Health;
- public Unit(float health)
- {
- Health = new Health(health);
- }
- }
- class HealthBarV2
- {
- private int X, Y;
- public HealthBarV2(int x, int y)
- {
- X = x;
- Y = y;
- }
- public void Draw(float normalizeValue)
- {
- Console.SetCursorPosition(X, Y);
- Console.Write("".PadLeft(12, ' '));
- Console.SetCursorPosition(X, Y);
- string content = "";
- content = content.PadLeft((int)(normalizeValue * 10), '#');
- Console.Write($"[{content}]");
- }
- }
- class HealthBar
- {
- private readonly Health _target;
- delegate void Drawer();
- private readonly Drawer CurrentDraw;
- public HealthBar(Health target)
- {
- _target = target;
- if(target is ArmoredHealth)
- {
- CurrentDraw = DrawArmored;
- }
- else if(target is Health)
- {
- CurrentDraw = DrawSimple;
- }
- }
- public void Draw(int x, int y)
- {
- Console.SetCursorPosition(x, y);
- CurrentDraw();
- }
- public void DrawArmored()
- {
- string content = "";
- content = content.PadLeft((int)(_target.CurrentHealth / _target.MaxHealth * 10), '#');
- Console.WriteLine($"<{content}>");
- }
- public void DrawSimple()
- {
- string content = "";
- content = content.PadLeft((int)(_target.CurrentHealth / _target.MaxHealth * 10), '#');
- Console.WriteLine($"[{content}]");
- }
- }
- static class HealthbarFactory
- {
- public static HealthBarV2 CreateHealthbar(Health health, int x, int y)
- {
- var healthbar = new HealthBarV2(x, y);
- health.OnChangeHealth += healthbar.Draw;
- healthbar.Draw(health.NormalizeValue);
- return healthbar;
- }
- }
- }
Add Comment
Please, Sign In to add comment