using System.Numerics; using GamePort.Assets; namespace GamePort.Game; /// Jimmy's owned/reused3ROC. Bounded to the recovered VR02 static /// scene; caller places body, registry and input phases explicitly. public sealed class Rocketship { private readonly OriginalBodyPhysics physics; private readonly OriginalCameraRay cameraRay; private readonly RocketFlightTuning tuning; private readonly float accelerationLean,decelerationLean; private OrdinaryBodyState body; private bool facing; private float verticalCommand,previousBank; private Vector3 destination; public DirectorActor Actor { get; } public bool Mounted { get; private set; } public Vector3 Root=>body.Position; public Vector3 Velocity=>body.Velocity; public Vector3 Acceleration=>body.Acceleration; public Vector3 Rotation { get; private set; } public bool Supported=>body.Supported; public RocketSteering Steering { get; private set; } public RocketManeuver Maneuver { get; private set; } public Vector3 CameraEye { get; private set; } public Vector3 CameraTarget { get; private set; } public Vector3 CompanionRoot=>Root+RocketOrientation.Rotate(new(0,10,-25),Rotation); public bool FireVisible { get; private set; } public Vector4 ImpactColor { get; private set; }=Vector4.One; public bool ImpactActive { get; private set; } public float ImpactTime { get; private set; } public int Contacts { get; private set; } public int BodySteps { get; private set; } public int FrameSteps { get; private set; } public int InputSteps { get; private set; } public int EngineMode { get; private set; } public event Action? EngineSound; public event Action? EngineStopped; public event Action? Sound; public event Action? RiderHit; public Rocketship(DirectorActor actor,Mesh world,string level) { if(level!="VR02"||actor.Kind!="3ROC"||actor.SourceGam is not{} source) throw new NotSupportedException("Live Rocketship requires the authored VR02 ship"); Actor=actor; float Required(string name)=>source.Properties.TryGetValue(name,out var p)?p.Float: throw new InvalidDataException($"Missing authored Rocketship {name}"); tuning=new(Required("AccelRate"),Required("DecelRate"),Required("MaxSpeed"),3900, Required("UpRate"),Required("DownRate"),Required("MaxVertVelocity")); accelerationLean=Required("AccelLean");decelerationLean=Required("DecelLean"); if(world.CollisionPolygons.Any(p=>p.MaterialId is{} id&&id is not(0 or 2 or 3 or 5))) throw new NotSupportedException("Rocketship VR02 contact materials require their recovered profile"); physics=new(OriginalStaticCollision.FromRouteScene(world,level)); cameraRay=OriginalCameraRay.FromRouteScene(world,level); Reset(); } public void Reset() { if(EngineMode!=0)SetEngine(0); Mounted=FireVisible=facing=ImpactActive=false; body=new(Actor.SourceGam!.Position,Vector3.Zero,Vector3.Zero,Vector3.Zero); Rotation=Actor.SourceGam.Rotation;destination=Rotation; Steering=default;Maneuver=default;verticalCommand=previousBank=ImpactTime=0; ImpactColor=Vector4.One;Contacts=BodySteps=FrameSteps=InputSteps=0; Actor.BodyEnabled=Actor.Visible=false; } public void Mount(Vector3 root,Vector3 rotation,Vector3 sharedEye) { //42944D copies transforms and flags; it does not reset retained motion. body=body with{Position=root};Rotation=rotation;CameraEye=sharedEye; CameraTarget=sharedEye+Vector3.UnitZ*200; Mounted=true;Actor.BodyEnabled=Actor.Visible=true;SyncActor(); } public void Release() { //428541 disables integration and explicitly stops43EA80 before hiding. Mounted=FireVisible=false;Actor.BodyEnabled=Actor.Visible=false;SetEngine(0); } public void StepBody(float seconds) { if(!Mounted)return; var step=physics.Step(body,seconds,50,.6f,profile:BodyResponseProfile.Rocketship); body=step.Body;BodySteps++; //43E8D0 ordinary VR02 material path; null material has no side effects. if(step.Contact is{} wall&&wall.StoredNormal.Y>-.7f&&wall.MaterialId.HasValue&&!ImpactActive) { ImpactActive=true;ImpactTime=0;Contacts++; RiderHit?.Invoke();Sound?.Invoke(197); } } public void StepFrame(PlayerInput input,float seconds) { if(facing)Rotation=RocketOrientation.Face(Rotation,destination,seconds,Maneuver.RollRemaining>0); var motion=RocketMotion.Apply(body.Velocity,Rotation,Steering.Speed,Steering.Bank,previousBank,seconds, Maneuver,input.RocketTurn,input.RocketRoll); Rotation=motion.Rotation;previousBank=motion.PreviousBank;Maneuver=motion.Maneuver; var vertical=RocketFlight.StepVertical(new(Root.Y,motion.Velocity.Y,verticalCommand),Steering,tuning,RecoveredMotion.Gravity.Y); verticalCommand=vertical.State.CommandedAcceleration; body=body with{Position=Root with{Y=vertical.State.Height},Velocity=motion.Velocity with{Y=vertical.State.Velocity}, Acceleration=body.Acceleration with{Y=vertical.BodyAcceleration}}; FrameSteps++; if(!Mounted){Steering=Steering with{Speed=0};return;} FireVisible=!Steering.Down&&Steering.Speed>0; if(ImpactActive) { double time=(double)ImpactTime+seconds; ImpactTime=(float)time; //43E78B..43E7A7 writes material+4C..58 RGBA, not model scale. if(time>1){ImpactActive=false;ImpactColor=Vector4.One;} else {float pulse=(float)((Math.Sin(time*20)+1)*.5);ImpactColor=new(1,pulse,pulse,1);} } SyncActor(); } public void StepInput(PlayerInput input,float seconds) { if(!Mounted)return; Steering=RocketFlight.StepKeyboardInput(Steering,input with{Forward=input.Forward||input.RocketMouseRise},Supported,seconds,tuning); destination=new(Steering.Up?accelerationLean:Steering.Down?decelerationLean:0,Rotation.Y, Steering.Up||Steering.Down||Steering.Speed>0?-Steering.Bank:0); facing=true; SetEngine(Steering.Up?2:Steering.Down||Steering.Speed<=0?0:1); var camera=RocketMotion.Follow(Root,Rotation,CameraEye,seconds,(a,b)=>cameraRay.Trace(a,b)); CameraEye=camera.Eye;CameraTarget=camera.Target;InputSteps++; } private void SetEngine(int mode) { if(EngineMode==mode)return; if(EngineMode!=0)EngineStopped?.Invoke(); EngineMode=mode; if(mode!=0)EngineSound?.Invoke(mode==1?195:196); } private void SyncActor(){Actor.Position=Root;Actor.Rotation=Rotation;Actor.Speed=Steering.Speed;} }