Guided projectile: binding lives and dies with the projectile; no explosion without a detonation
Unity-only, compile-checked by reading. * F5: GuidedProjectile.Binding is one owner: a new binding disposes the one it replaces, Kill disposes it, and OnDestroy does too, so a projectile torn down with its scene cannot leave an observer on the zone's subjects to touch a destroyed transform. * F6: Resolve takes whether the simulation detonated. A miss (a contact or delayed round that missed, a target that left) fades out with no explosion. * F7: BurstAt resets the previous target position and target velocity, so the retarget jump does not feed the guidance a bogus target velocity. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
a193853d34
commit
8c63fbe50e
2 changed files with 33 additions and 7 deletions
|
|
@ -50,20 +50,45 @@ public class GuidedProjectile : MonoBehaviour
|
|||
|
||||
// The simulation's verdict on this round, bound by GuidedProjectileManager to Zone.ShotCommitted and
|
||||
// ShotResolved for its ShotId: this object flies and looks, and neither decides where the round bursts nor when.
|
||||
public IDisposable Binding { get; set; }
|
||||
// The one owner of the subscription's lifetime: a new binding disposes the one it replaces, Kill disposes it,
|
||||
// and so does destruction, so a projectile torn down with its scene never leaves an observer on the zone's
|
||||
// subjects to touch a destroyed transform.
|
||||
public IDisposable Binding
|
||||
{
|
||||
get => _binding;
|
||||
set
|
||||
{
|
||||
_binding?.Dispose();
|
||||
_binding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private IDisposable _binding;
|
||||
|
||||
void OnDestroy() => Binding = null;
|
||||
|
||||
// The commit named a burst point: fly to it, whatever this round was homing on (a target it would have
|
||||
// passed, or a Range clamp it would have overshot).
|
||||
// passed, or a Range clamp it would have overshot). The point is a static target, so the old target's motion
|
||||
// is dropped with it: the jump to the new point must not feed the guidance a target velocity.
|
||||
public void BurstAt(Vector3 point)
|
||||
{
|
||||
Target = null;
|
||||
TargetPosition = () => point;
|
||||
_previousTargetPosition = point;
|
||||
_targetVelocity = Vector3.zero;
|
||||
}
|
||||
|
||||
// The resolution: the round bursts now, at the simulation's burst point when it has one.
|
||||
public void Resolve(Vector3? point)
|
||||
// The resolution. A round that detonated (a hit, or a burst) bursts now, at the simulation's burst point when
|
||||
// it has one; a round the simulation resolved without a detonation (a contact or delayed round that missed,
|
||||
// one whose target is gone) just falls away, with no explosion to show.
|
||||
public void Resolve(bool detonated, Vector3? point)
|
||||
{
|
||||
if (!_alive || !_active) return;
|
||||
if (!detonated)
|
||||
{
|
||||
StartCoroutine(FadeOut());
|
||||
return;
|
||||
}
|
||||
if (point.HasValue) transform.position = point.Value;
|
||||
if (HitEffect != null)
|
||||
{
|
||||
|
|
@ -195,7 +220,6 @@ public class GuidedProjectile : MonoBehaviour
|
|||
|
||||
IEnumerator Kill()
|
||||
{
|
||||
Binding?.Dispose();
|
||||
Binding = null;
|
||||
_active = false;
|
||||
_alive = false;
|
||||
|
|
|
|||
|
|
@ -16,8 +16,10 @@ public class GuidedProjectileManager : InstantWeaponEffectManager
|
|||
Vector3 At(ShotOutcome o) => new Vector3(o.BurstPoint.x, p.transform.position.y, o.BurstPoint.y);
|
||||
var bindings = new CompositeDisposable();
|
||||
zone.ShotCommitted.Where(o => o.ShotId == shotId && o.HasBurstPoint).Subscribe(o => p.BurstAt(At(o))).AddTo(bindings);
|
||||
zone.ShotResolved.Where(o => o.ShotId == shotId).Subscribe(o => p.Resolve(o.HasBurstPoint ? At(o) : (Vector3?) null)).AddTo(bindings);
|
||||
p.Binding = bindings;
|
||||
zone.ShotResolved.Where(o => o.ShotId == shotId)
|
||||
.Subscribe(o => p.Resolve(o.Result == ShotResult.Hit || o.Result == ShotResult.Burst, o.HasBurstPoint ? At(o) : (Vector3?) null))
|
||||
.AddTo(bindings);
|
||||
p.Binding = bindings; // replaces, and so disposes, any binding the projectile already had
|
||||
}
|
||||
|
||||
public Prototype ProjectilePrototype;
|
||||
|
|
|
|||
Loading…
Reference in a new issue