Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using Snake.Model;
|
||||
|
||||
namespace Snake.Persistance;
|
||||
|
||||
public class SnakeLevelLoader{
|
||||
|
||||
public readonly string SaveFileName;
|
||||
public SnakeLevelLoader(string saveFileName){
|
||||
SaveFileName = saveFileName;
|
||||
if(!File.Exists(SaveFileName)){
|
||||
throw new FileNotFoundException("Save file not found", SaveFileName);
|
||||
}
|
||||
string file_content = File.ReadAllText(SaveFileName);
|
||||
_stored_levels = JsonSerializer.Deserialize<List<StoredSnakeLevel>>(file_content, new JsonSerializerOptions(){PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower})
|
||||
?? throw new JsonException("Failed to deserialize stored levels");
|
||||
}
|
||||
|
||||
private List<StoredSnakeLevel> _stored_levels;
|
||||
public ReadOnlyCollection<StoredSnakeLevel> StoredLevels => _stored_levels.AsReadOnly();
|
||||
|
||||
public SnakeLevel LoadLevel(StoredSnakeLevel level){
|
||||
return new SnakeLevel(level.Size,
|
||||
level.Obstacles.Select(a => new Point(a[0], a[1])),
|
||||
level.SnakeStartLength);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Snake.Persistance;
|
||||
|
||||
public class StoredSnakeLevel{
|
||||
public required string LevelName {get; init;}
|
||||
public required int Size {get; init;}
|
||||
public required int SnakeStartLength {get; init;}
|
||||
public required int[][] Obstacles {get; init;}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\model\model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user