Add logging functionality to GamePresenter and implement GodotLogger

This commit is contained in:
2025-10-29 01:50:55 +01:00
parent 626f81cd85
commit d046130f17
3 changed files with 57 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using CryptonymThunder.Code.Autoloads; using CryptonymThunder.Code.Autoloads;
using CryptonymThunder.Code.Factories; using CryptonymThunder.Code.Factories;
@@ -11,6 +12,8 @@ using GameCore.ECS.Interfaces;
using GameCore.Events; using GameCore.Events;
using GameCore.Input; using GameCore.Input;
using GameCore.Inventory; using GameCore.Inventory;
using GameCore.Logging;
using GameCore.Logging.Interfaces;
using GameCore.Movement; using GameCore.Movement;
using GameCore.Player; using GameCore.Player;
using Godot; using Godot;
@@ -30,6 +33,7 @@ public partial class GamePresenter : Node
private GodotInputService _inputService; private GodotInputService _inputService;
private GodotWorldQuery _worldQuery; private GodotWorldQuery _worldQuery;
private PresenterFactory _presenterFactory; private PresenterFactory _presenterFactory;
private ILogger _logger = new NullLogger();
private readonly Dictionary<int, List<IPresenterComponent>> _presenterComponents = new(); private readonly Dictionary<int, List<IPresenterComponent>> _presenterComponents = new();
private readonly Dictionary<int, IEntityPresenter> _presenters = new(); private readonly Dictionary<int, IEntityPresenter> _presenters = new();
@@ -44,9 +48,17 @@ public partial class GamePresenter : Node
if (SimulationConfig != null) if (SimulationConfig != null)
{ {
simConfig.GravityStrength = SimulationConfig.GravityStrength; 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 effectFactory = new EffectFactory();
var componentFactory = new ComponentFactory(effectFactory); var componentFactory = new ComponentFactory(effectFactory);
@@ -78,15 +90,6 @@ public partial class GamePresenter : Node
_world.Subscribe<EntityDiedEvent>(OnEntityDied); _world.Subscribe<EntityDiedEvent>(OnEntityDied);
_world.Subscribe<SpawnEntityEvent>(OnSpawnEntity); _world.Subscribe<SpawnEntityEvent>(OnSpawnEntity);
_world.Subscribe<EquipWeaponEvent>(OnWeaponEquipped);
_world.Subscribe<WeaponFiredEvent>(e =>
{
GD.Print($"Weapon fired by Entity ID: {e.Owner.Id}");
});
_world.Subscribe<WeaponFireFailedEvent>(e =>
{
GD.Print($"Weapon fire failed!");
});
RegisterAllSceneEntities(); RegisterAllSceneEntities();
@@ -95,11 +98,6 @@ public partial class GamePresenter : Node
_presenterComponents.Add(playerData.Entity.Id, playerData.Components); _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) public override void _Input(InputEvent @event)
{ {
_inputService?.HandleInputEvent(@event); _inputService?.HandleInputEvent(@event);
@@ -181,4 +179,12 @@ public partial class GamePresenter : Node
return null; return null;
} }
public override void _ExitTree()
{
if (_logger is IDisposable disposableLogger)
{
disposableLogger.Dispose();
}
}
} }

View File

@@ -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}");
}
}
}

View File

@@ -0,0 +1 @@
uid://bu65qwpq5f8ba