using System.Numerics;
namespace GamePort.Game;
public readonly record struct GrapplerCameraLocals(Vector3 Eye,Vector3 Look,Vector3 Gains)
{
public static GrapplerCameraLocals Default=>new(new(0,200,-350),new(0,80,0),new(1.6f,1,1.6f));
public GrapplerCameraLocals Equip()=>this with{Eye=new(0,50,-350),Look=new(0,150,80)};
public GrapplerCameraLocals Attach()=>this with{Eye=new(Eye.X,400,-650),Gains=new(.8f,1,.8f)};
public GrapplerCameraLocals Detach()=>new(new(0,50,-350),new(0,130,100),new(1.6f,1,1.6f));
}
public readonly record struct GrapplerCameraResult(Vector3 Eye,Vector3 Target,Vector3 Look,Vector3 DesiredEye,float RayGain);
/// Ordinary438BC0/439900 follow branches. Excludes flags828/851 and
/// Bubble mode2; aim is quantized from look minus OLD shared eye.
public static class GrapplerCamera
{
public static GrapplerCameraResult Follow(GrapplerCameraLocals locals,Vector3 root,Vector3 rotation,
float turnRate,int movementMode,Vector3 oldEye,float seconds,Func? ray)
{
GrapplerMotion.Time(seconds);GrapplerMotion.Finite(root);GrapplerMotion.Finite(rotation);GrapplerMotion.Finite(oldEye);
if(movementMode==2)throw new NotSupportedException("Grappler camera excludes Bubble mode2");
var eyeLocal=locals.Eye;var lookLocal=locals.Look;var eyeRotation=rotation;Vector3 gains;
if(movementMode==0)
{eyeLocal.Y-=20;lookLocal.Y=(float)(lookLocal.Y+1.9*10);gains=locals.Gains+new Vector3(0,.2f,0);}
else {eyeRotation=new(0,RocketOrientation.AddAxis(rotation.Y,turnRate),rotation.Z);gains=new(1.2f,2,1.2f);}
var desired=root+RocketOrientation.Rotate(eyeLocal,eyeRotation);
var look=root+RocketOrientation.Rotate(lookLocal,rotation);float gain=1;
if(ray?.Invoke(look,desired) is{} hit){desired=look-(look-hit.Position)*.75f;gain=1.5f;}
var eye=oldEye+(desired-oldEye)*gains*(gain*seconds);
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,gain);
}
}