Move some processing to CC-Functions, might rework GUI too

This commit is contained in:
JFronny 2020-05-25 20:07:52 +02:00
parent 5cf9712792
commit dfe835652f
12 changed files with 116 additions and 195 deletions

View File

@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using CC_Functions.Commandline.TUI;
using Snakity.Graphics;
using Label = Snakity.Graphics.Label;
namespace Snakity
{
@ -73,7 +77,7 @@ namespace Snakity
# # #
# ## #
#####################", smooth1);
DiffDraw.Clear(level);
DiffDraw.Clear(ColorSelector.Get(level));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 2), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 2), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 3), new Point(0, 0)));
@ -100,6 +104,7 @@ namespace Snakity
Renderer.Render(smooth2);
DiffDraw.Draw(color);
t.Stop();
Thread.Sleep(500);
return t.Elapsed;
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Linq;
using Snakity.Graphics;
using CC_Functions.Misc;
using SpecialChars = CC_Functions.Misc.SpecialChars;
namespace Snakity
{
@ -8,7 +10,7 @@ namespace Snakity
{
public static Tuple<char[,], bool[,]> LoadLevel(string level, bool smooth)
{
char[,] content = Load(level);
char[,] content = level.ToNdArray2D();
int width = content.GetLength(0);
int height = content.GetLength(1);
bool[,] spawn = new bool[width, height];
@ -18,20 +20,24 @@ namespace Snakity
switch (content[x, y])
{
case ' ':
content[x, y] = SpecialChars.Space;
content[x, y] = SpecialChars.Empty;
break;
case '.':
content[x, y] = SpecialChars.Space;
content[x, y] = SpecialChars.Empty;
spawn[x, y] = true;
break;
case '#':
walls[x, y] = true;
char selected;
//Determine adjacent blocks
bool up = y > 0 && (walls[x, y - 1] || content[x, y - 1] == '#');
/*bool up = y > 0 && (walls[x, y - 1] || content[x, y - 1] == '#');
bool down = y < height - 1 && (walls[x, y + 1] || content[x, y + 1] == '#');
bool left = x > 0 && (walls[x - 1, y] || content[x - 1, y] == '#');
bool right = x < width - 1 && (walls[x + 1, y] || content[x + 1, y] == '#');
bool right = x < width - 1 && (walls[x + 1, y] || content[x + 1, y] == '#');*/
bool left = y > 0 && (walls[x, y - 1] || content[x, y - 1] == '#');
bool right = y < height - 1 && (walls[x, y + 1] || content[x, y + 1] == '#');
bool up = x > 0 && (walls[x - 1, y] || content[x - 1, y] == '#');
bool down = x < width - 1 && (walls[x + 1, y] || content[x + 1, y] == '#');
//figure out char
if (smooth)
if (up)
@ -40,17 +46,17 @@ namespace Snakity
{
if (left)
selected = right
? SpecialChars.Wall.UpDownLeftRight
: SpecialChars.Wall.UpDownLeft;
? SpecialChars.TwoLineSimple.UpDownLeftRight
: SpecialChars.TwoLineSimple.UpDownLeft;
else
selected = right ? SpecialChars.Wall.UpDownRight : SpecialChars.Wall.UpDown;
selected = right ? SpecialChars.TwoLineSimple.UpDownRight : SpecialChars.TwoLineSimple.UpDown;
}
else
{
if (left)
selected = right ? SpecialChars.Wall.UpLeftRight : SpecialChars.Wall.UpLeft;
selected = right ? SpecialChars.TwoLineSimple.UpLeftRight : SpecialChars.TwoLineSimple.UpLeft;
else
selected = right ? SpecialChars.Wall.UpRight : SpecialChars.Wall.Up;
selected = right ? SpecialChars.TwoLineSimple.UpRight : SpecialChars.TwoLineSimple.Up;
}
}
else
@ -58,16 +64,16 @@ namespace Snakity
if (down)
{
if (left)
selected = right ? SpecialChars.Wall.DownLeftRight : SpecialChars.Wall.DownLeft;
selected = right ? SpecialChars.TwoLineSimple.DownLeftRight : SpecialChars.TwoLineSimple.DownLeft;
else
selected = right ? SpecialChars.Wall.DownRight : SpecialChars.Wall.Down;
selected = right ? SpecialChars.TwoLineSimple.DownRight : SpecialChars.TwoLineSimple.Down;
}
else
{
if (left)
selected = right ? SpecialChars.Wall.LeftRight : SpecialChars.Wall.Left;
selected = right ? SpecialChars.TwoLineSimple.LeftRight : SpecialChars.TwoLineSimple.Left;
else
selected = right ? SpecialChars.Wall.Right : '#';
selected = right ? SpecialChars.TwoLineSimple.Right : '#';
}
}
else
@ -79,23 +85,5 @@ namespace Snakity
return new Tuple<char[,], bool[,]>(content, spawn);
}
public static char[,] Load(string text)
{
string[] levelArr = text.Split('\n');
int width = levelArr.Select(s => s.Length).OrderBy(s => s).Last();
int height = levelArr.Length;
char[,] tmp = new char[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
tmp[x, y] = SpecialChars.Space;
for (int i = 0; i < levelArr.Length; i++)
{
string s = levelArr[i];
for (int j = 0; j < s.Length; j++) tmp[j, i] = s[j];
}
return tmp;
}
}
}

View File

@ -1,5 +1,8 @@
using System;
using System.Linq;
using CC_Functions.Commandline.TUI;
using OneLineSimple = CC_Functions.Misc.SpecialChars.OneLineSimple;
using TwoLineSimple = CC_Functions.Misc.SpecialChars.TwoLineSimple;
namespace Snakity.Graphics
{
@ -7,35 +10,48 @@ namespace Snakity.Graphics
{
private static readonly char[] WallChars =
{
SpecialChars.Wall.Up, SpecialChars.Wall.Down, SpecialChars.Wall.Left,
SpecialChars.Wall.Right, SpecialChars.Wall.DownLeft, SpecialChars.Wall.DownRight,
SpecialChars.Wall.LeftRight, SpecialChars.Wall.UpDown, SpecialChars.Wall.UpLeft,
SpecialChars.Wall.UpRight, SpecialChars.Wall.DownLeftRight, SpecialChars.Wall.UpDownLeft,
SpecialChars.Wall.UpDownRight, SpecialChars.Wall.UpLeftRight, SpecialChars.Wall.UpDownLeftRight,
TwoLineSimple.Up, TwoLineSimple.Down, TwoLineSimple.Left,
TwoLineSimple.Right, TwoLineSimple.DownLeft, TwoLineSimple.DownRight,
TwoLineSimple.LeftRight, TwoLineSimple.UpDown, TwoLineSimple.UpLeft,
TwoLineSimple.UpRight, TwoLineSimple.DownLeftRight, TwoLineSimple.UpDownLeft,
TwoLineSimple.UpDownRight, TwoLineSimple.UpLeftRight, TwoLineSimple.UpDownLeftRight,
'#'
};
private static readonly char[] PlayerChars =
{
SpecialChars.Player.Up, SpecialChars.Player.Down, SpecialChars.Player.Left,
SpecialChars.Player.Right, SpecialChars.Player.DownLeft, SpecialChars.Player.DownRight,
SpecialChars.Player.LeftRight, SpecialChars.Player.UpDown, SpecialChars.Player.UpLeft,
SpecialChars.Player.UpRight, SpecialChars.Player.DownLeftRight, SpecialChars.Player.UpDownLeft,
SpecialChars.Player.UpDownRight, SpecialChars.Player.UpLeftRight, SpecialChars.Player.UpDownLeftRight,
OneLineSimple.Up, OneLineSimple.Down, OneLineSimple.Left,
OneLineSimple.Right, OneLineSimple.DownLeft, OneLineSimple.DownRight,
OneLineSimple.LeftRight, OneLineSimple.UpDown, OneLineSimple.UpLeft,
OneLineSimple.UpRight, OneLineSimple.DownLeftRight, OneLineSimple.UpDownLeft,
OneLineSimple.UpDownRight, OneLineSimple.UpLeftRight, OneLineSimple.UpDownLeftRight,
'P'
};
public static ConsoleColor Get(char c)
public static Pixel Get(char c)
{
Pixel p = new Pixel(c);
if (PlayerChars.Contains(c))
return ConsoleColor.Green;
if (WallChars.Contains(c) || c == '#')
return ConsoleColor.Gray;
return c switch
p.ForeColor = ConsoleColor.Green;
else if (WallChars.Contains(c) || c == '#')
p.ForeColor = ConsoleColor.Gray;
else p.ForeColor = c switch
{
SpecialChars.Enemy => ConsoleColor.Yellow,
_ => ConsoleColor.White
};
return p;
}
public static Pixel[,] Get(char[,] c)
{
int w = c.GetLength(0);
int h = c.GetLength(1);
Pixel[,] output = new Pixel[w, h];
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
output[x, y] = Get(c[x, y]);
return output;
}
}
}

View File

@ -1,57 +0,0 @@
using System;
namespace Snakity.Graphics
{
public static class DiffDraw
{
private static char[,] Screen { get; set; } = new char[0, 0];
private static char[,] _last = new char[0,0];
public static int Width => Screen.GetLength(0);
public static int Height => Screen.GetLength(1);
public static void Draw(bool color)
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
int width = Width;
int height = Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char tmp1 = Screen[x, y];
if (tmp1 == _last[x, y]) continue;
if (color)
Console.ForegroundColor = ColorSelector.Get(tmp1);
Console.CursorLeft = x;
Console.Write(tmp1);
}
Console.WriteLine();
Console.CursorLeft = 0;
}
_last = Screen;
}
public static char Get(Point p) => Get(p.X, p.Y);
public static char Get(int x, int y) => Screen[x, y];
public static void Set(Point p, char c) => Set(p.X, p.Y, c);
public static void Set(int x, int y, char c) => Screen[x, y] = c;
public static void Clear() => Clear(Width, Height);
public static void Clear(int width, int height)
{
Screen = new char[width, height];
_last = _last.Resize(width, height);
}
public static void Clear(char[,] content)
{
Screen = content;
_last = _last.Resize(Width, Height);
}
}
}

View File

@ -1,4 +1,6 @@
using System.Linq;
using System.Drawing;
using System.Linq;
using CC_Functions.Commandline.TUI;
namespace Snakity.Graphics
{
@ -22,7 +24,7 @@ namespace Snakity.Graphics
int tmpX = Position.X + j;
int tmpY = Position.Y + i;
if (tmpX < DiffDraw.Width && tmpY < DiffDraw.Height)
DiffDraw.Set(tmpX, tmpY, lines[i][j]);
DiffDraw.Set(tmpX, tmpY, ColorSelector.Get(lines[i][j]));
}
}
}

View File

@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CC_Functions.Commandline.TUI;
using OneLineSimple = CC_Functions.Misc.SpecialChars.OneLineSimple;
using TwoLineSimple = CC_Functions.Misc.SpecialChars.TwoLineSimple;
namespace Snakity.Graphics
{
@ -13,7 +17,7 @@ namespace Snakity.Graphics
public static void Render(bool smooth)
{
foreach (Label label in Labels) label.Render();
foreach (Point point in Enemies) DiffDraw.Set(point, SpecialChars.Enemy);
foreach (Point point in Enemies) DiffDraw.Set(point, ColorSelector.Get(SpecialChars.Enemy));
for (int i = 0; i < Player.Count; i++)
{
(Point position, Point direction) = Player[i];
@ -25,28 +29,28 @@ namespace Snakity.Graphics
if (Player.Count == 1)
{
if (direction == new Point(0, 1))
selected = SpecialChars.Player.Up;
selected = OneLineSimple.Up;
else if (direction == new Point(0, -1))
selected = SpecialChars.Player.Down;
selected = OneLineSimple.Down;
else if (direction == new Point(1, 0))
selected = SpecialChars.Player.Left;
selected = OneLineSimple.Left;
else if (direction == new Point(-1, 0))
selected = SpecialChars.Player.Right;
selected = OneLineSimple.Right;
else
selected = SpecialChars.Player.Down;
selected = OneLineSimple.Down;
}
else
{
Point prevPosition = Player[i + 1].Item1;
Point tmp = new Point(prevPosition.X - position.X, prevPosition.Y - position.Y);
if (tmp == new Point(0, 1))
selected = SpecialChars.Player.Down;
selected = OneLineSimple.Down;
else if (tmp == new Point(0, -1))
selected = SpecialChars.Player.Up;
selected = OneLineSimple.Up;
else if (tmp == new Point(1, 0))
selected = SpecialChars.Player.Right;
selected = OneLineSimple.Right;
else if (tmp == new Point(-1, 0))
selected = SpecialChars.Player.Left;
selected = OneLineSimple.Left;
else
throw new ArgumentException($"Unexpected: {{X={tmp.X};Y={tmp.Y}}}");
}
@ -56,13 +60,13 @@ namespace Snakity.Graphics
Point prevPosition = Player[i - 1].Item1;
Point tmp = new Point(prevPosition.X - position.X, prevPosition.Y - position.Y);
if (tmp == new Point(0, 1))
selected = SpecialChars.Player.Down;
selected = OneLineSimple.Down;
else if (tmp == new Point(0, -1))
selected = SpecialChars.Player.Up;
selected = OneLineSimple.Up;
else if (tmp == new Point(1, 0))
selected = SpecialChars.Player.Right;
selected = OneLineSimple.Right;
else if (tmp == new Point(-1, 0))
selected = SpecialChars.Player.Left;
selected = OneLineSimple.Left;
else
throw new ArgumentException($"Unexpected: {{X={tmp.X};Y={tmp.Y}}}");
}
@ -104,13 +108,13 @@ namespace Snakity.Graphics
switch (directions[1])
{
case Direction.Down:
selected = SpecialChars.Player.UpDown;
selected = OneLineSimple.UpDown;
break;
case Direction.Left:
selected = SpecialChars.Player.UpLeft;
selected = OneLineSimple.UpLeft;
break;
case Direction.Right:
selected = SpecialChars.Player.UpRight;
selected = OneLineSimple.UpRight;
break;
default:
throw new ArgumentOutOfRangeException();
@ -121,10 +125,10 @@ namespace Snakity.Graphics
switch (directions[1])
{
case Direction.Left:
selected = SpecialChars.Player.DownLeft;
selected = OneLineSimple.DownLeft;
break;
case Direction.Right:
selected = SpecialChars.Player.DownRight;
selected = OneLineSimple.DownRight;
break;
default:
throw new ArgumentOutOfRangeException();
@ -135,7 +139,7 @@ namespace Snakity.Graphics
switch (directions[1])
{
case Direction.Right:
selected = SpecialChars.Player.LeftRight;
selected = OneLineSimple.LeftRight;
break;
default:
throw new ArgumentOutOfRangeException();
@ -150,7 +154,7 @@ namespace Snakity.Graphics
else
selected = 'P';
DiffDraw.Set(position, selected);
DiffDraw.Set(position, ColorSelector.Get(selected));
}
}
}

View File

@ -3,58 +3,5 @@
public static class SpecialChars
{
public const char Enemy = '@';
public const char Space = ' ';
public static class Wall
{
// 1 connectors
public const char Up = '║';
public const char Down = '║';
public const char Left = '═';
public const char Right = '═';
// 2 connectors
public const char UpDown = '║';
public const char LeftRight = '═';
public const char DownRight = '╔';
public const char UpRight = '╚';
public const char DownLeft = '╗';
public const char UpLeft = '╝';
// 3 connectors
public const char UpDownLeft = '╣';
public const char UpDownRight = '╠';
public const char UpLeftRight = '╩';
public const char DownLeftRight = '╦';
// 4 connectors
public const char UpDownLeftRight = '╬';
}
public static class Player
{
// 1 connectors
public const char Up = '╵';
public const char Down = '╷';
public const char Left = '╴';
public const char Right = '╶';
// 2 connectors
public const char UpDown = '│';
public const char LeftRight = '─';
public const char DownRight = '┌';
public const char UpRight = '└';
public const char DownLeft = '┐';
public const char UpLeft = '┘';
// 3 connectors
public const char UpDownLeft = '┤';
public const char UpDownRight = '├';
public const char UpLeftRight = '┴';
public const char DownLeftRight = '┬';
// 4 connectors
public const char UpDownLeftRight = '┼';
}
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Drawing;
using System.Linq;
using CC_Functions.Commandline.TUI;
using Snakity.Graphics;
namespace Snakity.Loop
@ -26,7 +28,7 @@ namespace Snakity.Loop
if (Renderer.Enemies.Count > 0 && Program.Rnd.Next(50) != 0 || Renderer.Enemies.Count > 4) continue;
Point tmp1 = new Point(Program.Rnd.Next(DiffDraw.Width), Program.Rnd.Next(DiffDraw.Height));
int attempts = 0;
while (!spawnMap[tmp1.X, tmp1.Y] && attempts < 5)
while (!spawnMap[tmp1.Y, tmp1.X] && attempts < 5)
{
tmp1.X = Program.Rnd.Next(DiffDraw.Width);
tmp1.Y = Program.Rnd.Next(DiffDraw.Height);

View File

@ -1,4 +1,6 @@
using System;
using System.Drawing;
using CC_Functions.Commandline.TUI;
using Snakity.Graphics;
namespace Snakity.Loop
@ -66,7 +68,7 @@ namespace Snakity.Loop
Console.ReadKey();
if (headDelta != new Point(0, 0) && (Renderer.Player.Count < 2 ||
headDelta != Renderer.Player[1].Item1 - Renderer.Player[0].Item1))
headDelta != Renderer.Player[1].Item1.m(Renderer.Player[0].Item1)))
Renderer.Player[0] = new Tuple<Point, Point>(Renderer.Player[0].Item1, headDelta);
}
@ -79,15 +81,15 @@ namespace Snakity.Loop
_lastCheck += _delay;
Point head = Renderer.Player[0].Item1;
Point headDelta = Renderer.Player[0].Item2;
if (headDelta.Y == -1 && (head.Y == 0 || !CheckPoint(head + new Point(0, -1))))
if (headDelta.Y == -1 && (head.Y == 0 || !CheckPoint(head.p(new Point(0, -1)))))
return true;
if (headDelta.X == -1 && (head.X == 0 || !CheckPoint(head + new Point(-1, 0))))
if (headDelta.X == -1 && (head.X == 0 || !CheckPoint(head.p(new Point(-1, 0)))))
return true;
if (headDelta.Y == 1 && (head.Y >= DiffDraw.Height - 1 || !CheckPoint(head + new Point(0, 1))))
if (headDelta.Y == 1 && (head.Y >= DiffDraw.Height - 1 || !CheckPoint(head.p(new Point(0, 1)))))
return true;
if (headDelta.X == 1 && (head.X >= DiffDraw.Width - 1 || !CheckPoint(head + new Point(1, 0))))
if (headDelta.X == 1 && (head.X >= DiffDraw.Width - 1 || !CheckPoint(head.p(new Point(1, 0)))))
return true;
Renderer.Player.Insert(0, new Tuple<Point, Point>(Renderer.Player[0].Item1 + headDelta, headDelta));
Renderer.Player.Insert(0, new Tuple<Point, Point>(Renderer.Player[0].Item1.p(headDelta), headDelta));
if (_shouldIncrease)
{
_shouldIncrease = false;
@ -107,8 +109,11 @@ namespace Snakity.Loop
_delay = new TimeSpan(3000000);
_lastCheck = DateTime.Now;;
}
private static Point p(this Point pt, Point pt2) => new Point(pt.X + pt2.X, pt.Y + pt2.Y);
private static Point m(this Point pt, Point pt2) => new Point(pt.X - pt2.X, pt.Y - pt2.Y);
private static bool CheckPoint(Point point) =>
DiffDraw.Get(point) == SpecialChars.Space || DiffDraw.Get(point) == SpecialChars.Enemy;
DiffDraw.Get(point) == CC_Functions.Misc.SpecialChars.Empty || DiffDraw.Get(point) == SpecialChars.Enemy;
}
}

View File

@ -1,7 +1,11 @@
using System;
using System.Drawing;
using System.Linq;
using System.Threading;
using CC_Functions.Commandline.TUI;
using CC_Functions.Misc;
using Snakity.Graphics;
using Label = Snakity.Graphics.Label;
namespace Snakity.Loop
{
@ -79,7 +83,9 @@ Play again? (y/n)");
{
Console.Clear();
bool playing = true;
DiffDraw.Clear(2, 2);
Pixel[,] init = new Pixel[1,2];
init.Populate(new Pixel());
DiffDraw.Clear(init);
DiffDraw.Draw(false);
Label scoreLabel = new Label(new Point(0, 0), "");
Renderer.Labels.Clear();
@ -91,12 +97,12 @@ Play again? (y/n)");
Renderer.Player.Clear();
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 1), new Point(0, 1)));
Renderer.Enemies.Clear();
DiffDraw.Clear((char[,]) level.Clone());
DiffDraw.Clear(ColorSelector.Get(level));
DiffDraw.Draw(SettingsMan.Color);
Input.Reset();
while (playing)
{
DiffDraw.Clear((char[,]) level.Clone());
DiffDraw.Clear(ColorSelector.Get(level));
scoreLabel.Text = $"Score: {_score}";
Renderer.Render(SettingsMan.SmoothPlayer);
Input.Get();
@ -115,7 +121,7 @@ Play again? (y/n)");
DiffDraw.Clear();
DiffDraw.Draw(SettingsMan.Color);
(level, spawnable) = CharArrayLoader.LoadLevel(Levels.levels[Rnd.Next(Levels.levels.Length)], SettingsMan.SmoothTerrain);
DiffDraw.Clear((char[,]) level.Clone());
DiffDraw.Clear(ColorSelector.Get(level));
Renderer.Player.Clear();
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 2), new Point(0, 1)));
Renderer.Enemies.Clear();

View File

@ -1,4 +1,4 @@
using System;
/*using System;
namespace Snakity
{
@ -27,4 +27,4 @@ namespace Snakity
public static Point operator -(Point left, Point right) => new Point(left.X - right.X, left.Y - right.Y);
}
}
}*/

View File

@ -13,4 +13,7 @@
if [[ -f "$(SolutionDir)Data/pkgtool.exe" ]]; then $(SolutionDir)Data/pkgtool.exe build --noLogo --binDir .; else echo Cound not find Package build tools, skipping; fi
</PostBuildEvent>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CC-Functions.Commandline" Version="1.1.7450.31626" />
</ItemGroup>
</Project>