using System.Numerics; using GamePort.Assets; namespace GamePort.Game; /// Jimmy's owned C3DJEEP, summoned at Goddard and mounted by contact. /// Ordinary source vehicle/wheel profile is admitted in VR05 and outsideLEV1. public sealed class Scooter : IDisposable { private readonly OriginalStaticCollision world; private readonly OriginalBodyPhysics physics; private readonly Dictionary<(int,int),Vector3> normals; private readonly ScooterWheels wheels=new(); private OrdinaryBodyState body; private Vector3 destination; private float poseClock,faceRate=5,impactTime; private bool jumpLatch; private Vector3? supportNormal; public DirectorActor Actor{get;} public bool Spawned{get;private set;} public bool Mounted{get;private set;} public bool ImpactActive{get;private set;} public bool RiderStopped{get;private set;} public bool EnginePlaying{get;private set;} public Vector3 Root=>body.Position; public Vector3 Rotation{get;private set;} public Vector3 RiderRotation{get;private set;} public Vector3 Velocity=>body.Velocity; public Vector3 PendingImpulse=>body.Impulse; public bool Supported=>body.Supported; public ScooterSteering Steering{get;private set;} public IReadOnlyList WheelRoots=>wheels.Roots; public IReadOnlyList WheelRotations=>wheels.Rotations; public int FrontSupports=>wheels.Front; public int RearSupports=>wheels.Rear; public int BodySteps{get;private set;} public int InputSteps{get;private set;} public int FrameSteps{get;private set;} public int Contacts{get;private set;} public int Jumps{get;private set;} public Vector3 CameraEye{get;private set;} public Vector3 CameraTarget{get;private set;} public Func? DynamicRootResolver{get;set;} public event Action? EngineStarted; public event Action? EngineStopped; public event Action? EngineSpeedChanged; public event Action? MountedChanged; public Scooter(DirectorActor actor,Mesh mesh,string level) { if(level is not("VR05" or "LEV1"))throw new NotSupportedException("Scooter body profile covers VR05 and outsideLEV1"); Actor=actor;world=OriginalStaticCollision.FromRouteScene(mesh,level); //421910 ordinary callback is independent of Jimmy's special materials. physics=new(world,allowSpecialMaterials:true); normals=mesh.CollisionPolygons.ToDictionary(p=>(p.ShapeId,p.PolygonIndex),p=>p.Normal); Reset(); } public void Reset() { Release();body=new();Steering=default;RiderRotation=Rotation=destination=Vector3.Zero; faceRate=5;poseClock=impactTime=0;ImpactActive=jumpLatch=RiderStopped=false;wheels.Reset(); BodySteps=InputSteps=FrameSteps=Contacts=Jumps=0; } public void Summon(Vector3 root,Vector3 rotation,Vector3 eye) { body=body with{Position=root};RiderRotation=Rotation=destination=rotation;CameraEye=eye;CameraTarget=eye+Vector3.UnitZ*200; Spawned=true;Actor.Visible=Actor.BodyEnabled=Actor.MovementEnabled=true;Sync(); } public bool TryMount(Vector3 jimmyRoot,float radius) { if(!Spawned||Mounted||OriginalBodySupport.QuickMagnitude(Root-jimmyRoot)>=150+radius)return false; Mounted=true;RiderStopped=false;poseClock=0;MountedChanged?.Invoke();return true; } public void Release() { Spawned=Mounted=false;Actor.Visible=Actor.BodyEnabled=Actor.MovementEnabled=false; if(EnginePlaying){EnginePlaying=false;EngineStopped?.Invoke();} } public void StepBody(float dt,PlayerInput input) { if(!Spawned)return; var result=physics.Step(body,dt,150,.2f,friction:.9f,suppressionScale:1,wallContact:(current,wall)=> {body=current;Contact(wall.StoredNormal,input,dt);return body with{PublishedSpeed=Steering.Speed};}); body=result.Body;BodySteps++; if(DynamicRootResolver!=null)body=body with{Position=DynamicRootResolver(result.PreviousPosition,Root,150)}; if(result.SupportHit is{} hit&&normals.TryGetValue((hit.ShapeId,hit.PolygonId),out var normal)) supportNormal=normal; Sync(); } public void StepInput(PlayerInput input,float dt) { if(!Mounted)return;InputSteps++; var next=ScooterMotion.Input(Steering,input,RearSupports,dt); Steering=ImpactActive?Steering with{Angle=next.Angle,KeyboardTurn=next.KeyboardTurn}:next; var camera=ScooterCamera.Follow(Root,Rotation,CameraEye,dt,(start,end)=> { var context=physics.LastStaticContext??new(64.9519043f); context.Nearest=context.Nearest with{FaceDistance=130000}; //413870 pushes false: camera reclip tests faces, not wheel/body edges. return world.Move(start,end-start,64.9519043f,context,edges:false).Position; },impact:ImpactActive); CameraEye=camera.Eye;CameraTarget=camera.Target; } public void StepFrame(PlayerInput input,float dt) { if(!Spawned)return; Rotation=ScooterWheels.Face(Rotation,destination,faceRate,dt); if(ImpactActive) { var impact=ScooterMotion.Impact(Velocity,body.AngularVelocity,impactTime,dt); impactTime=impact.Time;ImpactActive=impact.Active; body=body with{Velocity=impact.Velocity,AngularVelocity=impact.AngularVelocity}; if(!ImpactActive)Steering=Steering with{Speed=0}; } else { var drive=ScooterMotion.Apply(Steering,Velocity,Rotation,Supported,FrontSupports,RearSupports,dt,.9f); Steering=drive.Steering;Rotation=drive.Rotation;body=body with{Velocity=drive.Velocity}; } FrameSteps++; if(Mounted) { if(!input.Jump)jumpLatch=false; else if(!jumpLatch&&Supported){body=body with{Impulse=new(0,700,0)};jumpLatch=true;Jumps++;} poseClock+=dt; if((double)poseClock>.3){RiderStopped=Steering.Speed<10;poseClock=0;} } else {Steering=Steering with{Speed=0};faceRate=10;} body=body with{PublishedSpeed=Steering.Speed}; //Jimmy copies the parent's Euler before the later wheel/lean phase. RiderRotation=Rotation; if(!EnginePlaying){EnginePlaying=true;EngineStarted?.Invoke();} else EngineSpeedChanged?.Invoke(Steering.Speed); Sync(); } public void StepWheels() { if(!Spawned)return; Rotation=Rotation with{Z=Steering.Angle*-.5f}; //Counts used by465220 are from the preceding wheel pass. var oldFront=FrontSupports;var oldRear=RearSupports; wheels.Update(Root,Rotation,Steering.Angle,Steering.Speed,Steering.Braking,ImpactActive,world); if(oldFront+oldRear==0){destination=new(340,Rotation.Y,0);faceRate=2;} else if(supportNormal is{} normal)destination=ScooterWheels.TerrainTarget(normal,Rotation.Y); Sync(); } private void Contact(Vector3 normal,PlayerInput input,float dt) { Contacts++; var result=ScooterMotion.Contact(Steering.Speed,Velocity,Rotation,normal,body.Acceleration,ImpactActive,input.Forward||input.Fire,dt); if(!ImpactActive&&result.Impact)impactTime=0; ImpactActive=result.Impact;Steering=Steering with{Speed=result.Speed};body=body with{Acceleration=result.Acceleration}; } private void Sync(){Actor.Position=Root;Actor.Rotation=Rotation;Actor.Speed=Steering.Speed;} public void Dispose()=>Release(); }