61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using Godot;
|
|
using GodotHostTest.Interfaces;
|
|
using OwofGames.AetherBind;
|
|
using R3;
|
|
|
|
namespace GodotHostTest.Game.Target;
|
|
|
|
public partial class Target : Node2D, ITarget
|
|
{
|
|
private readonly Subject<Unit> _hit = new();
|
|
|
|
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;
|
|
|
|
[Inject] private ITargetCollector _targetCollector = null!;
|
|
|
|
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);
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
_timedOut.Dispose();
|
|
_hit.Dispose();
|
|
}
|
|
|
|
// todo: hook to collision
|
|
private void OnHit()
|
|
{
|
|
Disable();
|
|
TimeOutTimer.Stop();
|
|
_hit.OnNext(Unit.Default);
|
|
}
|
|
|
|
private void Disable()
|
|
{
|
|
Image.Visible = false;
|
|
CollisionArea.Monitoring = false;
|
|
}
|
|
|
|
private void OnTimeout()
|
|
{
|
|
Disable();
|
|
_timedOut.OnNext(Unit.Default);
|
|
}
|
|
} |