Skip to content

Commit

Permalink
Merge pull request #4 from NRG-Drink/feat/simplify-and-improve
Browse files Browse the repository at this point in the history
Feat/simplify and improve
  • Loading branch information
NRG-Drink authored May 30, 2024
2 parents 611f94b + fbaedb1 commit be8ac73
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 195 deletions.
73 changes: 0 additions & 73 deletions NRG.Matrix.App/NRG.Matrix.App/DisplayObject.cs

This file was deleted.

15 changes: 0 additions & 15 deletions NRG.Matrix.App/NRG.Matrix.App/Models/Option.cs

This file was deleted.

15 changes: 0 additions & 15 deletions NRG.Matrix.App/NRG.Matrix.App/NRG.Matrix.App.csproj

This file was deleted.

31 changes: 0 additions & 31 deletions NRG.Matrix.App/NRG.Matrix.App/Program.cs

This file was deleted.

7 changes: 0 additions & 7 deletions NRG.Matrix.App/NRG.Matrix.App/Properties/launchSettings.json

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34707.107
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NRG.Matrix.App", "NRG.Matrix.App\NRG.Matrix.App.csproj", "{8B4976A1-F760-4945-B0E8-10A618690D20}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NRG.Matrix", "NRG.Matrix\NRG.Matrix.csproj", "{8B4976A1-F760-4945-B0E8-10A618690D20}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
73 changes: 73 additions & 0 deletions NRG.Matrix/NRG.Matrix/DisplayObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Drawing;

namespace NRG.Matrix.App;

public record DisplayObject
{
private static readonly Random _random = new();
private readonly int _traceLength = 20 + _random.Next(0, 11);
private string? _symbol;
private string _lastRandom = GetRandomSymbol();

public DisplayObject(int xRange)
{
Pos = new Point(_random.Next(0, xRange), 0);
IsTrace = false;
Color = OtherColor;
}

public DisplayObject(Point pos, int yOffset)
{
Pos = pos with { Y = pos.Y - yOffset };
IsTrace = true;
Color = TraceColor;
}

public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGreen;
public static ConsoleColor OtherColor { get; set; } = ConsoleColor.Gray;

public Point Pos { get; init; } = new(999, 999);
public ConsoleColor Color { get; init; } = ConsoleColor.Gray;
public bool IsTrace { get; init; } = false;

public string Symbol
{
get => _symbol is null ? GetNextRandomSymbol() : _symbol;
init => _symbol = value;
}

public DisplayObject Fall(int x = 0, int y = 1)
=> this with { Pos = new Point(Pos.X + x, Pos.Y + y) };

public IEnumerable<DisplayObject> Trace()
=> Enumerable.Range(1, _traceLength)
.Select(GetTrace)
.Append(GetClear(_traceLength + 1));

private DisplayObject GetTrace(int offset)
=> new(Pos, offset);

private DisplayObject GetClear(int offset)
=> new(Pos, offset) { Symbol = " " };

private string GetNextRandomSymbol()
{
var getRandom = ShouldGetNewRandom();
if (!IsTrace && !getRandom)
{
// Double chance for changed symbol on non trace object.
getRandom = ShouldGetNewRandom();
}
if (getRandom)
{
_lastRandom = GetRandomSymbol();
}
return _lastRandom;
}

private static string GetRandomSymbol()
=> ((char)_random.Next(33, 123)).ToString();

private static bool ShouldGetNewRandom()
=> _random.Next(0, 3) == 0;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,68 +1,59 @@
using System;
using System.Diagnostics;
using NRG.Matrix.App.Models;
using System.Text;

namespace NRG.Matrix.App;

public class Matrix(int delay = 80, TimeSpan? time = null, int maxObjects = 9999, Func<int, float>? objectAddRate = null)
public class Matrix(Option option)
{
private readonly bool _isEndlessMode = time is null;

private List<DisplayObject> list = [];
private List<DisplayObject> _displayObjects = [];
private (int Width, int Height) _windowDimension = (Console.WindowWidth, Console.WindowHeight);
private int _frames = 0;
private float _objectBuildup = 1;

private readonly float _addRate = option.AddRate;
private readonly int _delay = option.Delay < 0 ? 0 : option.Delay;
private readonly int _maxObjects = option.MaxObjects;
private float _objectBuildup = 1;

public void Enter()
{
delay = delay < 0 ? 0 : delay;
objectAddRate ??= e => e / 200.0f;
try
{
Console.Clear();
var sw = Stopwatch.StartNew();
while (_isEndlessMode || sw.Elapsed < time)
while (true)
{
AddObjects(Console.WindowWidth);
HandleObjects(Console.WindowHeight);
PrintObjects();

if (!_isEndlessMode)
{
HandleBenchmarkMode();
}

Task.Delay(delay).Wait();
Task.Delay(_delay).Wait();
}
}
finally
{
LeaveMatrix(delay, time);
LeaveMatrix(_delay);
}
}

private void AddObjects(int width)
{
if (maxObjects < list.Count)
if (_maxObjects < _displayObjects.Count)
{
return;
}

_objectBuildup += objectAddRate!(width);
_objectBuildup += width * _addRate / 200;
var objectsToAdd = (int)_objectBuildup;
for (int i = 0; i < objectsToAdd; i++)
{
for (int i = 0; i < objectsToAdd; i++)
{
var obj = new DisplayObject(width);
list.Add(obj);
list.AddRange(obj.Trace());
}
_displayObjects.Add(obj);
_displayObjects.AddRange(obj.Trace());
}
_objectBuildup -= objectsToAdd;
}

private void HandleObjects(int height)
{
list = list
_displayObjects = _displayObjects
.Select(e => e.Fall())
.Where(e => e.Pos.Y < height)
.OrderBy(e => e.IsTrace)
Expand All @@ -74,9 +65,8 @@ private void PrintObjects()
{
try
{
HandleWindowSizeChange();
Console.CursorVisible = false;
var validColors = list.Where(e => e.Pos.Y >= 0).GroupBy(e => e.Color);
var validColors = _displayObjects.Where(e => e.Pos.Y >= 0).GroupBy(e => e.Color);

var traces = validColors.FirstOrDefault(e => e.Key is ConsoleColor.DarkGreen);
PrintTrace(traces);
Expand Down Expand Up @@ -141,38 +131,39 @@ private void PrintOthers(IEnumerable<IGrouping<ConsoleColor, DisplayObject>> oth
private void HandleWindowSizeChange()
{
var hasWindowSizeChanges = _windowDimension.Width != Console.WindowWidth || _windowDimension.Height != Console.WindowHeight;
if (hasWindowSizeChanges)
if (!hasWindowSizeChanges)
{
Console.Clear();
list = list
.Where(e => e.Pos.X < Console.WindowWidth)
.Where(e => e.Pos.Y < Console.WindowHeight)
.ToList();
_windowDimension = (Console.WindowWidth, Console.WindowHeight);
return;
}
Console.Clear();
_displayObjects = _displayObjects
.Where(e => e.Pos.X < Console.WindowWidth)
.Where(e => e.Pos.Y < Console.WindowHeight)
.ToList();
_windowDimension = (Console.WindowWidth, Console.WindowHeight);
}

private void HandleBenchmarkMode()
{
Console.SetCursorPosition(5, 1);
Console.Write($"Number of objects in RAM: {list.Count} ");
_frames++;
}
//private void HandleBenchmarkMode()
//{
// Console.SetCursorPosition(5, 1);
// Console.Write($"Number of objects in RAM: {_displayObjects.Count} ");
// _frames++;
//}

private void LeaveMatrix(int delay, TimeSpan? time)
private void LeaveMatrix(int delay, TimeSpan? time = null)
{
Console.CursorVisible = true;
Console.ForegroundColor = ConsoleColor.Gray;
Console.SetCursorPosition(0, Console.WindowHeight + 1);
if (!_isEndlessMode)
{
Console.WriteLine(
$"Rendered Frames: {_frames} " +
$"of possible {time!.Value.TotalMilliseconds / delay} " +
$"in time {time} (hh:MM:ss)\n" +
$"With an average calculation time of " +
$"{((time!.Value.TotalMilliseconds - delay * _frames) / _frames):0.0} ms per frame"
);
}
//if (!_isEndlessMode)
//{
// Console.WriteLine(
// $"Rendered Frames: {_frames} " +
// $"of possible {time!.Value.TotalMilliseconds / delay} " +
// $"in time {time} (hh:MM:ss)\n" +
// $"With an average calculation time of " +
// $"{((time!.Value.TotalMilliseconds - delay * _frames) / _frames):0.0} ms per frame"
// );
//}
}
}
Loading

0 comments on commit be8ac73

Please sign in to comment.