feat: Implement save game functionality with encoding/decoding
- Added SaveGameCodec for encoding and decoding save game files. - Introduced SaveGamePersistence interface for slot-based save game persistence. - Implemented FlutterSaveGamePersistence for file-based save management on Flutter. - Enhanced WolfEngine to support saving and loading game states. - Updated menu manager to include save/load game options. - Created tests for SaveGameCodec to ensure proper functionality. Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
|
||||
|
||||
/// CLI host adapter for slot-based game save persistence.
|
||||
///
|
||||
/// Files are stored under `~/.wolf3d_saves` by default and named
|
||||
/// `SAVEGAM{slot}.{ext}` where `{ext}` follows the active game version.
|
||||
class CliSaveGamePersistence implements SaveGamePersistence {
|
||||
CliSaveGamePersistence({String? directoryPath})
|
||||
: _directoryPath =
|
||||
directoryPath ??
|
||||
'${Platform.environment['HOME'] ?? '.'}/.wolf3d_saves';
|
||||
|
||||
final String _directoryPath;
|
||||
|
||||
@override
|
||||
Future<Uint8List?> load({
|
||||
required int slot,
|
||||
required GameVersion version,
|
||||
}) async {
|
||||
try {
|
||||
final File file = File(_slotPath(slot, version));
|
||||
if (!file.existsSync()) {
|
||||
return null;
|
||||
}
|
||||
return await file.readAsBytes();
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> save({
|
||||
required int slot,
|
||||
required GameVersion version,
|
||||
required Uint8List bytes,
|
||||
}) async {
|
||||
final Directory dir = Directory(_directoryPath);
|
||||
if (!dir.existsSync()) {
|
||||
await dir.create(recursive: true);
|
||||
}
|
||||
|
||||
await File(_slotPath(slot, version)).writeAsBytes(bytes, flush: true);
|
||||
}
|
||||
|
||||
String _slotPath(int slot, GameVersion version) {
|
||||
final String normalizedSlot = slot.clamp(0, 9).toString();
|
||||
return '$_directoryPath/SAVEGAM$normalizedSlot.${version.fileExtension}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user