using System.Numerics; using GamePort.Assets; namespace GamePort.Game; /// Process globals owned by retained Jimmy; not TSK fields. public sealed class GrapplerMemory { public Vector3 Anchor; public float FirstLength,Length,ReelTarget; public float RopeScaleZ=1; public GrapplerSwing Swing; public void Clear(){Anchor=default;FirstLength=Length=ReelTarget=0;RopeScaleZ=1;Swing=default;} } public readonly record struct GrapplerMove(Vector3 Root,Vector3 Velocity); public readonly record struct GrapplerInputResult(Vector3 Root,Vector3 Rotation,Vector3 Velocity,bool NewAttachment,bool EndsActiveInput=false); /// Owned raw7 controller, bounded to VR08. Body and input phases are /// placed by LabSimulation; shared globals survive reconstructed scene loads. public sealed class GrapplerGadget : IDisposable { private readonly GrapplerRay ray; public GrapplerMemory Memory {get;} public GrapplerCameraLocals Camera {get;private set;}=GrapplerCameraLocals.Default; public bool Equipped {get;private set;} public bool Attached {get;private set;} public bool SwingPose {get;private set;} public bool CursorVisible {get;private set;} public bool LoopPlaying {get;private set;} public Vector3 CursorPosition {get;private set;} public Vector3 RopeRoot {get;private set;} public Vector3 RopeRotation {get;private set;}=new(0,0,180); public int Attachments {get;private set;} public int Detachments {get;private set;} public int Contacts {get;private set;} public event Action? EquippedChanged; public event Action? Detached; public event Action? Sound; public event Action? LoopStopped; public GrapplerGadget(Mesh world,string level,GrapplerMemory? memory=null) {ray=new(world,level);Memory=memory??new();} public void Equip() { if(Equipped)Release(); Equipped=true;Camera=Camera.Equip();EquippedChanged?.Invoke(true); } public void Release() { Detach();CursorVisible=false;Camera=GrapplerCameraLocals.Default; if(Equipped){Equipped=false;EquippedChanged?.Invoke(false);} SetLoop(false); } public void Reset() {Release();Memory.Clear();Attachments=Detachments=Contacts=0;RopeRoot=default;RopeRotation=new(0,0,180);} public void Detach() { if(!Attached)return; Attached=SwingPose=false;Detachments++; Memory.Swing=Memory.Swing with{X=Memory.Swing.X*.96f,Z=Memory.Swing.Z*.96f}; Camera=Camera.Detach();SetLoop(false);Detached?.Invoke(); } public void Contact() { if(!Attached)return; Contacts++;var response=GrapplerGeometry.Contact(Memory.Swing);Memory.Swing=response.Swing; if(response.Detach)Detach(); } public GrapplerInputResult StepInput(Vector3 root,Vector3 rotation,Vector3 velocity,PlayerInput input, float seconds,Func move) { if(!Equipped)return new(root,rotation,velocity,false); SetLoop(Attached&&input.Fire); Memory.Length=GrapplerMotion.FastReel(Memory.Length,Memory.ReelTarget,seconds); if(GrapplerMotion.JumpDetaches(input.Jump,input.Fire,Attached)) {Detach();return new(root,rotation,Vector3.Zero,false);} if(Attached) { rotation=new(0,rotation.Y,0);SwingPose=true; var constraint=GrapplerGeometry.Constrain(root,Memory.Anchor,Memory.Swing,Memory.Length,seconds); var body=move(constraint.DesiredRoot);root=body.Root;velocity=body.Velocity with{Y=0}; //The callback can detach/clear scalars, but this source tail still runs. Memory.Swing=GrapplerMotion.Swing(Memory.Swing,seconds,constraint.Offset.X,constraint.Offset.Z,Memory.Length); var(pitch,yaw)=OriginalHeading.Angles(constraint.Offset); RopeRotation=new(pitch*.02197265625f,yaw*.02197265625f,0); RopeRoot=constraint.DesiredRoot+Vector3.UnitY*116; } var aim=ray.Aim(root,rotation,input.Fire,input.GrapplerEnter,Memory.Length,seconds); Memory.Length=aim.Length;CursorVisible=aim.CursorVisible;CursorPosition=aim.CursorPosition; if(aim.EndsActiveInput||!input.Fire)return new(root,rotation,velocity,false,aim.EndsActiveInput); if(Attached) { bool reeling=Memory.Length>250; Memory.Length=GrapplerMotion.HeldReel(Memory.Length,seconds);SetLoop(reeling); return new(root,rotation,velocity,false,true); } //426A70 performs a fresh query; a cursor position is not an anchor cache. if(ray.Trace(root,rotation) is not{} hit)return new(root,rotation,velocity,false,true); var attached=GrapplerMotion.Attach(root,hit.Hit.Position,velocity,Memory.FirstLength); Memory.Anchor=hit.Hit.Position;Memory.Length=attached.Length;Memory.FirstLength=attached.InitialLength; if(attached.RopeScaleZ is{} scale)Memory.RopeScaleZ=scale; Memory.ReelTarget=attached.FastReelTarget;Memory.Swing=default; Attached=true;Attachments++;Camera=Camera.Attach(); Sound?.Invoke(31,false);SetLoop(false);SetLoop(true); return new(root,rotation,attached.Velocity,true,true); } public void Dispose()=>Release(); private void SetLoop(bool play) { if(LoopPlaying==play)return;LoopPlaying=play; if(play)Sound?.Invoke(34,true);else LoopStopped?.Invoke(); } }