Moved away from singleton pattern

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-17 20:24:39 +01:00
parent 4c28a66554
commit 55cf73f7f5
7 changed files with 70 additions and 47 deletions

View File

@@ -6,9 +6,7 @@ import 'package:wolf_3d_dart/wolf_3d_engine.dart';
import 'package:wolf_3d_dart/wolf_3d_synth.dart';
class Wolf3d {
Wolf3d._();
static final Wolf3d _instance = Wolf3d._();
static Wolf3d get I => _instance;
Wolf3d();
// --- State ---
final List<WolfensteinData> availableGames = [];
@@ -31,6 +29,13 @@ class Wolf3d {
int get activeEpisode => _activeEpisode;
void setActiveEpisode(int episodeIndex) {
if (_activeGame == null) {
throw StateError("No active game selected. Call setActiveGame() first.");
}
if (episodeIndex < 0 || episodeIndex >= _activeGame!.episodes.length) {
throw RangeError("Episode index out of range for the active game.");
}
_activeEpisode = episodeIndex;
}
@@ -45,12 +50,22 @@ class Wolf3d {
// --- Actions ---
void setActiveGame(WolfensteinData game) {
if (!availableGames.contains(game)) {
throw ArgumentError(
"The provided game data is not in the list of available games.",
);
}
if (_activeGame == game) {
return; // No change needed
}
_activeGame = game;
audio.activeGame = game;
}
/// Initializes the engine by loading available game data.
Future<void> init({String? directory}) async {
Future<Wolf3d> init({String? directory}) async {
await audio.init();
availableGames.clear();
@@ -63,7 +78,6 @@ class Wolf3d {
for (final version in versionsToTry) {
try {
final ext = version.version.fileExtension;
// final folder = 'assets/${version.path}';
final folder = 'packages/wolf_3d_assets/assets/${version.path}';
final data = WolfensteinLoader.loadFromBytes(
@@ -100,6 +114,8 @@ class Wolf3d {
debugPrint("External discovery failed: $e");
}
}
return this;
}
Future<ByteData?> _tryLoad(String path) async {