-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
285 lines (238 loc) · 10.3 KB
/
Program.cs
File metadata and controls
285 lines (238 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serialization
{
internal class Program
{
static void Main(string[] args)
{
try
{
SaveSystem saveSystem = new SaveSystem();
var binFormatter = new BinaryFormatter();
List<Item> starterItems = new List<Item>
{
new Item("Лопата", ItemRarity.Common),
new Item("Кирка", ItemRarity.Common),
new Item("Меч", ItemRarity.Common),
new Item("Сливочное пиво", ItemRarity.Rare)
};
Player player = new Player("Bobby", 100, 50, 1200, starterItems);
GameState gameState = new GameState(player, "Лес");
GameState loadedGame;
string saveDirectory = "Saves";
bool isPlaying = true;
while (isPlaying)
{
Console.Clear();
Console.WriteLine("1 - Сохранить игру");
Console.WriteLine("2 - Загрузить игру");
Console.WriteLine("3 - Список сохранений");
Console.WriteLine("4 - Удалить сохранение");
Console.WriteLine("5 - Выход");
Console.Write("Введите значение: ");
if (int.TryParse(Console.ReadLine(), out int choice))
{
switch (choice)
{
case 1:
Console.Write("Введите имя сохранения: ");
string saveName = Console.ReadLine();
saveSystem.SaveGame(gameState, saveName, binFormatter);
Console.WriteLine("Сохранение выполнено успешно!");
break;
case 2:
Console.Write("Введите имя сохранения для загрузки: ");
string loadName = Console.ReadLine();
loadedGame = saveSystem.LoadGame(loadName, binFormatter);
gameState = loadedGame;
Console.WriteLine("Загрузка выполнена успешно!");
break;
case 3:
Console.WriteLine("\nСписок сохранений:");
foreach (string saveFile in saveSystem.GetAllSaveFiles(saveDirectory))
{
Console.WriteLine(saveFile);
}
break;
case 4:
Console.Write("Введите имя файла: ");
saveSystem.DeleteSave(Console.ReadLine());
break;
case 5:
isPlaying = false;
break;
}
}
Console.WriteLine("\nНажмите для продолжения...");
Console.ReadKey(false);
}
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
}
public enum ItemRarity
{
Common,
Rare,
Epic,
Legendary
}
[Serializable]
public class Player
{
public string Name { get; }
public int Level { get; private set; }
public int Experience { get; private set; }
public int Health { get; private set; }
public int MaxHealth { get; private set; }
public int Mana { get; private set; }
public int MaxMana { get; private set; }
public float Gold { get; private set; }
public List<Item> Inventory { get; private set; } = new List<Item>();
public Player(string name, int maxHealth, int maxMana, float gold, List<Item> inventory)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Имя игрока не может быть пустым");
Name = name;
if (maxHealth > 0)
{
Health = maxHealth;
MaxHealth = maxHealth;
}
else throw new ArgumentException("Максимальное значение здоровья не может быть меньше одного");
if (maxMana > 0)
{
Mana = maxMana;
MaxMana = maxMana;
}
else throw new ArgumentException("Максимальное значение маны не может быть меньше одного");
Gold = gold;
Inventory = inventory;
Level = 1;
Experience = 0;
}
}
[Serializable]
public class Item
{
public int Id { get; }
public string Name { get; }
public ItemRarity Rarity { get; }
private static int _nextId = 0;
public Item(string name, ItemRarity rarity)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Имя предмета не может быть пустым");
Id = _nextId++;
Name = name;
Rarity = rarity;
}
}
[Serializable]
public class GameState
{
public Player CurrentPlayer { get; }
public string CurrentLocation { get; private set; }
public DateTime SaveTime { get; private set; }
public int PlayTimeInMinutes { get; private set; }
public Dictionary<string, bool> QuestProgress { get; private set; } = new Dictionary<string, bool>();
public List<string> UnlockedLocations { get; } = new List<string>();
[NonSerialized]
private DateTime _startTime;
public GameState(Player player, string location)
{
CurrentPlayer = player;
CurrentLocation = location;
_startTime = DateTime.Now;
}
public void UpdatePlayTime()
{
PlayTimeInMinutes = (int)(DateTime.Now - _startTime).TotalMinutes;
}
public void SetSaveTime()
{
SaveTime = DateTime.Now;
}
public DateTime GetStartTime() { return _startTime; }
public void RestoreStartTime()
{
_startTime = SaveTime.AddMinutes(-PlayTimeInMinutes);
}
}
public class SaveSystem
{
public void SaveGame(GameState gameState, string saveFileName, BinaryFormatter binFormatter)
{
gameState.UpdatePlayTime();
gameState.SetSaveTime();
string saveDirectory = "Saves";
if (!Directory.Exists(saveDirectory))
{
Directory.CreateDirectory(saveDirectory);
}
string filePath = Path.Combine(saveDirectory, $"{saveFileName}.bin");
using (var file = new FileStream(filePath, FileMode.OpenOrCreate))
{
binFormatter.Serialize(file, gameState);
Console.WriteLine("Данные сохранены");
}
}
public GameState LoadGame(string saveFileName, BinaryFormatter binFormatter)
{
string saveDirectory = "Saves";
string filePath = Path.Combine(saveDirectory, $"{saveFileName}.bin");
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"Файл сохранения не найден: {filePath}");
}
using (var file = new FileStream(filePath, FileMode.OpenOrCreate))
{
var newGameState = binFormatter.Deserialize(file) as GameState;
if (newGameState == null)
{
throw new Exception("Ошибка загрузки данных");
}
newGameState.RestoreStartTime();
newGameState.UpdatePlayTime();
Console.WriteLine("Данные загружены:");
Console.WriteLine($"Игрок: {newGameState.CurrentPlayer.Name}");
Console.WriteLine($"Локация: {newGameState.CurrentLocation}");
Console.WriteLine($"Время сохранения: {newGameState.SaveTime}");
Console.WriteLine($"Время с первого входа в игру: {newGameState.PlayTimeInMinutes} минут");
Console.WriteLine($"Время начала игры: {newGameState.GetStartTime()}");
return newGameState;
}
}
public List<string> GetAllSaveFiles(string saveDirectory)
{
if (!Directory.Exists(saveDirectory))
return new List<string>();
return Directory.GetFiles(saveDirectory, "*.bin").Select(Path.GetFileName).ToList();
}
public void DeleteSave(string saveFileName)
{
if (string.IsNullOrWhiteSpace(saveFileName))
{
Console.WriteLine("Имя файла не может быть пустым");
return;
}
string filePath = Path.Combine("Saves", $"{saveFileName}.bin");
if (File.Exists(filePath))
{
File.Delete(filePath);
Console.WriteLine($"Сохранение {saveFileName} удалено");
}
else
{
Console.WriteLine($"Файл {saveFileName} не найден");
}
}
}
}