сделал сейв предметов в файл

This commit is contained in:
polten0 2024-08-20 16:18:26 +03:00
parent 22eac8b847
commit e9b00841b8

View file

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers.ItemManager
@ -15,7 +17,7 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
{
LoadPlayerData();
}
public Dictionary<string, int> items;
public Dictionary<string, int> items = new Dictionary<string, int>();
/// <summary>
/// Принимает тэг и крафтит этот объект
/// </summary>
@ -50,11 +52,44 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
public void LoadPlayerData()
{
//TODO
items = new Dictionary<string, int>();
items.Add("wood", 5);
items.Add("steel", 110);
items.Add("peeble", 6);
if (File.Exists("Items.txt"))
{
string data;
using (StreamReader reader = new StreamReader("Items.txt"))
{
data = reader.ReadToEnd();
}
List<PlayerItemsData> itemsDatas = JsonSerializer.Deserialize<List<PlayerItemsData>>(data);
foreach (var item in itemsDatas)
{
items.Add(item.Name, item.Count);
}
}
}
public void SavePlayerData()
{
var options = new JsonSerializerOptions { WriteIndented = true };
List<PlayerItemsData> playerItemsDatas = new List<PlayerItemsData>();
foreach (var item in items)
{
playerItemsDatas.Add(new PlayerItemsData { Name = item.Key, Count = item.Value });
}
string data = JsonSerializer.Serialize<List<PlayerItemsData>>(playerItemsDatas);
using (StreamWriter outputFile = new StreamWriter("Items.txt", new FileStreamOptions() { Mode = FileMode.Create, Access = FileAccess.Write } ))
{
outputFile.WriteLine(data);
}
}
}
class PlayerItemsData
{
public string Name { get; set; }
public int Count { get; set; }
}
}