diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index bc9bce8a2..3f69563ed 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -535,7 +535,7 @@ public class ActionGameManager : MonoBehaviour missileManager.OnFireGuided.Where(x => x.source == _currentEntity).Take(1).Subscribe(x => { FollowCamera.Follow = x.missile.transform; - FollowCamera.LookAt = x.target; + FollowCamera.LookAt = x.target ? x.target : x.missile.transform; x.missile.OnKill += () => { FollowCamera.LookAt = ZoneRenderer.EntityInstances[CurrentEntity].LookAtPoint; diff --git a/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs b/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs index 9d9b02070..b5fdc47da 100644 --- a/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs +++ b/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs @@ -48,6 +48,31 @@ public class GuidedProjectile : MonoBehaviour public event Action OnKill; + // 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 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). + public void BurstAt(Vector3 point) + { + Target = null; + TargetPosition = () => point; + } + + // The resolution: the round bursts now, at the simulation's burst point when it has one. + public void Resolve(Vector3? point) + { + if (!_alive || !_active) return; + if (point.HasValue) transform.position = point.Value; + if (HitEffect != null) + { + var ht = HitEffect.Instantiate(); + ht.position = transform.position; + } + StartCoroutine(Kill()); + } + void OnEnable() { _active = _alive = true; @@ -170,6 +195,8 @@ public class GuidedProjectile : MonoBehaviour IEnumerator Kill() { + Binding?.Dispose(); + Binding = null; _active = false; _alive = false; Particles.Stop(true, ParticleSystemStopBehavior.StopEmitting); diff --git a/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs b/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs index 748503967..2e41570ef 100644 --- a/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs +++ b/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs @@ -9,6 +9,17 @@ using static CultMath.math; public class GuidedProjectileManager : InstantWeaponEffectManager { + // Presentation follows the committed shot (FireControl): the round retargets to the burst point the commit + // names and bursts when the shot resolves, rather than predicting either from its own homing. + private static void Bind(GuidedProjectile p, Zone zone, int shotId) + { + 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; + } + public Prototype ProjectilePrototype; public Subject<(Entity source, Transform target, GuidedProjectile missile)> OnFireGuided = new Subject<(Entity source, Transform target, GuidedProjectile missile)>(); @@ -39,7 +50,9 @@ public class GuidedProjectileManager : InstantWeaponEffectManager p.Velocity = barrel.forward * weapon.Velocity; p.Thrust = item.Evaluate(launcher.Thrust); p.TopSpeed = item.Evaluate(launcher.MissileVelocity); - OnFireGuided.OnNext((source.Entity, target.transform, p)); + Bind(p, source.Entity.Zone, shotId); + // A round with no target (a fused round fired at none) has no transform to hand a listener. + OnFireGuided.OnNext((source.Entity, target != null ? target.transform : null, p)); } else if(weapon.Data is GuidedWeaponData guidance) { @@ -59,6 +72,7 @@ public class GuidedProjectileManager : InstantWeaponEffectManager p.Thrust = item.Evaluate(guidance.Thrust); p.TopSpeed = item.Evaluate(guidance.MissileVelocity); p.TargetPosition = () => (source.Entity.Position + length(source.LookAtPoint.position.ToCultMath() - source.Entity.Position) * source.Entity.LookDirection).ToUnity(); + Bind(p, source.Entity.Zone, shotId); } else Debug.LogError($"Weapon {item.Data.Name} linked to {name} effect, but is not a Launcher!"); }