diff --git a/DataAccess/Repositories/BoardRepository.cs b/DataAccess/Repositories/BoardRepository.cs index 64e6cdd..da90e24 100644 --- a/DataAccess/Repositories/BoardRepository.cs +++ b/DataAccess/Repositories/BoardRepository.cs @@ -7,6 +7,7 @@ using NuciDAL.Repositories; using SokoGrump.DataAccess.DataObjects; +using SokoGrump.Models; using SokoGrump.Settings; namespace SokoGrump.DataAccess.Repositories @@ -14,21 +15,14 @@ namespace SokoGrump.DataAccess.Repositories /// /// Board repository implementation. /// - public class BoardRepository : IRepository + /// + /// Initializes a new instance of the class. + /// + /// File name. + public class BoardRepository(string boardsDirectory) : IRepository { - readonly string boardsDirectory; - public int EntitiesCount => GetAll().Count(); - /// - /// Initializes a new instance of the class. - /// - /// File name. - public BoardRepository(string boardsDirectory) - { - this.boardsDirectory = boardsDirectory; - } - /// /// Adds the specified board. /// @@ -50,8 +44,7 @@ public void TryAdd(BoardEntity boardEntity) { } /// Identifier. public BoardEntity Get(string id) { - BoardEntity boardEntity = new BoardEntity(); - string boardFile = Path.Combine(boardsDirectory, id, "board.xml"); + BoardEntity boardEntity = new(); string levelFile = Path.Combine("Levels", $"{id}.lvl"); string[] rows = File.ReadAllLines(levelFile); @@ -66,13 +59,13 @@ public BoardEntity Get(string id) { int tileId = (int)char.GetNumericValue(rows[y][x]); - if (tileId == 4) + if (tileId.Equals(TileId.PlayerOnGround)) { boardEntity.PlayerStartLocationX = x; boardEntity.PlayerStartLocationY = y; boardEntity.Tiles[x, y] = tileEntities[0]; } - else if (tileId == 6) + else if (tileId.Equals(TileId.PlayerOnTarget)) { boardEntity.PlayerStartLocationX = x; boardEntity.PlayerStartLocationY = y; @@ -115,7 +108,7 @@ public BoardEntity TryGet(string id) /// The boards public IEnumerable GetAll() { - List boardEntities = new List(); + List boardEntities = []; foreach (string boardFile in Directory.GetFiles(boardsDirectory)) { @@ -137,13 +130,9 @@ public void Update(BoardEntity boardEntity) { string boardFile = Path.Combine(boardsDirectory, boardEntity.Id, "board.xml"); - using (TextWriter writer = new StreamWriter(boardFile)) - { - XmlSerializer xml = new XmlSerializer(typeof(BoardEntity)); - xml.Serialize(writer, boardEntity); - } - - // TODO: Save the ProvinceMap and TerrainMap as well + using TextWriter writer = new StreamWriter(boardFile); + XmlSerializer xml = new(typeof(BoardEntity)); + xml.Serialize(writer, boardEntity); } public void TryUpdate(BoardEntity boardEntity) @@ -160,9 +149,7 @@ public void TryUpdate(BoardEntity boardEntity) /// /// Identifier. public void Remove(string id) - { - Directory.Delete(Path.Combine(boardsDirectory, id)); - } + => Directory.Delete(Path.Combine(boardsDirectory, id)); public void TryRemove(string id) { @@ -178,9 +165,7 @@ public void TryRemove(string id) /// /// Board. public void Remove(BoardEntity boardEntity) - { - Remove(boardEntity.Id); - } + => Remove(boardEntity.Id); public void TryRemove(BoardEntity boardEntity) { @@ -193,41 +178,41 @@ public void TryRemove(BoardEntity boardEntity) public void ApplyChanges() { } - Dictionary GetTileEntities() + static Dictionary GetTileEntities() { - Dictionary tiles = new Dictionary(); + Dictionary tiles = []; - TileEntity terrainTile = new TileEntity + TileEntity terrainTile = new() { Id = 0, SpriteSheet = "SpriteSheets/brick", TileType = "Walkable" }; - TileEntity wallTile = new TileEntity + TileEntity wallTile = new() { Id = 1, SpriteSheet = "SpriteSheets/wall", TileType = "Solid" }; - TileEntity boxTile = new TileEntity + TileEntity boxTile = new() { Id = 2, SpriteSheet = "SpriteSheets/crate", TileType = "Moveable" }; - TileEntity targetTile = new TileEntity + TileEntity targetTile = new() { Id = 3, SpriteSheet = "Tiles/tile3/0", TileType = "Walkable" }; - TileEntity completedTargetTile = new TileEntity + TileEntity completedTargetTile = new() { Id = 5, SpriteSheet = "Tiles/tile5/0", TileType = "Moveable" }; - TileEntity voidTile = new TileEntity + TileEntity voidTile = new() { Id = 7, SpriteSheet = "Tiles/tile7/0", diff --git a/GameLogic/GameManagers/BoardManager.cs b/GameLogic/GameManagers/BoardManager.cs index a62e7e4..80db04f 100644 --- a/GameLogic/GameManagers/BoardManager.cs +++ b/GameLogic/GameManagers/BoardManager.cs @@ -25,72 +25,56 @@ public void UnloadContent() tiles.Clear(); } - public void Update(double elapsedMiliseconds) - { - - } - - public Board GetBoard(int id) - { - Board loadedBoard = boards[id.ToString()]; - return new Board(loadedBoard); - } + public void Update(double elapsedMiliseconds) { } - public Tile GetTile(int id) - => new Tile(tiles[id]); + public Board GetBoard(int id) => new(boards[id.ToString()]); - public IEnumerable GetTiles() - { - return tiles.Values; - } + public Tile GetTile(int id) => new(tiles[id]); - void LoadBoards() - { - BoardRepository repository = new BoardRepository(ApplicationPaths.LevelsDirectory); + public IEnumerable GetTiles() => tiles.Values; - boards = repository.GetAll().ToDictionary(x => x.Id, x => x.ToDomainModel()); - } + void LoadBoards() => boards = new BoardRepository(ApplicationPaths.LevelsDirectory).GetAll().ToDictionary(x => x.Id, x => x.ToDomainModel()); void LoadTiles() { - Tile terrainTile = new Tile + Tile terrainTile = new() { Id = 0, SpriteSheet = "SpriteSheets/brick", TileType = TileType.Walkable }; - Tile wallTile = new Tile + Tile wallTile = new() { Id = 1, SpriteSheet = "SpriteSheets/wall", TileType = TileType.Solid }; - Tile boxTile = new Tile + Tile boxTile = new() { Id = 2, SpriteSheet = "SpriteSheets/crate", TileType = TileType.Moveable }; - Tile targetTile = new Tile + Tile targetTile = new() { Id = 3, SpriteSheet = "Tiles/tile3/0", TileType = TileType.Walkable }; - Tile completedTargetTile = new Tile + Tile completedTargetTile = new() { Id = 5, SpriteSheet = "Tiles/tile5/0", TileType = TileType.Moveable }; - Tile voidTile = new Tile + Tile voidTile = new() { Id = 7, SpriteSheet = "Tiles/tile7/0", TileType = TileType.Solid }; - tiles = new Dictionary + tiles = new() { { terrainTile.Id, terrainTile }, { wallTile.Id, wallTile }, diff --git a/GameLogic/GameManagers/GameManager.cs b/GameLogic/GameManagers/GameManager.cs index 3f538f2..9ace729 100644 --- a/GameLogic/GameManagers/GameManager.cs +++ b/GameLogic/GameManagers/GameManager.cs @@ -33,26 +33,19 @@ public class GameManager : IGameManager /// /// Initializes a new instance of the class. /// - public GameManager() - { - boardManager = new BoardManager(); - } + public GameManager() => boardManager = new BoardManager(); public void LoadContent() { random = new Random(); - boardManager.LoadContent(); } - public void UnloadContent() - { - boardManager.UnloadContent(); - } + public void UnloadContent() => boardManager.UnloadContent(); public void Update(double elapsedMiliseconds) { - Completed = board.Targets.All(targetLocation => board.Tiles[targetLocation.X, targetLocation.Y].Id == 2); + Completed = board.Targets.All(targetLocation => board.Tiles[targetLocation.X, targetLocation.Y].Id.Equals(TileId.CrateOnGround)); boardManager.Update(elapsedMiliseconds); } @@ -66,21 +59,21 @@ public void NewGame(int level) Level = level; board = boardManager.GetBoard(level); - player = new Player(); - player.Location = new Point2D( - board.PlayerStartLocation.X, - board.PlayerStartLocation.Y); + player = new Player + { + Location = board.PlayerStartLocation + }; for (int y = 0; y < GameDefines.BoardHeight; y++) { for (int x = 0; x < GameDefines.BoardWidth; x++) { - if (board.Tiles[x, y].Id == 3) + if (board.Tiles[x, y].Id.Equals(TileId.EmptyTarget)) { board.Tiles[x, y] = boardManager.GetTile(0); } - if (board.Tiles[x, y].Id == 5) + if (board.Tiles[x, y].Id.Equals(TileId.CrateOnTarget)) { board.Tiles[x, y] = boardManager.GetTile(2); } @@ -93,10 +86,7 @@ public void NewGame(int level) /// /// Retry this instance. /// - public void Retry() - { - NewGame(Level); - } + public void Retry() => NewGame(Level); /// /// Moves the player in a certain direction. @@ -148,19 +138,19 @@ public void MovePlayer(MovementDirection direction) return; } - if (board.Tiles[destX, destY].TileType == TileType.Walkable) + if (board.Tiles[destX, destY].TileType.Equals(TileType.Walkable)) { moved = true; } - else if (board.Tiles[destX, destY].TileType == TileType.Moveable) + else if (board.Tiles[destX, destY].TileType.Equals(TileType.Moveable)) { if ((dirX < 0 && player.Location.X >= 2) || (dirX > 0 && player.Location.X < GameDefines.BoardWidth - 2) || (dirY < 0 && player.Location.Y >= 2) || (dirY > 0 && player.Location.Y < GameDefines.BoardHeight - 2)) { // If it's a crate - if (board.Tiles[destX, destY].Id == 2) + if (board.Tiles[destX, destY].Id.Equals(TileId.CrateOnGround)) { - if (board.Tiles[dest2X, dest2Y].Id == 0) + if (board.Tiles[dest2X, dest2Y].Id.Equals(TileId.Ground)) { int variation = board.Tiles[destX, destY].Variation; board.Tiles[destX, destY] = boardManager.GetTile(0); @@ -191,25 +181,13 @@ public void MovePlayer(MovementDirection direction) } } - public List GetTargets() - { - return board.Targets; - } + public List GetTargets() => board.Targets; - public Player GetPlayer() - { - return player; - } + public Player GetPlayer() => player; - public Tile GetTile(int x, int y) - { - return board.Tiles[x, y]; - } + public Tile GetTile(int x, int y) => board.Tiles[x, y]; - public IEnumerable GetTiles() - { - return boardManager.GetTiles(); - } + public IEnumerable GetTiles() => boardManager.GetTiles(); void GenerateVariations() { @@ -217,7 +195,7 @@ void GenerateVariations() { for (int x = 0; x < GameDefines.BoardWidth; x++) { - if (board.Tiles[x, y].Id == 2) + if (board.Tiles[x, y].Id.Equals(TileId.CrateOnGround)) { board.Tiles[x, y].Variation = random.Next(0, 11); } diff --git a/GameLogic/Mapping/BoardMappingExtensions.cs b/GameLogic/Mapping/BoardMappingExtensions.cs index f6fe62e..6e52019 100644 --- a/GameLogic/Mapping/BoardMappingExtensions.cs +++ b/GameLogic/Mapping/BoardMappingExtensions.cs @@ -20,7 +20,7 @@ static class BoardMappingExtensions /// Board entity. internal static Board ToDomainModel(this BoardEntity boardEntity) { - Board board = new Board + Board board = new() { Id = boardEntity.Id, PlayerStartLocation = new Point2D( @@ -33,11 +33,11 @@ internal static Board ToDomainModel(this BoardEntity boardEntity) { for (int x = 0; x < boardEntity.Tiles.GetLength(0); x++) { - if (boardEntity.Tiles[x, y].Id == 3) + if (boardEntity.Tiles[x, y].Id.Equals(TileId.EmptyTarget)) { board.Targets.Add(new Point2D(x, y)); } - else if (boardEntity.Tiles[x, y].Id == 5) + else if (boardEntity.Tiles[x, y].Id.Equals(TileId.CrateOnTarget)) { board.Targets.Add(new Point2D(x, y)); } @@ -52,18 +52,13 @@ internal static Board ToDomainModel(this BoardEntity boardEntity) /// /// The entity. /// Board. - internal static BoardEntity ToEntity(this Board board) + internal static BoardEntity ToEntity(this Board board) => new() { - BoardEntity boardEntity = new BoardEntity - { - Id = board.Id, - PlayerStartLocationX = board.PlayerStartLocation.X, - PlayerStartLocationY = board.PlayerStartLocation.Y, - Tiles = board.Tiles.ToEntities() - }; - - return boardEntity; - } + Id = board.Id, + PlayerStartLocationX = board.PlayerStartLocation.X, + PlayerStartLocationY = board.PlayerStartLocation.Y, + Tiles = board.Tiles.ToEntities() + }; /// /// Converts the entities into domain models. @@ -71,11 +66,7 @@ internal static BoardEntity ToEntity(this Board board) /// The domain models. /// Board entities. internal static IEnumerable ToDomainModels(this IEnumerable boardEntities) - { - IEnumerable boards = boardEntities.Select(boardEntity => boardEntity.ToDomainModel()); - - return boards; - } + => boardEntities.Select(boardEntity => boardEntity.ToDomainModel()); /// /// Converts the domain models into entities. @@ -83,10 +74,6 @@ internal static IEnumerable ToDomainModels(this IEnumerable /// The entities. /// Boards. internal static IEnumerable ToEntities(this IEnumerable boards) - { - IEnumerable boardEntities = boards.Select(board => board.ToEntity()); - - return boardEntities; - } + => boards.Select(board => board.ToEntity()); } } diff --git a/GameLogic/Mapping/TileMappingExtensions.cs b/GameLogic/Mapping/TileMappingExtensions.cs index 83c5dd0..5b68c5d 100644 --- a/GameLogic/Mapping/TileMappingExtensions.cs +++ b/GameLogic/Mapping/TileMappingExtensions.cs @@ -18,34 +18,24 @@ static class TileMappingExtensions /// /// The domain model. /// World Tile entity. - internal static Tile ToDomainModel(this TileEntity tileEntity) + internal static Tile ToDomainModel(this TileEntity tileEntity) => new() { - Tile tile = new Tile - { - Id = tileEntity.Id, - SpriteSheet = tileEntity.SpriteSheet, - TileType = (TileType)Enum.Parse(typeof(TileType), tileEntity.TileType) - }; - - return tile; - } + Id = tileEntity.Id, + SpriteSheet = tileEntity.SpriteSheet, + TileType = Enum.Parse(tileEntity.TileType) + }; /// /// Converts the domain model into an entity. /// /// The entity. /// World Tile. - internal static TileEntity ToEntity(this Tile tile) + internal static TileEntity ToEntity(this Tile tile) => new() { - TileEntity worldEntity = new TileEntity - { - Id = tile.Id, - SpriteSheet = tile.SpriteSheet, - TileType = tile.TileType.ToString() - }; - - return worldEntity; - } + Id = tile.Id, + SpriteSheet = tile.SpriteSheet, + TileType = tile.TileType.ToString() + }; /// /// Converts the entities into domain models. @@ -53,11 +43,7 @@ internal static TileEntity ToEntity(this Tile tile) /// The domain models. /// World Tile entities. internal static IEnumerable ToDomainModels(this IEnumerable tileEntities) - { - IEnumerable tiles = tileEntities.Select(tileEntity => tileEntity.ToDomainModel()); - - return tiles; - } + => tileEntities.Select(tileEntity => tileEntity.ToDomainModel()); /// /// Converts the domain models into entities. @@ -65,11 +51,7 @@ internal static IEnumerable ToDomainModels(this IEnumerable ti /// The entities. /// World Tiles. internal static IEnumerable ToEntities(this IEnumerable tiles) - { - IEnumerable tileEntities = tiles.Select(tile => tile.ToEntity()); - - return tileEntities; - } + => tiles.Select(tile => tile.ToEntity()); /// /// Converts the entities into domain models. diff --git a/GameWindow.cs b/GameWindow.cs index 3f5bba1..30a2268 100644 --- a/GameWindow.cs +++ b/GameWindow.cs @@ -39,17 +39,6 @@ public GameWindow() cursor = new Cursor(); } - /// - /// Allows the game to perform any initialization it needs to before starting to run. - /// This is where it can query for any required services and load any non-graphic - /// related content. Calling base.Initialize will enumerate through any components - /// and initialize them as well. - /// - protected override void Initialize() - { - base.Initialize(); - } - /// /// LoadContent will be called once per game and is the place to load /// all of your content. @@ -91,7 +80,7 @@ protected override void UnloadContent() ScreenManager.Instance.UnloadContent(); - fpsIndicator.UnloadContent(); + FpsIndicator.UnloadContent(); cursor.UnloadContent(); // TODO: Logging diff --git a/Gui/Controls/GuiButton.cs b/Gui/Controls/GuiButton.cs index 2bf3f16..ea59970 100644 --- a/Gui/Controls/GuiButton.cs +++ b/Gui/Controls/GuiButton.cs @@ -21,10 +21,7 @@ public class GuiButton : GuiControl, IGuiControl GuiImage image; GuiTooltip tooltip; - public GuiButton() - { - FontName = "ButtonFont"; - } + public GuiButton() => FontName = "ButtonFont"; protected override void DoLoadContent() { @@ -38,26 +35,17 @@ protected override void DoLoadContent() FontName = "ToolTipFont", Size = new Size2D((int)(Size.Width * 2.5), (int)(Size.Height * 0.8)) }; - + RegisterChildren(image, tooltip); RegisterEvents(); SetChildrenProperties(); } - protected override void DoUnloadContent() - { - UnregisterEvents(); - } + protected override void DoUnloadContent() => UnregisterEvents(); - protected override void DoUpdate(GameTime gameTime) - { - SetChildrenProperties(); - } - - protected override void DoDraw(SpriteBatch spriteBatch) - { + protected override void DoUpdate(GameTime gameTime) => SetChildrenProperties(); - } + protected override void DoDraw(SpriteBatch spriteBatch) { } void RegisterEvents() { @@ -86,9 +74,6 @@ void OnMouseEntered(object sender, MouseEventArgs e) } } - void OnMouseLeft(object sender, MouseEventArgs e) - { - tooltip.Hide(); - } + void OnMouseLeft(object sender, MouseEventArgs e) => tooltip.Hide(); } } diff --git a/Gui/Controls/GuiGameBoard.cs b/Gui/Controls/GuiGameBoard.cs index bc6d32c..d523f24 100644 --- a/Gui/Controls/GuiGameBoard.cs +++ b/Gui/Controls/GuiGameBoard.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; @@ -19,25 +18,18 @@ namespace SokoGrump.Gui.Controls /// /// World map GUI element. /// - public class GuiGameBoard : GuiControl + public class GuiGameBoard(IGameManager game) : GuiControl { - IGameManager game; - Dictionary tileSprites; TextureSprite targetSprite; TextureSprite playerSprite; - public GuiGameBoard(IGameManager game) - { - this.game = game; - } - /// /// Loads the content. /// protected override void DoLoadContent() { - tileSprites = new Dictionary(); + tileSprites = []; targetSprite = new TextureSprite { ContentFile = "SpriteSheets/target" @@ -52,14 +44,14 @@ protected override void DoLoadContent() foreach (Tile tile in game.GetTiles()) { - TextureSprite tileSprite = new TextureSprite + TextureSprite tileSprite = new() { ContentFile = tile.SpriteSheet, SourceRectangle = new Rectangle2D(0, 0, GameDefines.MapTileSize, GameDefines.MapTileSize), IsActive = true }; - if (tile.Id == 2) + if (tile.Id.Equals(TileId.CrateOnGround)) { tileSprite.SpriteSheetEffect = new CrateSpriteSheetEffect(game); } @@ -125,24 +117,24 @@ protected override void DoDraw(SpriteBatch spriteBatch) y * GameDefines.MapTileSize); // TODO: This is temporary - if (tile.Id == 0 || tile.Id == 1) + if (tile.Id.Equals(TileId.Ground) || tile.Id.Equals(TileId.Wall)) { TileSpriteSheetEffect tileEffect = (TileSpriteSheetEffect)tileSprite.SpriteSheetEffect; tileEffect.TileLocation = new Point2D(x, y); - if (tile.Id == 0) + if (tile.Id.Equals(TileId.Ground)) { - tileEffect.TilesWith = new List { 0, 2, 3, 5 }; + tileEffect.TilesWith = [0, 2, 3, 5]; } - else if (tile.Id == 1) + else if (tile.Id.Equals(TileId.Wall)) { - tileEffect.TilesWith = new List { 1 }; + tileEffect.TilesWith = [1]; } tileEffect.Update(null); } - else if (tile.Id == 2) + else if (tile.Id.Equals(TileId.CrateOnGround)) { CrateSpriteSheetEffect crateEffect = (CrateSpriteSheetEffect)tileSprite.SpriteSheetEffect; @@ -150,7 +142,7 @@ protected override void DoDraw(SpriteBatch spriteBatch) crateEffect.Update(null); } - if (tile.Id == 2 && targets.Any(target => target.X == x && target.Y == y)) + if (tile.Id.Equals(TileId.CrateOnGround) && targets.Any(target => target.X.Equals(x) && target.Y.Equals(y))) { tileSprite.Tint = Colour.Red; } @@ -167,14 +159,12 @@ protected override void DoDraw(SpriteBatch spriteBatch) { Tile tile = game.GetTile(targetLocation.X, targetLocation.Y); - if (tile.Id == 2) + if (tile.Id.Equals(TileId.CrateOnGround)) { continue; } - targetSprite.Location = new Point2D( - Location.X + targetLocation.X * GameDefines.MapTileSize, - Location.Y + targetLocation.Y * GameDefines.MapTileSize); + targetSprite.Location = Location + targetLocation * GameDefines.MapTileSize; targetSprite.Draw(spriteBatch); } diff --git a/Gui/Controls/GuiInfoBar.cs b/Gui/Controls/GuiInfoBar.cs index 1292634..1f13699 100644 --- a/Gui/Controls/GuiInfoBar.cs +++ b/Gui/Controls/GuiInfoBar.cs @@ -49,28 +49,19 @@ protected override void DoLoadContent() /// /// Unloads the content. /// - protected override void DoUnloadContent() - { + protected override void DoUnloadContent() { } - } - /// /// Updates the content. /// /// The game time. - protected override void DoUpdate(GameTime gameTime) - { - this.SetChildrenProperties(); - } - + protected override void DoUpdate(GameTime gameTime) => SetChildrenProperties(); + /// /// Draw the content on the specified spriteBatch. /// /// Sprite batch. - protected override void DoDraw(SpriteBatch spriteBatch) - { - - } + protected override void DoDraw(SpriteBatch spriteBatch) { } void SetChildrenProperties() { @@ -79,7 +70,7 @@ void SetChildrenProperties() levelText.BackgroundColour = BackgroundColour; levelText.ForegroundColour = ForegroundColour; - + movesText.Text = $"Moves: {game.GetPlayer().MovesCount}"; levelText.Text = $"Level {game.Level}"; diff --git a/Gui/FpsIndicator.cs b/Gui/FpsIndicator.cs index 4c17458..85f5ba2 100644 --- a/Gui/FpsIndicator.cs +++ b/Gui/FpsIndicator.cs @@ -17,7 +17,6 @@ public class FpsIndicator { GameTime gameTime; SpriteFont fpsFont; - Vector2 fpsCounterSize; string fpsString; /// @@ -29,26 +28,17 @@ public class FpsIndicator /// /// Initializes a new instance of the class. /// - public FpsIndicator() - { - Location = Vector2.Zero; - } + public FpsIndicator() => Location = Vector2.Zero; /// /// Loads the content. /// - public void LoadContent() - { - fpsFont = NuciContentManager.Instance.LoadSpriteFont("Fonts/FrameCounterFont"); - } + public void LoadContent() => fpsFont = NuciContentManager.Instance.LoadSpriteFont("Fonts/FrameCounterFont"); /// /// Unloads the content. /// - public void UnloadContent() - { - - } + public static void UnloadContent() { } /// /// Updates the content. @@ -59,7 +49,6 @@ public void Update(GameTime gameTime) this.gameTime = gameTime; fpsString = $"FPS: {Math.Round(FramerateCounter.Instance.AverageFramesPerSecond)}"; - fpsCounterSize = fpsFont.MeasureString(fpsString); } /// diff --git a/Gui/Helpers/FramerateCounter.cs b/Gui/Helpers/FramerateCounter.cs index dbf9674..65a64e4 100644 --- a/Gui/Helpers/FramerateCounter.cs +++ b/Gui/Helpers/FramerateCounter.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; namespace SokoGrump.Gui.Helpers { @@ -9,7 +10,7 @@ namespace SokoGrump.Gui.Helpers public class FramerateCounter { static volatile FramerateCounter instance; - static object syncRoot = new object(); + static readonly Lock syncRoot = new(); readonly Queue sampleBuffer; @@ -21,14 +22,11 @@ public static FramerateCounter Instance { get { - if (instance == null) + if (instance is null) { lock (syncRoot) { - if (instance == null) - { - instance = new FramerateCounter(); - } + instance ??= new FramerateCounter(); } } @@ -68,10 +66,7 @@ public static FramerateCounter Instance /// /// Initializes a new instance of the class. /// - public FramerateCounter() - { - sampleBuffer = new Queue(); - } + public FramerateCounter() => sampleBuffer = new Queue(); /// /// Updates the framerate. diff --git a/Gui/Screens/GameFinishedScreen.cs b/Gui/Screens/GameFinishedScreen.cs index f4d296b..821b112 100644 --- a/Gui/Screens/GameFinishedScreen.cs +++ b/Gui/Screens/GameFinishedScreen.cs @@ -55,10 +55,7 @@ protected override void DoLoadContent() /// /// Unloads the content. /// - protected override void DoUnloadContent() - { - UnregisterEvents(); - } + protected override void DoUnloadContent() => UnregisterEvents(); /// /// Updates the content. @@ -80,13 +77,10 @@ protected override void DoUpdate(GameTime gameTime) /// Draw the content on the specified spriteBatch. /// /// Sprite batch. - protected override void DoDraw(SpriteBatch spriteBatch) - { - - } + protected override void DoDraw(SpriteBatch spriteBatch) { } /// - /// Registers the + /// Registers the void RegisterEvents() { InputManager.Instance.KeyboardKeyPressed += OnInputManagerKeyboardKeyPressed; @@ -105,24 +99,12 @@ void UnregisterEvents() /// /// Sets the properties of the child controls. /// - void SetChildrenProperties() - { - Image.Size = ScreenManager.Instance.Size; - } + void SetChildrenProperties() => Image.Size = ScreenManager.Instance.Size; - void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) => ChangeScreen(); - void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) => ChangeScreen(); - void ChangeScreen() - { - ScreenManager.Instance.ChangeScreens(); - } + static void ChangeScreen() => ScreenManager.Instance.ChangeScreens(); } } diff --git a/Gui/Screens/GameplayScreen.cs b/Gui/Screens/GameplayScreen.cs index e8d408f..056d5ff 100644 --- a/Gui/Screens/GameplayScreen.cs +++ b/Gui/Screens/GameplayScreen.cs @@ -5,7 +5,6 @@ using Microsoft.Xna.Framework.Input; using NuciXNA.Gui; -using NuciXNA.Gui.Controls; using NuciXNA.Gui.Screens; using NuciXNA.Input; using NuciXNA.Primitives; @@ -28,7 +27,7 @@ public class GameplayScreen : Screen GuiInfoBar infoBar; GuiGameBoard gameBoard; - int level; + readonly int level; public GameplayScreen(int level) { @@ -95,7 +94,7 @@ protected override void DoUpdate(GameTime gameTime) } else { - ScreenManager.Instance.ChangeScreens(typeof(GameFinishedScreen)); + ScreenManager.Instance.ChangeScreens(); SettingsManager.Instance.UserData.LastLevel = 0; } } @@ -107,10 +106,7 @@ protected override void DoUpdate(GameTime gameTime) /// Draw the content on the specified spriteBatch. /// /// Sprite batch. - protected override void DoDraw(SpriteBatch spriteBatch) - { - - } + protected override void DoDraw(SpriteBatch spriteBatch) { } /// /// Registers the events. @@ -180,9 +176,6 @@ void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) } } - void OnRetryButtonPressed(object sender, MouseButtonEventArgs e) - { - game.Retry(); - } + void OnRetryButtonPressed(object sender, MouseButtonEventArgs e) => game.Retry(); } } diff --git a/Gui/Screens/SettingsScreen.cs b/Gui/Screens/SettingsScreen.cs index 9ad914e..b038c27 100644 --- a/Gui/Screens/SettingsScreen.cs +++ b/Gui/Screens/SettingsScreen.cs @@ -32,12 +32,12 @@ protected override void DoLoadContent() Text = "Back", TargetScreen = typeof(TitleScreen) }; - + Items.Add(fullScreenToggle); Items.Add(backLink); RegisterEvents(); - + fullScreenToggle.SetState(SettingsManager.Instance.GraphicsSettings.Fullscreen); base.DoLoadContent(); @@ -58,22 +58,14 @@ protected override void DoUnloadContent() /// /// Registers the events. /// - void RegisterEvents() - { - fullScreenToggle.StateChanged += OnFullscreenToggleStateChanged; - } + void RegisterEvents() => fullScreenToggle.StateChanged += OnFullscreenToggleStateChanged; /// /// Unregisters the events. /// - void UnregisterEvents() - { - fullScreenToggle.StateChanged -= OnFullscreenToggleStateChanged; - } + void UnregisterEvents() => fullScreenToggle.StateChanged -= OnFullscreenToggleStateChanged; void OnFullscreenToggleStateChanged(object sender, EventArgs e) - { - SettingsManager.Instance.GraphicsSettings.Fullscreen = fullScreenToggle.IsOn; - } + => SettingsManager.Instance.GraphicsSettings.Fullscreen = fullScreenToggle.IsOn; } } diff --git a/Gui/Screens/SplashScreen.cs b/Gui/Screens/SplashScreen.cs index f480a27..b7dc33d 100644 --- a/Gui/Screens/SplashScreen.cs +++ b/Gui/Screens/SplashScreen.cs @@ -50,10 +50,7 @@ protected override void DoLoadContent() /// /// Unloads the content. /// - protected override void DoUnloadContent() - { - UnregisterEvents(); - } + protected override void DoUnloadContent() => UnregisterEvents(); /// /// Updates the content. @@ -75,10 +72,7 @@ protected override void DoUpdate(GameTime gameTime) /// Draw the content on the specified spriteBatch. /// /// Sprite batch. - protected override void DoDraw(SpriteBatch spriteBatch) - { - - } + protected override void DoDraw(SpriteBatch spriteBatch) { } /// /// Registers the events. @@ -102,24 +96,14 @@ void UnregisterEvents() /// Sets the properties of the child controls. /// void SetChildrenProperties() - { - LogoImage.Location = new Point2D((ScreenManager.Instance.Size.Width - LogoImage.Size.Width) / 2, - (ScreenManager.Instance.Size.Height - LogoImage.Size.Height) / 2); - } + => LogoImage.Location = new Point2D( + (ScreenManager.Instance.Size.Width - LogoImage.Size.Width) / 2, + (ScreenManager.Instance.Size.Height - LogoImage.Size.Height) / 2); - void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) => ChangeScreen(); - void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) => ChangeScreen(); - void ChangeScreen() - { - ScreenManager.Instance.ChangeScreens(); - } + static void ChangeScreen() => ScreenManager.Instance.ChangeScreens(); } } diff --git a/Gui/Screens/TitleScreen.cs b/Gui/Screens/TitleScreen.cs index d298723..a5e7b60 100644 --- a/Gui/Screens/TitleScreen.cs +++ b/Gui/Screens/TitleScreen.cs @@ -24,7 +24,7 @@ protected override void DoLoadContent() Id = nameof(newGameLink), Text = "New Game", TargetScreen = typeof(GameplayScreen), - Parameters = new object[] { default(int) } + Parameters = [default(int)] }; settingsLink = new GuiMenuLink { @@ -32,7 +32,7 @@ protected override void DoLoadContent() Text = "Settings", TargetScreen = typeof(SettingsScreen) }; - + if (SettingsManager.Instance.UserData.LastLevel > 0) { continueGameLink = new GuiMenuLink @@ -40,7 +40,7 @@ protected override void DoLoadContent() Id = nameof(continueGameLink), Text = "Continue Game", TargetScreen = typeof(GameplayScreen), - Parameters = new object[] { SettingsManager.Instance.UserData.LastLevel } + Parameters = [SettingsManager.Instance.UserData.LastLevel] }; Items.Add(continueGameLink); diff --git a/Gui/Screens/VictoryScreen.cs b/Gui/Screens/VictoryScreen.cs index 50cd042..aed3943 100644 --- a/Gui/Screens/VictoryScreen.cs +++ b/Gui/Screens/VictoryScreen.cs @@ -27,7 +27,7 @@ public class VictoryScreen : Screen /// The logo. public GuiImage Image { get; set; } - int level; + readonly int level; /// /// Initializes a new instance of the class. @@ -59,10 +59,7 @@ protected override void DoLoadContent() /// /// Unloads the content. /// - protected override void DoUnloadContent() - { - UnregisterEvents(); - } + protected override void DoUnloadContent() => UnregisterEvents(); /// /// Updates the content. @@ -84,13 +81,10 @@ protected override void DoUpdate(GameTime gameTime) /// Draw the content on the specified spriteBatch. /// /// Sprite batch. - protected override void DoDraw(SpriteBatch spriteBatch) - { - - } + protected override void DoDraw(SpriteBatch spriteBatch) { } /// - /// Registers the + /// Registers the void RegisterEvents() { InputManager.Instance.KeyboardKeyPressed += OnInputManagerKeyboardKeyPressed; @@ -109,24 +103,12 @@ void UnregisterEvents() /// /// Sets the properties of the child controls. /// - void SetChildrenProperties() - { - Image.Size = ScreenManager.Instance.Size; - } + void SetChildrenProperties() => Image.Size = ScreenManager.Instance.Size; - void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e) => ChangeScreen(); - void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) - { - ChangeScreen(); - } + void OnInputManagerMouseButtonPressed(object sender, MouseButtonEventArgs e) => ChangeScreen(); - void ChangeScreen() - { - ScreenManager.Instance.ChangeScreens(level); - } + void ChangeScreen() => ScreenManager.Instance.ChangeScreens(level); } } diff --git a/Gui/SpriteEffects/CrateSpriteSheetEffect.cs b/Gui/SpriteEffects/CrateSpriteSheetEffect.cs index d1c18c8..2e4c415 100644 --- a/Gui/SpriteEffects/CrateSpriteSheetEffect.cs +++ b/Gui/SpriteEffects/CrateSpriteSheetEffect.cs @@ -3,7 +3,6 @@ using NuciXNA.Primitives; using SokoGrump.GameLogic.GameManagers; -using SokoGrump.Models; namespace SokoGrump.Gui.SpriteEffects { @@ -25,10 +24,6 @@ public CrateSpriteSheetEffect(IGameManager game) /// /// Game time. protected override void DoUpdate(GameTime gameTime) - { - Tile tile = game.GetTile(TileLocation.X, TileLocation.Y); - - CurrentFrame = new Point2D(tile.Variation, 0); - } + => CurrentFrame = new Point2D(game.GetTile(TileLocation.X, TileLocation.Y).Variation, 0); } } diff --git a/Gui/SpriteEffects/PlayerSpriteSheetEffect.cs b/Gui/SpriteEffects/PlayerSpriteSheetEffect.cs index 4609131..abfd725 100644 --- a/Gui/SpriteEffects/PlayerSpriteSheetEffect.cs +++ b/Gui/SpriteEffects/PlayerSpriteSheetEffect.cs @@ -11,8 +11,7 @@ public class PlayerSpriteSheetEffect : SpriteSheetEffect { readonly IGameManager game; - public PlayerSpriteSheetEffect(IGameManager game) - : base() + public PlayerSpriteSheetEffect(IGameManager game) : base() { FrameAmount = new Size2D(3, 6); @@ -25,9 +24,7 @@ public PlayerSpriteSheetEffect(IGameManager game) /// Game time. protected override void DoUpdate(GameTime gameTime) { - Player player = game.GetPlayer(); - - if (player.Direction == MovementDirection.West) + if (game.GetPlayer().Direction.Equals(MovementDirection.West)) { CurrentFrame = new Point2D(0, 0); } diff --git a/Gui/SpriteEffects/TileSpritesheetEffect.cs b/Gui/SpriteEffects/TileSpritesheetEffect.cs index ea1045c..59a928f 100644 --- a/Gui/SpriteEffects/TileSpritesheetEffect.cs +++ b/Gui/SpriteEffects/TileSpritesheetEffect.cs @@ -17,11 +17,10 @@ public class TileSpriteSheetEffect : SpriteSheetEffect public List TilesWith { get; set; } - public TileSpriteSheetEffect(IGameManager game) - : base() + public TileSpriteSheetEffect(IGameManager game) : base() { FrameAmount = new Size2D(3, 6); - TilesWith = new List(); + TilesWith = []; this.game = game; } @@ -33,13 +32,12 @@ public TileSpriteSheetEffect(IGameManager game) protected override void DoUpdate(GameTime gameTime) { // TODO: Dirty fix - if (TileLocation.X == 0 || TileLocation.X == GameDefines.BoardWidth - 1 || - TileLocation.Y == 0 || TileLocation.Y == GameDefines.BoardHeight - 1) + if (TileLocation.X.Equals(0) || TileLocation.X.Equals(GameDefines.BoardWidth - 1) || + TileLocation.Y.Equals(0) || TileLocation.Y.Equals(GameDefines.BoardHeight - 1)) { return; } - int id = game.GetTile(TileLocation.X, TileLocation.Y).Id; int idN = game.GetTile(TileLocation.X, TileLocation.Y - 1).Id; int idW = game.GetTile(TileLocation.X - 1, TileLocation.Y).Id; int idS = game.GetTile(TileLocation.X, TileLocation.Y + 1).Id; diff --git a/Models/Board.cs b/Models/Board.cs index 988c679..15d092f 100644 --- a/Models/Board.cs +++ b/Models/Board.cs @@ -15,7 +15,7 @@ public class Board : ModelBase public Point2D PlayerStartLocation { get; set; } public List Targets { get; set; } - + /// /// Gets or sets the tiles. /// @@ -24,13 +24,13 @@ public class Board : ModelBase public Board() { - Targets = new List(); + Targets = []; } public Board(Board board) { PlayerStartLocation = board.PlayerStartLocation; - Targets = new List(board.Targets); + Targets = [.. board.Targets]; Tiles = new Tile[GameDefines.BoardWidth, GameDefines.BoardHeight]; for (int y = 0; y < GameDefines.BoardHeight; y++) diff --git a/Models/ModelBase.cs b/Models/ModelBase.cs index 6af20e0..30a6034 100644 --- a/Models/ModelBase.cs +++ b/Models/ModelBase.cs @@ -35,7 +35,7 @@ public abstract class ModelBase : IEquatable /// ; otherwise, false. public virtual bool Equals(ModelBase other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -58,7 +58,7 @@ public virtual bool Equals(ModelBase other) /// ; otherwise, false. public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) + if (obj is null) { return false; } @@ -68,7 +68,7 @@ public override bool Equals(object obj) return true; } - if (obj.GetType() != GetType()) + if (!obj.GetType().Equals(GetType())) { return false; } @@ -85,9 +85,9 @@ public override int GetHashCode() { unchecked { - return ((Id != null ? Id.GetHashCode() : 0) * 397) ^ - (Name != null ? Name.GetHashCode() : 0) ^ - (Description != null ? Description.GetHashCode() : 0); + return ((Id is not null ? Id.GetHashCode() : 0) * 397) ^ + (Name is not null ? Name.GetHashCode() : 0) ^ + (Description is not null ? Description.GetHashCode() : 0); } } } diff --git a/Models/Tile.cs b/Models/Tile.cs index 86ec141..704baa5 100644 --- a/Models/Tile.cs +++ b/Models/Tile.cs @@ -26,9 +26,7 @@ public class Tile /// The variation. public int Variation { get; set; } - public Tile() - { - } + public Tile() { } public Tile(Tile tile) { diff --git a/Models/TileId.cs b/Models/TileId.cs new file mode 100644 index 0000000..02e7b8d --- /dev/null +++ b/Models/TileId.cs @@ -0,0 +1,14 @@ +namespace SokoGrump.Models +{ + public enum TileId + { + Ground = 0, + Wall = 1, + CrateOnGround = 2, + EmptyTarget = 3, + PlayerOnGround = 4, + CrateOnTarget = 5, + PlayerOnTarget = 6, + Void = 7 + } +} diff --git a/Settings/AudioSettings.cs b/Settings/AudioSettings.cs index 1ba89a2..ce972fe 100644 --- a/Settings/AudioSettings.cs +++ b/Settings/AudioSettings.cs @@ -8,9 +8,6 @@ public class AudioSettings /// The sound toggle. public bool SoundEnabled { get; set; } - public AudioSettings() - { - SoundEnabled = true; - } + public AudioSettings() => SoundEnabled = true; } } diff --git a/Settings/SettingsManager.cs b/Settings/SettingsManager.cs index cd8cee6..4001bc6 100644 --- a/Settings/SettingsManager.cs +++ b/Settings/SettingsManager.cs @@ -1,5 +1,5 @@ using System.IO; - +using System.Threading; using NuciXNA.DataAccess.IO; using NuciXNA.Graphics; @@ -11,7 +11,7 @@ namespace SokoGrump.Settings public class SettingsManager { static volatile SettingsManager instance; - static object syncRoot = new object(); + static readonly Lock syncRoot = new(); /// /// Gets the instance. @@ -21,14 +21,11 @@ public static SettingsManager Instance { get { - if (instance == null) + if (instance is null) { lock (syncRoot) { - if (instance == null) - { - instance = new SettingsManager(); - } + instance ??= new SettingsManager(); } } @@ -78,7 +75,7 @@ public void LoadContent() return; } - XmlFileObject xmlManager = new XmlFileObject(); + XmlFileObject xmlManager = new(); SettingsManager storedSettings = xmlManager.Read(ApplicationPaths.SettingsFile); instance = storedSettings; @@ -89,7 +86,7 @@ public void LoadContent() /// public void SaveContent() { - XmlFileObject xmlManager = new XmlFileObject(); + XmlFileObject xmlManager = new(); xmlManager.Write(ApplicationPaths.SettingsFile, this); }