diff --git a/C#/forSpbu/Game/EventLoop.cs b/C#/forSpbu/Game/EventLoop.cs new file mode 100644 index 0000000..af8db25 --- /dev/null +++ b/C#/forSpbu/Game/EventLoop.cs @@ -0,0 +1,44 @@ +namespace Game; + +public class EventLoop +{ + public event EventHandler LeftHandler = (_, _) => { }; + public event EventHandler RightHandler = (_, _) => { }; + public event EventHandler UpHandler = (_, _) => { }; + public event EventHandler DownHandler = (_, _) => { }; + public event EventHandler EndHandler = (_, _) => { }; + public event EventHandler StartHandler = (_, _) => { }; + + public void Run() + { + var endCondition = false; + StartHandler(this, EventArgs.Empty); + while (true) + { + if (endCondition) + { + break; + } + + switch (Console.ReadKey(true).Key) + { + case ConsoleKey.LeftArrow: + LeftHandler(this, EventArgs.Empty); + break; + case ConsoleKey.RightArrow: + RightHandler(this, EventArgs.Empty); + break; + case ConsoleKey.UpArrow: + UpHandler(this, EventArgs.Empty); + break; + case ConsoleKey.DownArrow: + DownHandler(this, EventArgs.Empty); + break; + case ConsoleKey.Enter: + EndHandler(this, EventArgs.Empty); + endCondition = true; + break; + } + } + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Game.cs b/C#/forSpbu/Game/Game.cs new file mode 100644 index 0000000..0df4441 --- /dev/null +++ b/C#/forSpbu/Game/Game.cs @@ -0,0 +1,74 @@ +using System.Security.Principal; + +namespace Game; + +public class Game +{ + private Map _map; + private int _xPosition; + private int _yPosition; + private Action _movePlayer; + + public enum Side + { + Right, + Left, + Up, + Down + } + + public Game(string path, Action movePlayer) + { + _map = new Map(path); + _xPosition = _map.PlayerMapWidth / 2; + _yPosition = _map.PlayerMapHeight / 2; + _movePlayer = movePlayer; + } + + public void OnStart(object? sender, EventArgs args) + { + _map.DrawMap(); + Console.SetCursorPosition(0, 0); + } + + public void OnLeft(object? sender, EventArgs args) + { + if (_xPosition > 0 && _map.PlayerMap[_yPosition][_xPosition - 1]) + { + _movePlayer(_xPosition, _yPosition, _xPosition - 1, _yPosition); + _xPosition--; + } + } + + public void OnRight(object? sender, EventArgs args) + { + if (_xPosition + 1 < _map.PlayerMapWidth && _map.PlayerMap[_yPosition][_xPosition + 1]) + { + _movePlayer(_xPosition, _yPosition, _xPosition + 1, _yPosition); + _xPosition++; + } + } + + public void OnDown(object? sender, EventArgs args) + { + if (_yPosition + 1 < _map.PlayerMapHeight && _map.PlayerMap[_yPosition + 1][_xPosition]) + { + _movePlayer(_xPosition, _yPosition, _xPosition, _yPosition + 1); + _yPosition++; + } + } + + public void OnUp(object? sender, EventArgs args) + { + if (_yPosition > 0 && _map.PlayerMap[_yPosition - 1][_xPosition]) + { + _movePlayer(_xPosition, _yPosition, _xPosition, _yPosition - 1); + _yPosition--; + } + } + + public static void OnEnd(object? sender, EventArgs args) + { + Console.Clear(); + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Game.csproj b/C#/forSpbu/Game/Game.csproj new file mode 100644 index 0000000..2b14c81 --- /dev/null +++ b/C#/forSpbu/Game/Game.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/C#/forSpbu/Game/IncorrectMapException.cs b/C#/forSpbu/Game/IncorrectMapException.cs new file mode 100644 index 0000000..703233a --- /dev/null +++ b/C#/forSpbu/Game/IncorrectMapException.cs @@ -0,0 +1,8 @@ +namespace Game; + +public class IncorrectMapException : Exception +{ + public IncorrectMapException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Program.cs b/C#/forSpbu/Game/Program.cs new file mode 100644 index 0000000..78eb9c9 --- /dev/null +++ b/C#/forSpbu/Game/Program.cs @@ -0,0 +1,23 @@ +using Game; + +var eventLoop = new EventLoop(); +var game = new Game.Game("../../../map.txt", movePlayer); + +void movePlayer(int oldXPosition, int oldYPosition, int newXPosition, int newYPosition) { + Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1); + Console.Write(' '); + Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1); + Console.SetCursorPosition(newXPosition + 1, newYPosition + 1); + Console.Write('@'); + Console.SetCursorPosition(newXPosition + 1, newYPosition + 1); +} + +eventLoop.StartHandler += game.OnStart; +eventLoop.EndHandler += Game.Game.OnEnd; + +eventLoop.LeftHandler += game.OnLeft; +eventLoop.RightHandler += game.OnRight; +eventLoop.DownHandler += game.OnDown; +eventLoop.UpHandler += game.OnUp; + +eventLoop.Run(); \ No newline at end of file diff --git a/C#/forSpbu/Game/map.txt b/C#/forSpbu/Game/map.txt new file mode 100644 index 0000000..c934f43 --- /dev/null +++ b/C#/forSpbu/Game/map.txt @@ -0,0 +1,11 @@ + ____________________ +| | +| | | +| | +| | +| | +| | +| | +| | +| | +|____________________| \ No newline at end of file diff --git a/C#/forSpbu/forSpbu.sln b/C#/forSpbu/forSpbu.sln index 527f400..684fea5 100644 --- a/C#/forSpbu/forSpbu.sln +++ b/C#/forSpbu/forSpbu.sln @@ -10,6 +10,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03.03", "03.03", "{882A9B9C EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10.03", "10.03", "{EA6FC7D9-BDFB-49CD-AC00-FC5DDC5274B0}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "31.03", "31.03", "{DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Game", "Game\Game.csproj", "{1239ED0E-09B4-44EB-96F6-4945F308E2AD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,12 +25,17 @@ Global {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.Build.0 = Release|Any CPU {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution - {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {1239ED0E-09B4-44EB-96F6-4945F308E2AD} = {DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6} EndGlobalSection EndGlobal