/
ConsoleSprite.cs
156 lines (144 loc) · 4.85 KB
/
ConsoleSprite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Blackjack
{
public enum SpriteOrigin
{
TopLeft = 0,
Left = 1,
BottomLeft = 3,
Bottom = 4,
BottomRight = 5,
Right = 6,
TopRight = 7,
Top = 8,
Center = 9
}
class ConsoleSprite
{
protected SpriteOrigin origin;
public int X, Y, Width, Height;
public KeyValuePair<ConsoleColor, char>[,] Sprite;
public ConsoleColor Background;
public ConsoleSprite(SpriteOrigin origin, KeyValuePair<int, int> position, int width = 0, int height = 0, KeyValuePair<ConsoleColor, char>[,] sprite = null, ConsoleColor background = ConsoleColor.Black)
{
this.origin = origin;
X = position.Key;
Y = position.Value;
Width = width;
Height = height;
Sprite = sprite;
Background = background;
}
public ConsoleSprite(ConsoleSprite sprite)
{
origin = sprite.origin;
X = sprite.X;
Y = sprite.Y;
Width = sprite.Width;
Height = sprite.Height;
Sprite = sprite.Sprite;
Background = sprite.Background;
}
public virtual void RenderSprite()
{
int writeX = X, writeY = Y;
// Set origin to render from
switch (origin)
{
case SpriteOrigin.Left:
writeY = Y - Height / 2;
break;
case SpriteOrigin.BottomLeft:
writeY = Y - Height;
break;
case SpriteOrigin.Bottom:
writeX = X - Width / 2;
writeY = Y - Height;
break;
case SpriteOrigin.BottomRight:
writeX = X - Width;
writeY = Y - Height;
break;
case SpriteOrigin.Right:
writeX = X - Width;
writeY = Y - Height / 2;
break;
case SpriteOrigin.TopRight:
writeX = X - Width;
break;
case SpriteOrigin.Top:
writeX = X - Width / 2;
break;
case SpriteOrigin.Center:
writeX = X - Width / 2;
writeY = Y - Height / 2;
break;
}
Console.BackgroundColor = Background;
// Render it
for (int y = 0; y < Height; y++)
{
Console.SetCursorPosition(writeX, writeY + y);
for (int x = 0; x < Width; x++)
{
if (Sprite[y, x].Key != Console.ForegroundColor)
{
Console.ForegroundColor = Sprite[y, x].Key;
}
Console.Out.Write(Sprite[y, x].Value);
}
}
}
// Same as RenderSprite, but it removes everything in that area
public void RemoveRender()
{
int writeX = X, writeY = Y;
switch (origin)
{
case SpriteOrigin.Left:
writeY = Y - Height / 2;
break;
case SpriteOrigin.BottomLeft:
writeY = Y - Height;
break;
case SpriteOrigin.Bottom:
writeX = X - Width / 2;
writeY = Y - Height;
break;
case SpriteOrigin.BottomRight:
writeX = X - Width;
writeY = Y - Height;
break;
case SpriteOrigin.Right:
writeX = X - Width;
writeY = Y - Height / 2;
break;
case SpriteOrigin.TopRight:
writeX = X - Width;
break;
case SpriteOrigin.Top:
writeX = X - Width / 2;
break;
case SpriteOrigin.Center:
writeX = X - Width / 2;
writeY = Y - Height / 2;
break;
}
for (int y = 0; y < Height; y++)
{
Console.SetCursorPosition(writeX, writeY + y);
for (int x = 0; x < Width; x++)
{
Console.Out.Write(' ');
}
}
}
public ConsoleSprite Clone()
{
ConsoleSprite sprite = new ConsoleSprite(this);
return sprite;
}
}
}