feat: working version with scopes.
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user