feat: working version with scopes.

This commit is contained in:
redglow
2026-07-18 12:09:43 +02:00
parent 23e160438f
commit 04f0b5ef16
15 changed files with 120 additions and 87 deletions
+3 -1
View File
@@ -14,6 +14,8 @@ public partial class GameServiceSource : RootServiceSource
builder
.AddSingleton<ICurrentLevel, CurrentLevel>()
.AddInstance<ILevelDataProvider, LevelsData>(_levelsData)
.AddValueProvider<ILevel, RootScope, LevelScope>();
.AddValueProvider<ILevel, RootScope, LevelScope>()
.AddValueProvider<ITargetCollector, LevelScope>()
.AddValueProvider<ITargetSpawner, LevelScope>();
}
}
+7 -6
View File
@@ -1,5 +1,7 @@
using Godot;
using GodotHostTest.GodotDI;
using GodotHostTest.Interfaces;
using OwofGames.GodotLume;
namespace GodotHostTest.Game;
@@ -7,10 +9,9 @@ public partial class LevelServiceSource : ServiceSource
{
[Export] private TargetSpawner _targetSpawner = null!;
// protected override void ConfigureServices(IServiceCollection serviceCollection)
// {
// serviceCollection
// .AddSingleton<ITargetCollector>(_targetSpawner)
// .AddSingleton<ITargetSpawner>(_targetSpawner);
// }
protected override void OnScopeCreated(IProvider serviceProvider)
{
serviceProvider.Get<IValueSetter<ITargetCollector>>().Set(_targetSpawner);
serviceProvider.Get<IValueSetter<ITargetSpawner>>().Set(_targetSpawner);
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ public partial class LevelsData : Resource, ILevelDataProvider
public ILevel GetLevelData(int levelNumber)
{
var entry = _levelDataEntries[levelNumber];
var entry = _levelDataEntries[levelNumber - 1];
return entry;
}
}
+2 -2
View File
@@ -10,12 +10,12 @@ namespace GodotHostTest.Game;
public partial class Projectile : Node2D
{
[Inject] private readonly ILogger<Projectile> _logger = null!;
[Inject] private readonly ITargetSpawner _targetSpawner = null!;
[Inject] private readonly IValueGetter<ITargetSpawner> _targetSpawner = null!;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_targetSpawner.TargetSpawned
_targetSpawner.Value.TargetSpawned
.Subscribe(_ => _logger.LogInformation("Projectile got informed of target spawning")).AddTo(this);
}
}
+5 -5
View File
@@ -1,22 +1,24 @@
using R3;
using Godot;
using GodotHostTest.GodotDI;
using GodotHostTest.Helpers;
using GodotHostTest.Interfaces;
using R3;
namespace GodotHostTest.Game;
public partial class Score : Control, IScore
{
[Inject] private readonly IValueGetter<ITargetSpawner> _targetSpawner = null!;
[Export] private AnimationPlayer _animationPlayer = null!;
[Export] private Label _label = null!;
[Inject] private readonly ITargetSpawner _targetSpawner = null!;
private int _score;
int IScore.Score => _score;
public override void _Ready()
{
_targetSpawner.TargetSpawned.Subscribe(OnTargetSpawned).AddTo(this);
_targetSpawner.Value.TargetSpawned.Subscribe(OnTargetSpawned).AddTo(this);
base._Ready();
}
@@ -34,6 +36,4 @@ public partial class Score : Control, IScore
{
_label.Text = $"Score: {_score}";
}
int IScore.Score => _score;
}
+19 -19
View File
@@ -7,19 +7,30 @@ namespace GodotHostTest.Game;
public partial class Target : Node2D, ITarget
{
[Export] public double TimeOutDurationInSeconds;
[Export] public required Timer TimeOutTimer;
[Export] public required Sprite2D Image;
[Export] public required Area2D CollisionArea;
private readonly Subject<Unit> _timedOut = new();
private readonly Subject<Unit> _hit = new();
[Inject] private readonly ITargetCollector _targetCollector = null!;
[Inject] private readonly IValueGetter<ITargetCollector> _targetCollector = null!;
private readonly Subject<Unit> _timedOut = new();
[Export] public required Area2D CollisionArea;
[Export] public required Sprite2D Image;
[Export] public double TimeOutDurationInSeconds;
[Export] public required Timer TimeOutTimer;
public Observable<Unit> TimedOut => _timedOut.AsObservable();
public Observable<Unit> Hit => _hit.AsObservable();
public void Enable()
{
Image.Visible = true;
CollisionArea.Monitoring = true;
TimeOutTimer.Start(TimeOutDurationInSeconds);
}
public override void _Ready()
{
_targetCollector.NewTarget(this);
_targetCollector.Value.NewTarget(this);
}
public override void _ExitTree()
@@ -28,10 +39,6 @@ public partial class Target : Node2D, ITarget
_hit.Dispose();
}
public Observable<Unit> TimedOut => _timedOut.AsObservable();
public Observable<Unit> Hit => _hit.AsObservable();
// todo: hook to collision
private void OnHit()
{
@@ -40,13 +47,6 @@ public partial class Target : Node2D, ITarget
_hit.OnNext(Unit.Default);
}
public void Enable()
{
Image.Visible = true;
CollisionArea.Monitoring = true;
TimeOutTimer.Start(TimeOutDurationInSeconds);
}
private void Disable()
{
Image.Visible = false;