diff --git a/UICatalog/Scenarios/Mazing.cs b/UICatalog/Scenarios/Mazing.cs new file mode 100644 index 000000000..87606cecc --- /dev/null +++ b/UICatalog/Scenarios/Mazing.cs @@ -0,0 +1,405 @@ +#nullable enable +using System.Text; +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +[ScenarioMetadata ("A Mazing", "Illustrates how to make a basic maze game.")] +[ScenarioCategory ("Drawing")] +[ScenarioCategory ("Mouse and KeyBoard")] +[ScenarioCategory ("Games")] +public class Mazing : Scenario +{ + private Toplevel? _top; + private MazeGenerator? _m; + + private List? _potions; + private List? _goblins; + private string? _message; + private bool _dead; + + public override void Main () + { + Application.Init (); + _top = new (); + + _m = new (); + + GenerateNpcs (); + + // Define the keys for movement + _top.KeyBindings.Add (Key.CursorLeft, Command.Left); + _top.KeyBindings.Add (Key.CursorRight, Command.Right); + _top.KeyBindings.Add (Key.CursorUp, Command.Up); + _top.KeyBindings.Add (Key.CursorDown, Command.Down); + + // Changing the key-bindings of a View is not allowed, however, + // by default, Toplevel does't bind any of our movement keys, so + // we can take advantage of the CommandNotBound event to handle them + // + // An alternative implementation would be to create a TopLevel subclass that + // calls AddCommand/KeyBindings.Add in the constructor. See the Snake game scenario + // for an example. + _top.CommandNotBound += TopCommandNotBound; + + _top.DrawingContent += (s, _) => + { + if (s is not Toplevel top) + { + return; + } + + // Build maze + var lc = new LineCanvas (_m.BuildWallLinesFromMaze ()); + + // Print maze + foreach (KeyValuePair p in lc.GetMap ()) + { + top.Move (p.Key.X, p.Key.Y); + top.AddRune (p.Value); + } + + // Draw objects + top.Move (_m.Start.X, _m.Start.Y); + top.AddStr ("s"); + + top.Move (_m.End.X, _m.End.Y); + top.AddStr ("e"); + + top.Move (_m.Player.X, _m.Player.Y); + top.SetAttribute (new (Color.Cyan, top.GetNormalColor ().Background)); + top.AddStr (_dead ? "x" : "@"); + + // Draw goblins + foreach (Point goblin in _goblins!) + { + top.Move (goblin.X, goblin.Y); + top.SetAttribute (new (Color.Red, top.GetNormalColor ().Background)); + top.AddStr ("G"); + } + + // Draw potions + foreach (Point potion in _potions!) + { + top.Move (potion.X, potion.Y); + top.SetAttribute (new (Color.Yellow, top.GetNormalColor ().Background)); + top.AddStr ("p"); + } + + // Draw UI + top.SetAttribute (top.GetNormalColor ()); + + var g = new Gradient ([new (Color.Red), new (Color.BrightGreen)], [10]); + top.Move (_m.MazeWidth + 1, 0); + top.AddStr ("Name: Sir Flibble"); + top.Move (_m.MazeWidth + 1, 1); + top.AddStr ("HP:"); + + for (var i = 0; i < _m.PlayerHp; i++) + { + top.Move (_m.MazeWidth + 1 + "HP:".Length + i, 1); + top.SetAttribute (new (g.GetColorAtFraction (i / 20f))); + top.AddRune ('█'); + } + + top.SetAttribute (top.GetNormalColor ()); + + if (!string.IsNullOrWhiteSpace (_message)) + { + top.Move (_m.MazeWidth + 2, 2); + top.AddStr (_message); + } + }; + + Application.Run (_top); + + _top.Dispose (); + Application.Shutdown (); + } + + private void GenerateNpcs () + { + _goblins = _m?.GenerateSpawnLocations (3, []); // Generate 3 goblins + _potions = _m?.GenerateSpawnLocations (3, _goblins!); // Generate 3 potions + } + + private void TopCommandNotBound (object? sender, CommandEventArgs e) + { + if (_dead) + { + return; + } + + Point newPos = _m.Player; + + Command? command = e.Context?.Command; + + if (command == Command.Left) + { + newPos = _m.Player with { X = _m.Player.X - 1 }; + } + + if (command == Command.Right) + { + newPos = _m.Player with { X = _m.Player.X + 1 }; + } + + if (command == Command.Up) + { + newPos = _m.Player with { Y = _m.Player.Y - 1 }; + } + + if (command == Command.Down) + { + newPos = _m.Player with { Y = _m.Player.Y + 1 }; + } + + // Only move if in bounds and it's a path + if (newPos.X >= 0 && newPos.X < _m._maze.GetLength (1) && newPos.Y >= 0 && newPos.Y < _m._maze.GetLength (0) && _m._maze [newPos.Y, newPos.X] == 0) + { + _m.Player = newPos; + + // Check if player is on a goblin + if (_goblins!.Contains (_m.Player)) + { + _message = "You fight a goblin!"; + _m.PlayerHp -= 5; // Decrease player's HP when attacked + + // Remove the goblin + _goblins.Remove (_m.Player); + + // Check if player is dead + if (_m.PlayerHp <= 0) + { + _message = "You died!"; + Application.Top!.SetNeedsDraw (); // trigger redraw + _dead = true; + + return; // Stop further action if dead + } + } + else if (_potions!.Contains (_m.Player)) + { + _message = "You drink a health potion!"; + _m.PlayerHp = Math.Min (20, _m.PlayerHp + 5); // increase player's HP when drinking potion + + // Remove the potion + _potions.Remove (_m.Player); + } + else + { + _message = string.Empty; + } + + Application.Top!.SetNeedsDraw (); // trigger redraw + } + + // Optional win condition: + if (_m.Player == _m.End) + { + var hp = _m.PlayerHp; + _m = new (); // Generate a new maze + _m.PlayerHp = hp; + GenerateNpcs (); + Application.Top!.SetNeedsDraw (); // trigger redraw + } + } +} + +internal class MazeGenerator +{ + private const int WIDTH = 20; + private const int HEIGHT = 10; + public int [,] _maze; + public Random Rand { get; } = new (); + public Point Start { get; } + public Point End { get; } + public Point Player { get; set; } + public int PlayerHp { get; set; } = 20; + + // Private accessors for width and height + public int MazeWidth => WIDTH * 2 + 1; + public int MazeHeight => HEIGHT * 2 + 1; + + public MazeGenerator () + { + int w = WIDTH * 2 + 1; + int h = HEIGHT * 2 + 1; + _maze = new int [h, w]; + + // Fill with walls + for (var y = 0; y < h; y++) + for (var x = 0; x < w; x++) + { + _maze [y, x] = 1; + } + + // Start carving from a random odd cell + int startX = Rand.Next (WIDTH) * 2 + 1; + int startY = Rand.Next (HEIGHT) * 2 + 1; + Carve (new (startX, startY)); + + // Set random entrance + Start = GetRandomEdgePoint (w, h, true); + _maze [Start.Y, Start.X] = 0; + Player = Start; + + // Set random exit (ensure it's not same as entrance) + End = GetRandomEdgePoint (w, h, false, Start.X, Start.Y); + _maze [End.Y, End.X] = 0; + } + + public List BuildWallLinesFromMaze () + { + List lines = new (); + + int h = _maze.GetLength (0); + int w = _maze.GetLength (1); + + // Horizontal lines + for (var y = 0; y < h; y++) + { + var x = 0; + + while (x < w) + { + if (_maze [y, x] == 1) + { + int startX = x; + + while (x < w && _maze [y, x] == 1) + { + x++; + } + + int length = x - startX; + + if (length > 1) + { + lines.Add (new (new (startX, y), length, Orientation.Horizontal, LineStyle.Single)); + } + } + else + { + x++; + } + } + } + + // Vertical lines + for (var x = 0; x < w; x++) + { + var y = 0; + + while (y < h) + { + if (_maze [y, x] == 1) + { + int startY = y; + + while (y < h && _maze [y, x] == 1) + { + y++; + } + + int length = y - startY; + lines.Add (new (new (x, startY), length, Orientation.Vertical, LineStyle.Single)); + } + else + { + y++; + } + } + } + + return lines; + } + + public List GenerateSpawnLocations (int count, List exclude) + { + // Create a new copy of the list so we can track exclusions + exclude = exclude.ToList (); + + List locations = new (); + + for (var i = 0; i < count; i++) + { + Point point; + + do + { + point = new (Rand.Next (1, WIDTH * 2), Rand.Next (1, HEIGHT * 2)); + } + + // Ensure the spawn point is not in the exclusion list and it's an open space (not a wall) + while (exclude.Contains (point) || _maze [point.Y, point.X] != 0); + + exclude.Add (point); // Mark this location as occupied + locations.Add (point); // Add the location to the list + } + + return locations; + } + + private void Carve (Point p) + { + _maze [p.Y, p.X] = 0; + + int [] [] dirs = + { + [0, -2], + [0, 2], + [-2, 0], + [2, 0] + }; + + Shuffle (dirs); + + foreach (int [] dir in dirs) + { + int nx = p.X + dir [0], ny = p.Y + dir [1]; + + if (nx > 0 && ny > 0 && nx < WIDTH * 2 && ny < HEIGHT * 2 && _maze [ny, nx] == 1) + { + _maze [p.Y + dir [1] / 2, p.X + dir [0] / 2] = 0; + Carve (new (nx, ny)); + } + } + } + + private void Shuffle (int [] [] array) + { + for (int i = array.Length - 1; i > 0; i--) + { + int j = Rand.Next (i + 1); + int [] temp = array [i]; + array [i] = array [j]; + array [j] = temp; + } + } + + private Point GetRandomEdgePoint (int w, int h, bool isEntrance, int avoidX = -1, int avoidY = -1) + { + List candidates = []; + + for (var i = 1; i < h - 1; i += 2) + { + candidates.Add (new (0, i)); // Left edge + candidates.Add (new (w - 1, i)); // Right edge + } + + for (var i = 1; i < w - 1; i += 2) + { + candidates.Add (new (i, 0)); // Top edge + candidates.Add (new (i, h - 1)); // Bottom edge + } + + // Remove one if same as entrance + if (!isEntrance) + { + candidates.RemoveAll (p => p.X == avoidX && p.Y == avoidY); + } + + return candidates [Rand.Next (candidates.Count)]; + } +} diff --git a/UICatalog/Scenarios/Snake.cs b/UICatalog/Scenarios/Snake.cs index 3ff04241d..04538885b 100644 --- a/UICatalog/Scenarios/Snake.cs +++ b/UICatalog/Scenarios/Snake.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Text; -using System.Threading.Tasks; using Terminal.Gui; namespace UICatalog.Scenarios; @@ -11,9 +7,10 @@ namespace UICatalog.Scenarios; [ScenarioMetadata ("Snake", "The game of apple eating.")] [ScenarioCategory ("Colors")] [ScenarioCategory ("Drawing")] +[ScenarioCategory ("Games")] public class Snake : Scenario { - private bool isDisposed; + private bool _isDisposed; public override void Main () { @@ -33,7 +30,7 @@ public class Snake : Scenario Task.Run ( () => { - while (!isDisposed) + while (!_isDisposed) { sw.Restart (); @@ -60,7 +57,7 @@ public class Snake : Scenario protected override void Dispose (bool disposing) { - isDisposed = true; + _isDisposed = true; base.Dispose (disposing); } @@ -170,7 +167,7 @@ public class Snake : Scenario var middle = new Point (width / 2, height / 2); // Start snake with a length of 2 - Snake = new List { middle, middle }; + Snake = new () { middle, middle }; Apple = GetNewRandomApplePoint (); SleepAfterAdvancingState = StartingSpeed; @@ -198,19 +195,19 @@ public class Snake : Scenario switch (CurrentDirection) { case Direction.Left: - return new Point (Head.X - 1, Head.Y); + return new (Head.X - 1, Head.Y); case Direction.Right: - return new Point (Head.X + 1, Head.Y); + return new (Head.X + 1, Head.Y); case Direction.Up: - return new Point (Head.X, Head.Y - 1); + return new (Head.X, Head.Y - 1); case Direction.Down: - return new Point (Head.X, Head.Y + 1); + return new (Head.X, Head.Y + 1); } - throw new Exception ("Unknown direction"); + throw new ("Unknown direction"); } private Point GetNewRandomApplePoint () @@ -302,7 +299,7 @@ public class Snake : Scenario State = state; CanFocus = true; - ColorScheme = new ColorScheme + base.ColorScheme = new () { Normal = white, Focus = white, @@ -310,21 +307,40 @@ public class Snake : Scenario HotFocus = white, Disabled = white }; + + KeyBindings.Add (Key.CursorLeft, Command.Left); + KeyBindings.Add (Key.CursorRight, Command.Right); + KeyBindings.Add (Key.CursorUp, Command.Up); + KeyBindings.Add (Key.CursorDown, Command.Down); + + AddCommand (Command.Left, () => SetDirection (Direction.Left)); + AddCommand (Command.Right, () => SetDirection (Direction.Right)); + AddCommand (Command.Up, () => SetDirection (Direction.Up)); + AddCommand (Command.Down, () => SetDirection (Direction.Down)); + + return; + + bool? SetDirection (Direction direction) + { + State.PlannedDirection = direction; + + return true; + } } - public SnakeState State { get; } + private SnakeState State { get; } protected override bool OnDrawingContent () { SetAttribute (white); - ClearViewport (null); + ClearViewport (); var canvas = new LineCanvas (); canvas.AddLine (Point.Empty, State.Width, Orientation.Horizontal, LineStyle.Double); canvas.AddLine (Point.Empty, State.Height, Orientation.Vertical, LineStyle.Double); - canvas.AddLine (new Point (0, State.Height - 1), State.Width, Orientation.Horizontal, LineStyle.Double); - canvas.AddLine (new Point (State.Width - 1, 0), State.Height, Orientation.Vertical, LineStyle.Double); + canvas.AddLine (new (0, State.Height - 1), State.Width, Orientation.Horizontal, LineStyle.Double); + canvas.AddLine (new (State.Width - 1, 0), State.Height, Orientation.Vertical, LineStyle.Double); for (var i = 1; i < State.Snake.Count; i++) { @@ -355,39 +371,5 @@ public class Snake : Scenario return true; } - - // BUGBUG: Should (can) this use key bindings instead. - protected override bool OnKeyDown (Key key) - { - if (key.KeyCode == KeyCode.CursorUp) - { - State.PlannedDirection = Direction.Up; - - return true; - } - - if (key.KeyCode == KeyCode.CursorDown) - { - State.PlannedDirection = Direction.Down; - - return true; - } - - if (key.KeyCode == KeyCode.CursorLeft) - { - State.PlannedDirection = Direction.Left; - - return true; - } - - if (key.KeyCode == KeyCode.CursorRight) - { - State.PlannedDirection = Direction.Right; - - return true; - } - - return false; - } } }