View difference between Paste ID: nXFSHxEN and 1LbxtFLg
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Drawing;
3
using System.Collections.Generic;
4
using System.Threading;
5
6
namespace IMJunior
7
{
8
    class Program
9
    { 
10
        static void Main()
11
        {
12
            Unit[] units =
13
            {
14
                new Unit(100),
15
                new Unit(100),
16
            };
17
18
            HealthBarV2[] healthbars = new HealthBarV2[units.Length];
19
            foreach (var unit in units)
20
            {
21
                unit.Health.OnChangeHealth += ChangeHealthHandler;
22
            }
23
24
            for (int i = 0; i < units.Length; i++)
25
            {
26
                healthbars[i] = HealthbarFactory.CreateHealthbar(units[i].Health, 0, i);
27
28
            }
29
30
            Thread.Sleep(1000);
31
            units[0].Health.TryDamage(50);
32
            Thread.Sleep(1000);
33
            units[0].Health.TryDamage(25);
34
            Thread.Sleep(1000);
35
            units[0].Health.TryDamage(15);
36
37
            Console.SetCursorPosition(0, 10);
38
        }
39
40
        static void ChangeHealthHandler(float normalize)
41
        {
42
            Console.Beep();
43
        }
44
    }
45
46
    delegate void DamageHandler(float normalize);
47
48
    class Health
49
    {
50
        public float MaxHealth { get; private set; }
51
        public float CurrentHealth { get; protected set; }
52
        public float NormalizeValue
53
        {
54
            get
55
            {
56
                return CurrentHealth / MaxHealth;
57
            }
58
        }
59
        public DamageHandler OnChangeHealth;
60
61
62
        public Health(float maxHealth)
63
        {
64
            OnChangeHealth = (v) => { };
65
66
            MaxHealth = maxHealth;
67
            CurrentHealth = MaxHealth;
68
        }
69
70
        public virtual void TryDamage(float damage)
71
        {
72
            CurrentHealth -= damage;
73
            if(OnChangeHealth != null)
74
            {
75
                OnChangeHealth(NormalizeValue);
76
            }
77
        }
78
    }
79
80
    class ArmoredHealth : Health
81
    {
82
        public ArmoredHealth(float maxHealth) : base(maxHealth)
83
        {
84
        }
85
86
        public override void TryDamage(float damage)
87
        {
88
            CurrentHealth -= damage * 0.5f;
89
        }
90
    }
91
92
    class Building 
93
    {
94
        public readonly Health Health;
95
96
        public Building(Health health)
97
        {
98
            Health = health;
99
        }
100
    }
101
102
    class Unit
103
    {
104
        public readonly Health Health;
105
106
        public Unit(float health)
107
        {
108
            Health = new Health(health);
109
        }
110
    }
111
112
    class HealthBarV2
113
    {
114
        private int X, Y;
115
116
        public HealthBarV2(int x, int y)
117
        {
118
            X = x;
119
            Y = y;
120
        }
121
122
        public void Draw(float normalizeValue)
123
        {
124
            Console.SetCursorPosition(X, Y);
125
            Console.Write("".PadLeft(12, ' '));
126
            Console.SetCursorPosition(X, Y);
127
            string content = "";
128
            content = content.PadLeft((int)(normalizeValue * 10), '#');
129
            Console.Write($"[{content}]");
130
        }
131
    }
132
133
    class HealthBar
134
    {
135
        private readonly Health _target;
136
        delegate void Drawer();
137
        private readonly Drawer CurrentDraw;
138
139
        public HealthBar(Health target)
140
        {
141
            _target = target;
142
            if(target is ArmoredHealth)
143
            {
144
                CurrentDraw = DrawArmored;
145
            }
146
            else if(target is Health)
147
            {
148
                CurrentDraw = DrawSimple;
149
            }
150
        }
151
152
        public void Draw(int x, int y)
153
        {
154
            Console.SetCursorPosition(x, y);
155
            CurrentDraw();
156
        }
157
158
        public void DrawArmored()
159
        {
160
            string content = "";
161
            content = content.PadLeft((int)(_target.CurrentHealth / _target.MaxHealth * 10), '#');
162
            Console.WriteLine($"<{content}>");
163
        }
164
165
        public void DrawSimple()
166
        {
167
            string content = "";
168
            content = content.PadLeft((int)(_target.CurrentHealth / _target.MaxHealth * 10), '#');
169
            Console.WriteLine($"[{content}]");
170
        }
171
    }
172
173
    static class HealthbarFactory
174
    {
175
        public static HealthBarV2 CreateHealthbar(Health health, int x, int y)
176
        {
177
            var healthbar = new HealthBarV2(x, y);
178
            health.OnChangeHealth += healthbar.Draw;
179
            healthbar.Draw(health.NormalizeValue);
180
            return healthbar;
181
        }
182
    }
183
}