-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from NRG-Drink/feat/simplify-and-improve
Feat/simplify and improve
- Loading branch information
Showing
14 changed files
with
189 additions
and
195 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.