diff --git a/Code/Presenters/GamePresenter.cs b/Code/Presenters/GamePresenter.cs index dd36ee1..2671d94 100644 --- a/Code/Presenters/GamePresenter.cs +++ b/Code/Presenters/GamePresenter.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using CryptonymThunder.Code.Autoloads; using CryptonymThunder.Code.Factories; @@ -11,6 +12,8 @@ using GameCore.ECS.Interfaces; using GameCore.Events; using GameCore.Input; using GameCore.Inventory; +using GameCore.Logging; +using GameCore.Logging.Interfaces; using GameCore.Movement; using GameCore.Player; using Godot; @@ -30,6 +33,7 @@ public partial class GamePresenter : Node private GodotInputService _inputService; private GodotWorldQuery _worldQuery; private PresenterFactory _presenterFactory; + private ILogger _logger = new NullLogger(); private readonly Dictionary> _presenterComponents = new(); private readonly Dictionary _presenters = new(); @@ -44,9 +48,17 @@ public partial class GamePresenter : Node if (SimulationConfig != null) { simConfig.GravityStrength = SimulationConfig.GravityStrength; + + if (simConfig.LoggingEnabled) + { + _logger = new CompositeLogger( + new GodotLogger(), + new FileLogger("game_log.txt", LogLevel.Info) + ); + } } - _world = new World(_inputService, _worldQuery, simConfig); + _world = new World(_inputService, _worldQuery, simConfig, _logger); var effectFactory = new EffectFactory(); var componentFactory = new ComponentFactory(effectFactory); @@ -78,15 +90,6 @@ public partial class GamePresenter : Node _world.Subscribe(OnEntityDied); _world.Subscribe(OnSpawnEntity); - _world.Subscribe(OnWeaponEquipped); - _world.Subscribe(e => - { - GD.Print($"Weapon fired by Entity ID: {e.Owner.Id}"); - }); - _world.Subscribe(e => - { - GD.Print($"Weapon fire failed!"); - }); RegisterAllSceneEntities(); @@ -95,11 +98,6 @@ public partial class GamePresenter : Node _presenterComponents.Add(playerData.Entity.Id, playerData.Components); } - private void OnWeaponEquipped(EquipWeaponEvent e) - { - GD.Print($"Weapon equipped: {e.NewWeaponItemId} for Entity ID: {e.Owner.Id}"); - } - public override void _Input(InputEvent @event) { _inputService?.HandleInputEvent(@event); @@ -181,4 +179,12 @@ public partial class GamePresenter : Node return null; } + + public override void _ExitTree() + { + if (_logger is IDisposable disposableLogger) + { + disposableLogger.Dispose(); + } + } } \ No newline at end of file diff --git a/Code/Services/GodotLogger.cs b/Code/Services/GodotLogger.cs new file mode 100644 index 0000000..4ecc91e --- /dev/null +++ b/Code/Services/GodotLogger.cs @@ -0,0 +1,35 @@ +using System; +using GameCore.Logging.Interfaces; +using Godot; + +namespace CryptonymThunder.Code.Services; + +public class GodotLogger : ILogger +{ + public void Debug(string message) + { + GD.Print($"[DEBUG] {message}"); + } + + public void Info(string message) + { + GD.Print($"[INFO] {message}"); + } + + public void Warn(string message) + { + GD.PushWarning(message); + } + + public void Error(string message, Exception ex = null) + { + if (ex != null) + { + GD.PrintErr($"[ERROR] {message} Exception: {ex}"); + } + else + { + GD.PrintErr($"[ERROR] {message}"); + } + } +} \ No newline at end of file diff --git a/Code/Services/GodotLogger.cs.uid b/Code/Services/GodotLogger.cs.uid new file mode 100644 index 0000000..7c24aeb --- /dev/null +++ b/Code/Services/GodotLogger.cs.uid @@ -0,0 +1 @@ +uid://bu65qwpq5f8ba