using System.Numerics; namespace GamePort.Game; public readonly record struct RocketManeuver(float TurnRemaining=0,float RollRemaining=0, bool TurnLatched=false,bool RollLatched=false); public readonly record struct RocketHorizontal(Vector3 Velocity,Vector3 Rotation,float PreviousBank,RocketManeuver Maneuver); public readonly record struct RocketCamera(Vector3 Eye,Vector3 Target,Vector3 Look,Vector3 DesiredEye,int Pitch,int Yaw); /// 41A140 horizontal/auxiliary publication and41AC0E follow camera. /// Callers own body integration, facing and registry input placement. public static class RocketMotion { public static RocketHorizontal Apply(Vector3 velocity,Vector3 rotation,float speed,float bank, float previousBank,float seconds,RocketManeuver maneuver=default,bool turnPressed=false, bool rollPressed=false,bool suppressed=false,float friction=.8f) { Validate(seconds);Finite(velocity);Finite(rotation); if(!float.IsFinite(speed)||!float.IsFinite(bank)||!float.IsFinite(previousBank)||!float.IsFinite(friction) ||!float.IsFinite(maneuver.TurnRemaining)||!float.IsFinite(maneuver.RollRemaining)) throw new ArgumentOutOfRangeException(nameof(speed)); bool moving=velocity!=Vector3.Zero; float turn=maneuver.TurnRemaining,roll=maneuver.RollRemaining; bool turnLatch=maneuver.TurnLatched,rollLatch=maneuver.RollLatched; if(!suppressed) { if(!turnPressed)turnLatch=false; else if(moving&&!turnLatch){turnLatch=true;if(turn<=0)turn=1;} if(!rollPressed)rollLatch=false; else if(moving&&!rollLatch){rollLatch=true;if(roll<=0)roll=1;} } if(turn>0) { rotation=rotation with{Y=RocketOrientation.AddAxis(rotation.Y,180*seconds)}; turn=MathF.Max(0,turn-seconds); } else if(moving) { previousBank=bank; rotation=rotation with{Y=RocketOrientation.AddAxis(rotation.Y,bank*seconds)}; } if(roll>0) { roll=MathF.Max(0,roll-seconds); rotation=rotation with{Z=RocketOrientation.AddAxis(rotation.Z,270*seconds)}; } if(turn<=0) { var forward=RocketOrientation.Rotate(new(0,0,speed),rotation); float side=moving?(float)((double)previousBank*.0181818176060915f*(1-(double)friction)*speed):0; var(sin,cos)=RocketOrientation.Trig(rotation.Y); velocity=new(forward.X+cos*side,velocity.Y,forward.Z+sin*side); } return new(velocity,rotation,previousBank,new(turn,roll,turnLatch,rollLatch)); } public static RocketCamera Follow(Vector3 root,Vector3 rotation,Vector3 oldEye,float seconds, Func? ray=null,int detail=0) { Validate(seconds);Finite(root);Finite(rotation);Finite(oldEye); var desired=root+RocketOrientation.Rotate(new(0,200,-500),rotation); var look=root+RocketOrientation.Rotate(new(0,50,200),rotation); float gain=1; if(ray?.Invoke(look,desired) is{} hit){desired=look-(look-hit.Position)*.75f;gain=2;} var eye=oldEye+(desired-oldEye)*(gain*seconds*(detail<=1?.6f:1.2f)); var(pitch,yaw)=OriginalHeading.Angles(look-oldEye); var direction=RocketOrientation.Rotate(Vector3.UnitZ,new(pitch*.02197265625f,yaw*.02197265625f,0)); return new(eye,eye+direction*200,look,desired,pitch,yaw); } private static void Validate(float seconds) { if(!float.IsFinite(seconds)||seconds<=0||seconds>=1)throw new ArgumentOutOfRangeException(nameof(seconds)); } private static void Finite(Vector3 value) { if(!float.IsFinite(value.X)||!float.IsFinite(value.Y)||!float.IsFinite(value.Z))throw new ArgumentOutOfRangeException(nameof(value)); } }