diff --git a/Assets/Scripts/Editor/CapabilityPresentationSmoke.cs b/Assets/Scripts/Editor/CapabilityPresentationSmoke.cs index 0e40cb753..75d97c63f 100644 --- a/Assets/Scripts/Editor/CapabilityPresentationSmoke.cs +++ b/Assets/Scripts/Editor/CapabilityPresentationSmoke.cs @@ -81,21 +81,28 @@ public static class CapabilityPresentationSmoke typeof(ActionGameManager).GetProperty("Instance", Any).SetValue(null, manager); manager.CurrentEntity = ship; - var instanceObject = new GameObject("Smoke Entity Instance"); - var instance = instanceObject.AddComponent(); - Awaken(instance); - instance.HullColliders = new HullCollider[0]; - instance.RadiatorHardpoints = new RadiatorHardpoint[0]; - instance.ThrusterHardpoints = new ThrusterHardpoint[0]; - instance.WeaponHardpoints = new WeaponHardpoint[0]; - instance.ArticulationPoints = new ArticulationPoint[0]; - // The beam is a child before SetEntity on both ship paths, so the binder finds it as the game does. var beamPrefab = AssetDatabase.LoadAssetAtPath("Assets/Content/Prefabs/Tractor Beam.prefab") ?? throw new InvalidOperationException("No Tractor Beam prefab."); - var beam = ((GameObject)PrefabUtility.InstantiatePrefab(beamPrefab, instanceObject.transform)).GetComponent(); - beam.Power = .8f; - beam.Direction = Vector3.forward; - instance.SetEntity(null, ship); + // An entity's instance as the game builds it: the beam is a child before SetEntity on both ship paths, so the + // binder finds it as the game does. + (GameObject Object, EntityInstance Instance, TractorBeam Beam) Bound(string name, Entity entity) + { + var go = new GameObject(name); + var bound = go.AddComponent(); + Awaken(bound); + bound.HullColliders = new HullCollider[0]; + bound.RadiatorHardpoints = new RadiatorHardpoint[0]; + bound.ThrusterHardpoints = new ThrusterHardpoint[0]; + bound.WeaponHardpoints = new WeaponHardpoint[0]; + bound.ArticulationPoints = new ArticulationPoint[0]; + var tractor = ((GameObject)PrefabUtility.InstantiatePrefab(beamPrefab, go.transform)).GetComponent(); + tractor.Power = .8f; + tractor.Direction = Vector3.forward; + bound.SetEntity(null, entity); + return (go, bound, tractor); + } + var (instanceObject, instance, beam) = Bound("Smoke Entity Instance", ship); + var (otherObject, _, otherBeam) = Bound("Smoke Other Instance", other); int Effects() => instance.GetComponentsInChildren(true).Count(p => p != null && !p.isOriginalPrototype); void Expect(string what, int effects, int kicks) @@ -123,9 +130,15 @@ public static class CapabilityPresentationSmoke // An untracked ship is not drawn at (ActionGameManager.DrawsAt, since contact-hud): no hull effect, no impulse. Expect("hull hit on an untracked ship", 2, 1); + // Once the player's instruments track it, the same hit is drawn. + other.VisibleEntities.Add(ship); + ship.Capabilities.PublishAbsorb(Hit(AbsorbLayer.Hull)); + Expect("hull hit on a tracked ship", 3, 1); + other.VisibleEntities.Remove(ship); + manager.CurrentEntity = ship; ship.Capabilities.PublishAbsorb(Hit(AbsorbLayer.Shield)); - Expect("player's shield hit", 2, 2); + Expect("player's shield hit", 3, 2); // Grabs: the beam presents the simulation's events and nothing else. float Emission() => beam.ParticleSystem.emission.rateOverTimeMultiplier; @@ -156,12 +169,32 @@ public static class CapabilityPresentationSmoke ship.Capabilities.PublishGrab(Grab(GrabPhase.Cancelled, float3(0f, 0f, 0f))); ExpectBeam("grab cancelled", 0f, false); - // The beam belongs to the ship's entity: another entity's grab is no business of its own. + // Aim is on the ground plane: a beam above the held point does not pitch toward it. + var beamHome = beam.transform.position; + beam.transform.position = beamHome + new Vector3(0f, 10f, 0f); + ship.Capabilities.PublishGrab(Grab(GrabPhase.Started, float3(20f, 7f, 0f))); + ExpectBeam("grab from above", .8f, true); + ship.Capabilities.PublishGrab(Grab(GrabPhase.Completed, float3(20f, 7f, 0f))); + beam.transform.position = beamHome; + + // A grab held at the beam's own position has no direction to take: the beam keeps the aim it had. + beam.Direction = Vector3.right; + var atBeam = float3(beam.transform.position.x, 0f, beam.transform.position.z); + ship.Capabilities.PublishGrab(Grab(GrabPhase.Started, atBeam)); + ExpectBeam("grab at the beam", .8f, true); + ship.Capabilities.PublishGrab(Grab(GrabPhase.Completed, atBeam)); + beam.Direction = Vector3.forward; + ExpectBeam("after the grab at the beam", 0f, false); + + // The beam belongs to the ship's entity: another entity's grab, with its own beam bound, moves only that beam. other.Capabilities.PublishGrab(Grab(GrabPhase.Started, float3(20f, 0f, 0f))); ExpectBeam("another entity's grab", 0f, false); + otherBeam.Update(); + if (abs(otherBeam.ParticleSystem.emission.rateOverTimeMultiplier - .8f) > 1e-4f) + throw new InvalidOperationException("The other entity's own beam did not present its grab."); - Debug.Log("CAPABILITY_PRESENTATION_SMOKE ok effects=2 impulses=2 grabs=ok"); - return new[] { feedbackObject, effectObject, managerObject, instanceObject }; + Debug.Log("CAPABILITY_PRESENTATION_SMOKE ok effects=3 impulses=2 grabs=ok"); + return new[] { feedbackObject, effectObject, managerObject, instanceObject, otherObject }; } private static void Clean(GameObject[] objects, string work) diff --git a/Assets/Scripts/Editor/FloatingItemsDrawnSmoke.cs b/Assets/Scripts/Editor/FloatingItemsDrawnSmoke.cs index f67c0db10..61f7caae2 100644 --- a/Assets/Scripts/Editor/FloatingItemsDrawnSmoke.cs +++ b/Assets/Scripts/Editor/FloatingItemsDrawnSmoke.cs @@ -180,6 +180,32 @@ public static class FloatingItemsDrawnSmoke At(drawn, released, "step"); Require(drawn.transform.position != before, "the pickup did not move with the body."); + // A held item keeps its label lit, so a pull is read as it happens. The label is let go dark first (nothing views + // it), then the zone holds the item through its own TryHold and the label must come up. + var earlyPickup = Pickups().Single(p => p.Item == early.Item); + earlyPickup.LabelPersistDuration = 1; + earlyPickup.LabelFadeDuration = .2f; + for (var i = 0; i < 1200 && earlyPickup.ScanLabel.color.a > .05f; i++) yield return null; + Require(earlyPickup.ScanLabel.color.a <= .05f, "label: an unviewed, unheld label did not go dark."); + Require(zone.TryHold(taker, early.Id), "label: the zone refused to hold the item."); + for (var i = 0; i < 1200 && earlyPickup.ScanLabel.color.a < .95f; i++) yield return null; + Require(earlyPickup.ScanLabel.color.a >= .95f, "label: a held item's label is not lit."); + + // The test scene's stand-in sim and tendril ride the zone's own held-pose curve: its pose at each point of a grab + // is where the zone puts a held item at the same progress. + var (extend, envelop, pull) = (1.5f, .75f, 1.5f); + var holderAt = new CultMath.float3(taker.Position.x, 0f, taker.Position.z); + var claimedAt = new CultMath.float3(early.HeldFrom.x, 0f, early.HeldFrom.y); + foreach (var elapsed in new[] { 0f, extend + envelop - .01f, extend + envelop + .3f, extend + envelop + .75f, extend + envelop + pull }) + { + zone.SetHeldProgress(early.Id, FieldTester.PullProgress(elapsed, extend, envelop, pull)); + zone.Update(.01f); + var want = FieldTester.PulledPosition(claimedAt, holderAt, elapsed, extend, envelop, pull); + Require(Mathf.Abs(early.Body.Position.x - want.x) < 1e-3f && Mathf.Abs(early.Body.Position.y - want.z) < 1e-3f, + $"stand-in pose at {elapsed}s is {want}, the zone holds the item at {early.Body.Position}."); + } + zone.Free(early.Id); + // Pickup destroys exactly the picked-up item's pickup. Require(zone.TryPickUp(taker, released.Id), "the station refused a one-cell stack."); yield return null; diff --git a/Assets/Scripts/Gameplay/FieldDriver.cs b/Assets/Scripts/Gameplay/FieldDriver.cs index 93a62fa6f..1c5d0a352 100644 --- a/Assets/Scripts/Gameplay/FieldDriver.cs +++ b/Assets/Scripts/Gameplay/FieldDriver.cs @@ -294,8 +294,8 @@ public class FieldDriver : MonoBehaviour, IAbsorbPresenter, IGrabPresenter, IMel _field.SetFloat("_TendrilRadius", TendrilTipScale * TendrilTipRadius); break; case GrabPhase.Pull: - // The same curve as Zone's held pose (Zone.cs): the tip rides the item the simulation pulls in. - _tendrilTargetPos = lerp(_grabStart.ToCultMath(), transform.position.ToCultMath(), smoothstep(0, 1, _grabTime)).ToUnity(); + // The curve of Zone's held pose (Zone.HeldPull): the tip rides the item the simulation pulls in. + _tendrilTargetPos = lerp(_grabStart.ToCultMath(), transform.position.ToCultMath(), Zone.HeldPull(_grabTime)).ToUnity(); _tendrilBendTarget = _grabStart; if (_fadePoint > .99 && transform.InverseTransformPoint(_tendrilTargetPos).sqrMagnitude < 1) _fadePoint = _grabTime; diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 29262c69c..e7d17bd54 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -394,6 +394,10 @@ public class Zone // After StepClouds and before FireControl.Step: each floating item drifts as a KinematicBody on the zone's force // and is removed once its lifetime has run out. A held item is neither integrated nor expired: it is placed // between where it was claimed and its holder, now that the entities have moved. + // The curve a held item rides from where it was claimed to its holder: the one owner, which the test scene's + // stand-in sim and tendril call rather than spell again. + public static float HeldPull(float progress) => smoothstep(0f, 1f, progress); + private void StepFloatingItems(float deltaTime) { var settings = _itemManager.GameplaySettings; @@ -401,7 +405,7 @@ public class Zone { if (floating.Holder != null) { - floating.Body.Position = lerp(floating.HeldFrom, floating.Holder.Position.xz, smoothstep(0f, 1f, floating.HeldProgress)); + floating.Body.Position = lerp(floating.HeldFrom, floating.Holder.Position.xz, HeldPull(floating.HeldProgress)); continue; } floating.Body.Step(deltaTime, GetForce(floating.Body.Position), settings); diff --git a/Assets/Scripts/UI/FieldTester.cs b/Assets/Scripts/UI/FieldTester.cs index d8b8b96bb..493b0b60e 100644 --- a/Assets/Scripts/UI/FieldTester.cs +++ b/Assets/Scripts/UI/FieldTester.cs @@ -148,22 +148,27 @@ public class FieldTester : MonoBehaviour _capabilityEvents.PublishGrab(new GrabEvent(id, GrabPhase.Pull, _grabStart, GrabPullTime)); } var pickup = _pickups[_grabIndex].transform; - if (_pullPublished) + pickup.position = PulledPosition(_grabStart, TestField.transform.position.ToCultMath(), _grabElapsed, + GrabExtendTime, GrabEnvelopTime, GrabPullTime).ToUnity(); + if (_pullPublished && PullProgress(_grabElapsed, GrabExtendTime, GrabEnvelopTime, GrabPullTime) >= 1) { - var progress = saturate((_grabElapsed - pullStart) / max(GrabPullTime, 1e-4f)); - pickup.position = lerp(_grabStart, TestField.transform.position.ToCultMath(), smoothstep(0, 1, progress)).ToUnity(); - if (progress >= 1) - { - _capabilityEvents.PublishGrab(new GrabEvent(id, GrabPhase.Completed, pickup.position.ToCultMath(), 0)); - Destroy(pickup.gameObject); - var p = _pickups[_grabIndex]; - p.transform = null; - _pickups[_grabIndex] = p; - _grabbing = false; - } + _capabilityEvents.PublishGrab(new GrabEvent(id, GrabPhase.Completed, pickup.position.ToCultMath(), 0)); + Destroy(pickup.gameObject); + var p = _pickups[_grabIndex]; + p.transform = null; + _pickups[_grabIndex] = p; + _grabbing = false; } } + // How far along the pull a grab is `elapsed` seconds in: 0 through Extend and Envelop, then 0..1 over the pull. + public static float PullProgress(float elapsed, float extend, float envelop, float pull) => + saturate((elapsed - extend - envelop) / max(pull, 1e-4f)); + + // Where the stand-in sim holds its pickup: still through Extend and Envelop, then along Zone's held-pose curve. + public static float3 PulledPosition(float3 start, float3 holder, float elapsed, float extend, float envelop, float pull) => + lerp(start, holder, Zone.HeldPull(PullProgress(elapsed, extend, envelop, pull))); + private void Update() { var move = _forceThrust ? float2(0,1) : _input.Player.Move.ReadValue().ToCultMath(); diff --git a/tests/Aetheria.Shared.Tests/PickupTests.cs b/tests/Aetheria.Shared.Tests/PickupTests.cs index d3c9f92e0..d88ed5e53 100644 --- a/tests/Aetheria.Shared.Tests/PickupTests.cs +++ b/tests/Aetheria.Shared.Tests/PickupTests.cs @@ -403,15 +403,18 @@ public sealed class PickupTests : IDisposable Assert.True(block.Holder == null, "block is held"); } - [Fact] - public void TheBeamTakesFromWhicheverBayCanStore() + // Either bay may be the full one: a selection that read only the first bay, or only the last, fails one orientation. + [Theory] + [InlineData(0)] + [InlineData(1)] + public void TheBeamTakesFromWhicheverBayCanStore(int fullBay) { using var bag = new CompositeDisposable(); var lab = Build(Beam()); var ship = Spawn(lab, float3(0, 0, 0), bays: 2); Assert.Equal(2, ship.CargoBays.Count); - // The first bay is full; only the second can take the ore. - for (var i = 0; i < 16; i++) Assert.True(ship.CargoBays[0].TryStore(lab.Make("Spare"))); + var freeBay = 1 - fullBay; + for (var i = 0; i < 16; i++) Assert.True(ship.CargoBays[fullBay].TryStore(lab.Make("Spare"))); var ore = Drift(lab, 40, 0); var events = Watch(ship, bag); ship.TractorCommand = 1; @@ -419,8 +422,8 @@ public sealed class PickupTests : IDisposable Assert.Contains(events, e => e.Phase == GrabPhase.Started && e.Target.Equals(ore.Id)); Assert.Contains(events, e => e.Phase == GrabPhase.Completed); Assert.False(lab.Zone.FloatingItems.Contains(ore), "ore is still in the zone"); - Assert.Equal(16, ship.CargoBays[0].Cargo.Count); - Assert.Single(ship.CargoBays[1].Cargo); + Assert.Equal(16, ship.CargoBays[fullBay].Cargo.Count); + Assert.Single(ship.CargoBays[freeBay].Cargo); } [Fact]