Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions DataAccess/Repositories/BoardRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@
using NuciDAL.Repositories;

using SokoGrump.DataAccess.DataObjects;
using SokoGrump.Models;
using SokoGrump.Settings;

namespace SokoGrump.DataAccess.Repositories
{
/// <summary>
/// Board repository implementation.
/// </summary>
public class BoardRepository : IRepository<string, BoardEntity>
/// <remarks>
/// Initializes a new instance of the <see cref="BoardRepository"/> class.
/// </remarks>
/// <param name="boardsDirectory">File name.</param>
public class BoardRepository(string boardsDirectory) : IRepository<string, BoardEntity>
{
readonly string boardsDirectory;

public int EntitiesCount => GetAll().Count();

/// <summary>
/// Initializes a new instance of the <see cref="BoardRepository"/> class.
/// </summary>
/// <param name="boardsDirectory">File name.</param>
public BoardRepository(string boardsDirectory)
{
this.boardsDirectory = boardsDirectory;
}

/// <summary>
/// Adds the specified board.
/// </summary>
Expand All @@ -50,8 +44,7 @@ public void TryAdd(BoardEntity boardEntity) { }
/// <param name="id">Identifier.</param>
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);

Expand All @@ -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;
Expand Down Expand Up @@ -115,7 +108,7 @@ public BoardEntity TryGet(string id)
/// <returns>The boards</returns>
public IEnumerable<BoardEntity> GetAll()
{
List<BoardEntity> boardEntities = new List<BoardEntity>();
List<BoardEntity> boardEntities = [];

foreach (string boardFile in Directory.GetFiles(boardsDirectory))
{
Expand All @@ -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)
Expand All @@ -160,9 +149,7 @@ public void TryUpdate(BoardEntity boardEntity)
/// </summary>
/// <param name="id">Identifier.</param>
public void Remove(string id)
{
Directory.Delete(Path.Combine(boardsDirectory, id));
}
=> Directory.Delete(Path.Combine(boardsDirectory, id));

public void TryRemove(string id)
{
Expand All @@ -178,9 +165,7 @@ public void TryRemove(string id)
/// </summary>
/// <param name="boardEntity">Board.</param>
public void Remove(BoardEntity boardEntity)
{
Remove(boardEntity.Id);
}
=> Remove(boardEntity.Id);

public void TryRemove(BoardEntity boardEntity)
{
Expand All @@ -193,41 +178,41 @@ public void TryRemove(BoardEntity boardEntity)

public void ApplyChanges() { }

Dictionary<int, TileEntity> GetTileEntities()
static Dictionary<int, TileEntity> GetTileEntities()
{
Dictionary<int, TileEntity> tiles = new Dictionary<int, TileEntity>();
Dictionary<int, TileEntity> 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",
Expand Down
40 changes: 12 additions & 28 deletions GameLogic/GameManagers/BoardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tile> GetTiles()
{
return tiles.Values;
}
public Tile GetTile(int id) => new(tiles[id]);

void LoadBoards()
{
BoardRepository repository = new BoardRepository(ApplicationPaths.LevelsDirectory);
public IEnumerable<Tile> 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<int, Tile>
tiles = new()
{
{ terrainTile.Id, terrainTile },
{ wallTile.Id, wallTile },
Expand Down
60 changes: 19 additions & 41 deletions GameLogic/GameManagers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,19 @@ public class GameManager : IGameManager
/// <summary>
/// Initializes a new instance of the <see cref="GameEngine"/> class.
/// </summary>
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);
}
Expand All @@ -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);
}
Expand All @@ -93,10 +86,7 @@ public void NewGame(int level)
/// <summary>
/// Retry this instance.
/// </summary>
public void Retry()
{
NewGame(Level);
}
public void Retry() => NewGame(Level);

/// <summary>
/// Moves the player in a certain direction.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -191,33 +181,21 @@ public void MovePlayer(MovementDirection direction)
}
}

public List<Point2D> GetTargets()
{
return board.Targets;
}
public List<Point2D> 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<Tile> GetTiles()
{
return boardManager.GetTiles();
}
public IEnumerable<Tile> GetTiles() => boardManager.GetTiles();

void GenerateVariations()
{
for (int y = 0; y < GameDefines.BoardHeight; y++)
{
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);
}
Expand Down
Loading