Add new Views system
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using CoopSweeper.GameTypes;
|
||||
using CoopSweeper.View;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
@@ -7,20 +9,23 @@ namespace CoopSweeper
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static IField[,] currentMap = null;
|
||||
const int MAP_POS_X = 3;
|
||||
const int MAP_POS_Y = 3;
|
||||
|
||||
static List<IView> _views = new List<IView>();
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.SetCursorPosition(0, 0);
|
||||
Console.CursorVisible = false;
|
||||
Console.Clear();
|
||||
var game = new Game();
|
||||
var cursorPosX = 0;
|
||||
var cursorPosY = 0;
|
||||
StartNewGame(game, cursorPosX, cursorPosY);
|
||||
_views.Add(new GameView(game) { Position = new Position(1, 3) });
|
||||
_views.Add(new MenuBarView() { Position = new Position(0, 2) });
|
||||
StartNewGame(game);
|
||||
game.GameFinished += won =>
|
||||
{
|
||||
Console.SetCursorPosition(0, 0);
|
||||
@@ -28,201 +33,40 @@ namespace CoopSweeper
|
||||
Console.Write("You won!");
|
||||
else
|
||||
Console.Write("You lost!");
|
||||
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, true);
|
||||
Console.ReadKey();
|
||||
StartNewGame(game, cursorPosX, cursorPosY);
|
||||
Draw(false);
|
||||
Console.ReadKey(true);
|
||||
StartNewGame(game);
|
||||
};
|
||||
ConsoleKey key = (ConsoleKey)(-1);
|
||||
while ((key = Console.ReadKey().Key) != ConsoleKey.Escape)
|
||||
Draw(true);
|
||||
while ((key = Console.ReadKey(true).Key) != ConsoleKey.Escape)
|
||||
{
|
||||
var oldCursorPosX = cursorPosX;
|
||||
var oldCursorPosY = cursorPosY;
|
||||
bool fullRedraw = false;
|
||||
switch(key)
|
||||
{
|
||||
case ConsoleKey.UpArrow:
|
||||
cursorPosY--; break;
|
||||
case ConsoleKey.DownArrow:
|
||||
cursorPosY++; break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
cursorPosX--; break;
|
||||
case ConsoleKey.RightArrow:
|
||||
cursorPosX++; break;
|
||||
case ConsoleKey.Spacebar:
|
||||
fullRedraw = true;
|
||||
game.Reveal(cursorPosX, cursorPosY); break;
|
||||
case ConsoleKey.F:
|
||||
fullRedraw = true;
|
||||
game.ToggleMark(cursorPosX, cursorPosY); break;
|
||||
}
|
||||
if (cursorPosX < 0) cursorPosX = 0;
|
||||
if (cursorPosY < 0) cursorPosY = 0;
|
||||
if (cursorPosX >= game.Map.GetLength(0)) cursorPosX = game.Map.GetLength(0) - 1;
|
||||
if (cursorPosY >= game.Map.GetLength(1)) cursorPosY = game.Map.GetLength(1) - 1;
|
||||
if (fullRedraw)
|
||||
DrawMap(3, 3, game.Map, cursorPosX, cursorPosY, true);
|
||||
else if(oldCursorPosX != cursorPosX || oldCursorPosY != cursorPosY)
|
||||
{
|
||||
Console.SetCursorPosition(oldCursorPosX + 3, oldCursorPosY + 3);
|
||||
DrawChar(game.Map[oldCursorPosX, oldCursorPosY], false);
|
||||
Console.SetCursorPosition(cursorPosX + 3, cursorPosY + 3);
|
||||
DrawChar(game.Map[cursorPosX, cursorPosY], true);
|
||||
}
|
||||
HandleKeyEvent(key);
|
||||
Draw(false);
|
||||
Console.SetCursorPosition(0, 0);
|
||||
Console.CursorVisible = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawChar(IField f, bool isCursor)
|
||||
private static void Draw(bool fullRedraw)
|
||||
{
|
||||
var oldBg = Console.BackgroundColor;
|
||||
var oldFg = Console.ForegroundColor;
|
||||
bool fgChanged = false;
|
||||
bool bgChanged = false;
|
||||
var c = 'E';
|
||||
var state = f.DisplayState;
|
||||
switch (state) {
|
||||
case DisplayState.EMPTY:
|
||||
c = ' ';
|
||||
break;
|
||||
case DisplayState.NONE:
|
||||
bgChanged = true;
|
||||
fgChanged = true;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
Console.BackgroundColor = ConsoleColor.Gray;
|
||||
//c = '◌';
|
||||
c = ' ';
|
||||
break;
|
||||
case DisplayState.QUESTIONMARK:
|
||||
fgChanged = true;
|
||||
bgChanged = true;
|
||||
Console.BackgroundColor = ConsoleColor.Gray;
|
||||
Console.ForegroundColor = ConsoleColor.DarkBlue;
|
||||
c = '?';
|
||||
break;
|
||||
case DisplayState.BOMB:
|
||||
fgChanged = true;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
c = '☼';
|
||||
break;
|
||||
case DisplayState.ERROR:
|
||||
c = 'e';
|
||||
break;
|
||||
case DisplayState.FLAG:
|
||||
fgChanged = true;
|
||||
bgChanged = true;
|
||||
Console.BackgroundColor = ConsoleColor.Gray;
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
c = 'F';
|
||||
break;
|
||||
case DisplayState.NUMBER1:
|
||||
case DisplayState.NUMBER2:
|
||||
case DisplayState.NUMBER3:
|
||||
case DisplayState.NUMBER4:
|
||||
case DisplayState.NUMBER5:
|
||||
case DisplayState.NUMBER6:
|
||||
case DisplayState.NUMBER7:
|
||||
case DisplayState.NUMBER8:
|
||||
fgChanged = true;
|
||||
switch(state)
|
||||
{
|
||||
case DisplayState.NUMBER1:
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
break;
|
||||
case DisplayState.NUMBER2:
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
break;
|
||||
case DisplayState.NUMBER3:
|
||||
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
||||
break;
|
||||
case DisplayState.NUMBER4:
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
break;
|
||||
default:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
break;
|
||||
}
|
||||
c = ((int)state).ToString()[0];
|
||||
break;
|
||||
}
|
||||
if (isCursor)
|
||||
{
|
||||
fgChanged = true;
|
||||
bgChanged = true;
|
||||
var s = Console.ForegroundColor;
|
||||
Console.ForegroundColor = Console.BackgroundColor;
|
||||
Console.BackgroundColor = s;
|
||||
}
|
||||
Console.Write(c);
|
||||
if (fgChanged)
|
||||
Console.ForegroundColor = oldFg;
|
||||
if (bgChanged)
|
||||
Console.BackgroundColor = oldBg;
|
||||
foreach(var view in _views)
|
||||
view.Draw(fullRedraw);
|
||||
}
|
||||
|
||||
public static void StartNewGame(Game game, int cursorPosX, int cursorPosY)
|
||||
private static void HandleKeyEvent(ConsoleKey key)
|
||||
{
|
||||
foreach(var view in _views)
|
||||
if (view.HandleKeyEvent(key))
|
||||
break;
|
||||
}
|
||||
|
||||
public static void StartNewGame(Game game)
|
||||
{
|
||||
game.GenerateGame(100, 30, 15);
|
||||
Console.Clear();
|
||||
Console.SetCursorPosition(0, 0);
|
||||
DrawBorder(MAP_POS_X - 1, MAP_POS_X - 1, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2);
|
||||
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, false);
|
||||
}
|
||||
|
||||
private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY, bool doChangeDetection)
|
||||
{
|
||||
for (var y = 0; y < map.GetLength(1); y++)
|
||||
{
|
||||
if(currentMap == null || !doChangeDetection) Console.SetCursorPosition(posX, posY + y);
|
||||
for (var x = 0; x < map.GetLength(0); x++)
|
||||
{
|
||||
if (currentMap != null && doChangeDetection)
|
||||
{
|
||||
var currentField = currentMap[x, y];
|
||||
var newField = map[x, y];
|
||||
if (currentField == null || currentField.DisplayState != newField.DisplayState)
|
||||
{
|
||||
Console.SetCursorPosition(posX + x, posY + y);
|
||||
DrawChar(map[x, y], x == cursorX && y == cursorY);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawChar(map[x, y], x == cursorX && y == cursorY);
|
||||
}
|
||||
}
|
||||
}
|
||||
currentMap = ArrayHelpers.Clone2D(map);
|
||||
}
|
||||
|
||||
private static void DrawBorder(int posX, int posY, int width, int height)
|
||||
{
|
||||
Console.SetCursorPosition(posX, posY);
|
||||
var linebuilder = new StringBuilder();
|
||||
linebuilder.Append("╔");
|
||||
for (var x = 0; x < width - 2; x++)
|
||||
linebuilder.Append("═");
|
||||
linebuilder.Append("╗");
|
||||
Console.Write(linebuilder);
|
||||
for (var y = 1; y < height - 1; y++)
|
||||
{
|
||||
Console.SetCursorPosition(posX, posY + y);
|
||||
Console.Write("║");
|
||||
Console.SetCursorPosition(posX + width - 1, posY + y);
|
||||
Console.Write("║");
|
||||
}
|
||||
Console.SetCursorPosition(posX, posY + height - 1);
|
||||
linebuilder = new StringBuilder();
|
||||
linebuilder.Append("╚");
|
||||
for (var x = 0; x < width - 2; x++)
|
||||
linebuilder.Append("═");
|
||||
linebuilder.Append("╝");
|
||||
Console.Write(linebuilder);
|
||||
// ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖
|
||||
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
|
||||
// ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢
|
||||
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
|
||||
// └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜
|
||||
Draw(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user