refactor: implement singleton pattern for key managers and improve resource access
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.Events;
|
||||
@@ -7,17 +6,18 @@ namespace Mr.BrickAdventures.scripts.Events;
|
||||
[GlobalClass]
|
||||
public partial class SpeedRunEventHandler : Node
|
||||
{
|
||||
private SpeedRunManager _speedRunManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
|
||||
EventBus.Instance.LevelCompleted += OnLevelCompleted;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
EventBus.Instance.LevelCompleted -= OnLevelCompleted;
|
||||
}
|
||||
|
||||
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
|
||||
{
|
||||
_speedRunManager.Split();
|
||||
SpeedRunManager.Instance?.Split();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -12,13 +11,11 @@ public partial class AudioSettings : Control
|
||||
[Export] public Control AudioSettingsControl { get; set; }
|
||||
[Export] public float MuteThreshold { get; set; } = -20f;
|
||||
|
||||
private UIManager _uiManager;
|
||||
private ConfigFileHandler _configFileHandler;
|
||||
private UIManager UIManager => UIManager.Instance;
|
||||
private ConfigFileHandler ConfigFileHandler => ConfigFileHandler.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
_configFileHandler = GetNode<ConfigFileHandler>(Constants.ConfigFileHandlerPath);
|
||||
Initialize();
|
||||
MasterVolumeSlider.ValueChanged += OnMasterVolumeChanged;
|
||||
MusicVolumeSlider.ValueChanged += OnMusicVolumeChanged;
|
||||
@@ -35,10 +32,10 @@ public partial class AudioSettings : Control
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (!@event.IsActionReleased("ui_cancel")) return;
|
||||
if (!_uiManager.IsScreenOnTop(AudioSettingsControl)) return;
|
||||
if (!UIManager.IsScreenOnTop(AudioSettingsControl)) return;
|
||||
|
||||
SaveSettings();
|
||||
_uiManager.PopScreen();
|
||||
UIManager.PopScreen();
|
||||
}
|
||||
|
||||
private void OnSfxVolumeChanged(double value)
|
||||
@@ -84,7 +81,7 @@ public partial class AudioSettings : Control
|
||||
|
||||
private void SaveSettings()
|
||||
{
|
||||
var settingsConfig = _configFileHandler.SettingsConfig;
|
||||
var settingsConfig = ConfigFileHandler.SettingsConfig;
|
||||
settingsConfig.SetValue("audio_settings", "master_volume", MasterVolumeSlider.Value);
|
||||
settingsConfig.SetValue("audio_settings", "music_volume", MusicVolumeSlider.Value);
|
||||
settingsConfig.SetValue("audio_settings", "sfx_volume", SfxVolumeSlider.Value);
|
||||
@@ -94,7 +91,7 @@ public partial class AudioSettings : Control
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
var settingsConfig = _configFileHandler.SettingsConfig;
|
||||
var settingsConfig = ConfigFileHandler.SettingsConfig;
|
||||
if (!settingsConfig.HasSection("audio_settings")) return;
|
||||
|
||||
var masterVolume = (float)settingsConfig.GetValue("audio_settings", "master_volume", MasterVolumeSlider.Value);
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class Credits : Control
|
||||
{
|
||||
private UIManager _uiManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
}
|
||||
private UIManager UIManager => UIManager.Instance;
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (!@event.IsActionPressed("ui_cancel")) return;
|
||||
if (_uiManager != null && _uiManager.IsScreenOnTop(this))
|
||||
if (UIManager != null && UIManager.IsScreenOnTop(this))
|
||||
{
|
||||
_uiManager.PopScreen();
|
||||
UIManager.PopScreen();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -16,15 +15,12 @@ public partial class MainMenu : Control
|
||||
[Export] public Control SettingsControl { get; set; }
|
||||
[Export] public Control CreditsControl { get; set; }
|
||||
|
||||
private SaveSystem _saveSystem;
|
||||
private GameManager _gameManager;
|
||||
private UIManager _uiManager;
|
||||
private SaveSystem SaveSystem => SaveSystem.Instance;
|
||||
private GameManager GameManager => GameManager.Instance;
|
||||
private UIManager UIManager => UIManager.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_saveSystem = SaveSystem.Instance;
|
||||
_gameManager = GameManager.Instance;
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
NewGameButton.Pressed += OnNewGamePressed;
|
||||
ContinueButton.Pressed += OnContinuePressed;
|
||||
@@ -33,9 +29,9 @@ public partial class MainMenu : Control
|
||||
ExitButton.Pressed += OnExitPressed;
|
||||
|
||||
VersionLabel.Text = $"v. {ProjectSettings.GetSetting("application/config/version")}";
|
||||
ContinueButton.Disabled = !_saveSystem.CheckSaveExists();
|
||||
ContinueButton.Disabled = !SaveSystem.CheckSaveExists();
|
||||
|
||||
if (_saveSystem.CheckSaveExists())
|
||||
if (SaveSystem.CheckSaveExists())
|
||||
ContinueButton.GrabFocus();
|
||||
else
|
||||
NewGameButton.GrabFocus();
|
||||
@@ -43,26 +39,26 @@ public partial class MainMenu : Control
|
||||
|
||||
private void OnExitPressed()
|
||||
{
|
||||
_gameManager.QuitGame();
|
||||
GameManager.QuitGame();
|
||||
}
|
||||
|
||||
private void OnCreditsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(CreditsControl);
|
||||
UIManager.PushScreen(CreditsControl);
|
||||
}
|
||||
|
||||
private void OnSettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(SettingsControl);
|
||||
UIManager.PushScreen(SettingsControl);
|
||||
}
|
||||
|
||||
private void OnContinuePressed()
|
||||
{
|
||||
_gameManager.ContinueGame();
|
||||
GameManager.ContinueGame();
|
||||
}
|
||||
|
||||
private void OnNewGamePressed()
|
||||
{
|
||||
_gameManager.StartNewGame();
|
||||
GameManager.StartNewGame();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -14,13 +13,11 @@ public partial class PauseMenu : Control
|
||||
[Export] public Button SettingsButton { get; set; }
|
||||
[Export] public PackedScene MainMenuScene { get; set; }
|
||||
|
||||
private GameManager _gameManager;
|
||||
private UIManager _uiManager;
|
||||
private GameManager GameManager => GameManager.Instance;
|
||||
private UIManager UIManager => UIManager.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GameManager.Instance;
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
ResumeButton.Pressed += OnResumePressed;
|
||||
MainMenuButton.Pressed += OnMainMenuPressed;
|
||||
@@ -33,36 +30,36 @@ public partial class PauseMenu : Control
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (!@event.IsActionPressed("pause")) return;
|
||||
if (_uiManager.IsVisibleOnStack(PauseMenuControl))
|
||||
if (UIManager.IsVisibleOnStack(PauseMenuControl))
|
||||
OnResumePressed();
|
||||
else
|
||||
{
|
||||
_uiManager.PushScreen(PauseMenuControl);
|
||||
_gameManager.PauseGame();
|
||||
UIManager.PushScreen(PauseMenuControl);
|
||||
GameManager.PauseGame();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(SettingsControl);
|
||||
_gameManager.PauseGame();
|
||||
UIManager.PushScreen(SettingsControl);
|
||||
GameManager.PauseGame();
|
||||
}
|
||||
|
||||
private void OnQuitPressed()
|
||||
{
|
||||
_gameManager.QuitGame();
|
||||
GameManager.QuitGame();
|
||||
}
|
||||
|
||||
private void OnMainMenuPressed()
|
||||
{
|
||||
_gameManager.ResumeGame();
|
||||
_gameManager.ResetCurrentSessionState();
|
||||
GameManager.ResumeGame();
|
||||
GameManager.ResetCurrentSessionState();
|
||||
GetTree().ChangeSceneToPacked(MainMenuScene);
|
||||
}
|
||||
|
||||
private void OnResumePressed()
|
||||
{
|
||||
_uiManager.PopScreen();
|
||||
_gameManager.ResumeGame();
|
||||
UIManager.PopScreen();
|
||||
GameManager.ResumeGame();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -16,11 +15,10 @@ public partial class SettingsMenu : Control
|
||||
[Export] public Button DisplaySettingsButton { get; set; }
|
||||
[Export] public Button GameplaySettingsButton { get; set; }
|
||||
|
||||
private UIManager _uiManager;
|
||||
private UIManager UIManager => UIManager.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
InputSettingsButton.Pressed += OnInputSettingsPressed;
|
||||
AudioSettingsButton.Pressed += OnAudioSettingsPressed;
|
||||
@@ -36,26 +34,26 @@ public partial class SettingsMenu : Control
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (!@event.IsActionPressed("ui_cancel")) return;
|
||||
if (_uiManager.IsScreenOnTop(SettingsMenuControl)) _uiManager.PopScreen();
|
||||
if (UIManager.IsScreenOnTop(SettingsMenuControl)) UIManager.PopScreen();
|
||||
}
|
||||
|
||||
private void OnInputSettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(InputSettingsControl);
|
||||
UIManager.PushScreen(InputSettingsControl);
|
||||
}
|
||||
|
||||
private void OnAudioSettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(AudioSettingsControl);
|
||||
UIManager.PushScreen(AudioSettingsControl);
|
||||
}
|
||||
|
||||
private void OnDisplaySettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(DisplaySettingsControl);
|
||||
UIManager.PushScreen(DisplaySettingsControl);
|
||||
}
|
||||
|
||||
private void OnGameplaySettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(GameplaySettingsControl);
|
||||
UIManager.PushScreen(GameplaySettingsControl);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
@@ -17,14 +16,8 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
|
||||
[Signal] public delegate void ExitTriggeredEventHandler();
|
||||
|
||||
private GameManager _gameManager;
|
||||
private AchievementManager _achievementManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GameManager.Instance;
|
||||
_achievementManager = GetNode<AchievementManager>(Constants.AchievementManagerPath);
|
||||
|
||||
BodyEntered += OnExitAreaBodyEntered;
|
||||
|
||||
}
|
||||
@@ -34,10 +27,9 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
if (Locked) return;
|
||||
|
||||
EmitSignalExitTriggered();
|
||||
_achievementManager.UnlockAchievement(AchievementId);
|
||||
// Get current level from GameStateStore
|
||||
AchievementManager.Instance?.UnlockAchievement(AchievementId);
|
||||
var currentLevel = GameStateStore.Instance?.Session.CurrentLevel ?? 0;
|
||||
_gameManager.UnlockLevel(currentLevel + 1);
|
||||
GameManager.Instance?.UnlockLevel(currentLevel + 1);
|
||||
CallDeferred(nameof(GoToNextLevel));
|
||||
}
|
||||
|
||||
@@ -54,6 +46,6 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
|
||||
private void GoToNextLevel()
|
||||
{
|
||||
_gameManager.OnLevelComplete();
|
||||
GameManager.Instance?.OnLevelComplete();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -17,12 +16,8 @@ public partial class LeverComponent : Node
|
||||
[Signal]
|
||||
public delegate void ActivatedEventHandler();
|
||||
|
||||
private FloatingTextManager _floatingTextManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
|
||||
if (Area == null)
|
||||
{
|
||||
GD.PushError("LeverComponent: Area is not set.");
|
||||
@@ -52,7 +47,7 @@ public partial class LeverComponent : Node
|
||||
private async Task Activate()
|
||||
{
|
||||
EmitSignalActivated();
|
||||
_floatingTextManager?.ShowMessage("Activated!", ((Node2D)Owner).GlobalPosition);
|
||||
FloatingTextManager.Instance?.ShowMessage("Activated!", ((Node2D)Owner).GlobalPosition);
|
||||
Sfx?.Play();
|
||||
Sprite.Frame = StartAnimationIndex + 1;
|
||||
var timer = GetTree().CreateTimer(AnimationDuration);
|
||||
|
||||
Reference in New Issue
Block a user