180 lines
8.5 KiB
C#
180 lines
8.5 KiB
C#
using System.Numerics;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>Connect recovered raw-owner preparations and authored transforms to
|
|
/// the port's player body. Full scene scheduling/other actor classes stay separate.</summary>
|
|
public sealed class PlayerSceneBinding : IDisposable
|
|
{
|
|
private readonly SceneDirector director;
|
|
private readonly LabSimulation player;
|
|
private readonly SceneInventory inventory;
|
|
private readonly string tag;
|
|
public Scooter? Scooter {get;}
|
|
public Rocketship? Rocketship { get; }
|
|
public bool InventoryControlled=>director.InventoryEnabled&&(director.Controlled||
|
|
Rocketship is{Mounted:true} rocket&&director.ControlledTag==rocket.Actor.Tag||
|
|
Scooter is{Mounted:true} scooter&&director.ControlledTag==scooter.Actor.Tag);
|
|
public int UnsupportedGadgetCleanups { get; private set; }
|
|
|
|
public PlayerSceneBinding(SceneDirector director, LabSimulation player, SceneInventory inventory, string tag,
|
|
Rocketship? rocketship=null,Scooter? scooter=null)
|
|
{
|
|
this.director = director; this.player = player; this.inventory = inventory; this.tag = tag;
|
|
Rocketship=rocketship;Scooter=scooter;
|
|
if(rocketship!=null)rocketship.RiderHit+=player.RocketImpact;
|
|
director.ActorPreparing += Prepare;
|
|
director.AuthoredTransformChanged += Transform;
|
|
director.GadgetReleaseRequested += Release;
|
|
inventory.GadgetSelected += Selected;
|
|
player.Invisible.EquippedChanged += InvisibleEquipped;
|
|
player.Baseball.EquippedChanged += BaseballEquipped;
|
|
if(player.Grappler!=null)player.Grappler.EquippedChanged+=InvisibleEquipped;
|
|
player.DefaultGadgetRequested += DefaultGadget;
|
|
Selected(inventory.SelectedGadget);
|
|
}
|
|
|
|
public bool Step(PlayerInput input)
|
|
{
|
|
if(player.Noclip&&(Rocketship?.Mounted==true||Scooter?.Spawned==true))inventory.SelectGadget(-1);
|
|
bool VehicleControl()=>Rocketship is{Mounted:true} r&&(director.AuthoredControlledTag==r.Actor.Tag||director.DebugForceControl);
|
|
bool ScooterControl()=>Scooter is{Mounted:true}s&&(director.AuthoredControlledTag==s.Actor.Tag||director.DebugForceControl);
|
|
var acceptsInput = director.Controlled || player.Noclip || VehicleControl()||ScooterControl();
|
|
Scooter?.StepBody(FixedStepClock.Step,input);
|
|
Rocketship?.StepBody(FixedStepClock.Step);
|
|
bool JimmyControl()=>(director.Controlled&&Rocketship?.Mounted!=true&&Scooter?.Mounted!=true)||player.Noclip;
|
|
player.Step(input,JimmyControl(),director.ControlledTag!=null,()=>
|
|
{
|
|
if(Scooter is{Spawned:true,Mounted:false}s&&!player.Noclip&&s.TryMount(player.ActorRoot,player.InteractionRadius))
|
|
{player.EnterScooter();director.TransferControl(s.Actor);}
|
|
if(!player.InputAfterPlayerUpdate)
|
|
{
|
|
if(VehicleControl())Rocketship!.StepInput(input,FixedStepClock.Step);
|
|
if(ScooterControl())Scooter!.StepInput(input,FixedStepClock.Step);
|
|
}
|
|
},JimmyControl);
|
|
Scooter?.StepFrame(input,FixedStepClock.Step);
|
|
if(Scooter is{Mounted:true}scooter)
|
|
{
|
|
player.ApplyScooterTransform(scooter.Root,scooter.Rotation);
|
|
director.TransferControl(scooter.Actor);
|
|
if(player.InputAfterPlayerUpdate&&ScooterControl())scooter.StepInput(input,FixedStepClock.Step);
|
|
}
|
|
Scooter?.StepWheels();
|
|
Rocketship?.StepFrame(input,FixedStepClock.Step);
|
|
if(Rocketship is{Mounted:true} rocket)
|
|
{
|
|
//43DF70 copies passenger transforms and reasserts ship ownership.
|
|
player.ApplyAuthoredTransform(rocket.Root,rocket.Rotation);
|
|
director.TransferControl(rocket.Actor);
|
|
if(player.InputAfterPlayerUpdate&&VehicleControl())rocket.StepInput(input,FixedStepClock.Step);
|
|
}
|
|
if (player.GadgetMode==0 && player.Grounded && director.FindActor(tag) is{} actor) actor.Animation="STOP";
|
|
director.SyncActor(tag,player.ActorRoot,player.Rotation);
|
|
return acceptsInput;
|
|
}
|
|
|
|
private void Prepare(DirectorActor actor, ScriptPreparation action)
|
|
{
|
|
if (!string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase)) return;
|
|
if (action == ScriptPreparation.StopMotion) player.PrepareForScript();
|
|
else if (action == ScriptPreparation.FlattenOrientation) player.FlattenOrientation();
|
|
else if (action == ScriptPreparation.ReleaseGadget)
|
|
Release(actor);
|
|
}
|
|
|
|
private void Release(DirectorActor actor)
|
|
{
|
|
if (!string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase)) return;
|
|
//427FF0 switches raw mode, not display selection. A rejected Invisible
|
|
//equip can leave display2 with raw mode-1; cleanup does not equip it.
|
|
if (player.GadgetMode is 0 or 1 or 2 or 4 or 6 or 7 || inventory.SelectedGadget is -1 or 3)
|
|
{ inventory.SelectGadget(-1); player.GadgetMode = -1; }
|
|
else if (player.GadgetMode != -1) UnsupportedGadgetCleanups++;
|
|
}
|
|
|
|
private void Selected(int item)
|
|
{
|
|
if(Scooter is{Spawned:true}scooter)
|
|
{
|
|
bool mounted=scooter.Mounted;
|
|
if(director.FindActor("C3DGODDARD") is{} dog)
|
|
{dog.Position=scooter.Root;dog.Rotation=scooter.Rotation;dog.Visible=dog.BodyEnabled=dog.MovementEnabled=true;}
|
|
scooter.Release();
|
|
if(mounted)
|
|
{
|
|
player.LeaveScooter(scooter.Root,scooter.Rotation,scooter.CameraEye);
|
|
if(director.FindActor(tag) is{} rider){rider.Animation="RUN";director.TransferControl(rider);}
|
|
}
|
|
}
|
|
if(Rocketship?.Mounted==true)
|
|
{
|
|
Rocketship.Release();
|
|
player.LeaveRocketship(Rocketship.CameraEye);
|
|
if(director.FindActor(tag) is{} rider){rider.Animation="FALL";director.TransferControl(rider);}
|
|
}
|
|
//The display tile remains available for Practice outside the bounded
|
|
//live scene; unsupported selection must not invent a Jimmy flight mode.
|
|
player.GadgetMode=item==1&&Rocketship==null||item==4&&Scooter==null?-1:GadgetScreen.EquippedMode(item);
|
|
if(item==4&&Scooter!=null)
|
|
{
|
|
if(director.FindActor("C3DGODDARD") is{} dog&&dog.BodyEnabled)
|
|
{
|
|
Scooter.Summon(dog.Position,dog.Rotation,player.CameraEye);
|
|
dog.Visible=dog.BodyEnabled=dog.MovementEnabled=false;
|
|
}
|
|
else player.GadgetMode=-1;
|
|
}
|
|
if(item==1&&Rocketship!=null)
|
|
{
|
|
var rider=director.FindActor(tag)!;
|
|
Rocketship.Mount(player.ActorRoot,rider.Rotation with{Y=player.Yaw},player.CameraEye);
|
|
rider.Animation="DRIVE";
|
|
director.TransferControl(Rocketship.Actor);
|
|
}
|
|
}
|
|
|
|
public void ResetVehicle()
|
|
{
|
|
if(Rocketship?.Mounted==true||Scooter?.Spawned==true)inventory.SelectGadget(-1);
|
|
Rocketship?.Reset();
|
|
Scooter?.Reset();
|
|
}
|
|
private void DefaultGadget() => inventory.SelectGadget(-1);
|
|
|
|
private void InvisibleEquipped(bool equipped)
|
|
{
|
|
if (director.FindActor("C3DGODDARD") is not { } companion) return;
|
|
//427FF0 requires Goddard complete9E8==false before showing/enabling.
|
|
//These six PostLoad levels set that flag (retained-companion.md).
|
|
if (!equipped && player.LevelCode is "LEV6" or "LEV7" or "LV1C" or "LV1D" or "LV4A" or "LV6A") return;
|
|
companion.Visible = companion.MovementEnabled = companion.BodyEnabled = !equipped;
|
|
}
|
|
|
|
private void BaseballEquipped(bool equipped)
|
|
{
|
|
//Raw6 cleanup42871D has no Invisible's complete9E8 restoration veto.
|
|
if(director.FindActor("C3DGODDARD") is {} companion)
|
|
companion.Visible=companion.MovementEnabled=companion.BodyEnabled=!equipped;
|
|
}
|
|
|
|
private void Transform(DirectorActor actor)
|
|
{
|
|
if (string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase))
|
|
player.ApplyAuthoredTransform(actor.Position, actor.Rotation);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
director.ActorPreparing -= Prepare;
|
|
director.AuthoredTransformChanged -= Transform;
|
|
director.GadgetReleaseRequested -= Release;
|
|
inventory.GadgetSelected -= Selected;
|
|
player.Invisible.EquippedChanged -= InvisibleEquipped;
|
|
player.Baseball.EquippedChanged -= BaseballEquipped;
|
|
if(player.Grappler!=null)player.Grappler.EquippedChanged-=InvisibleEquipped;
|
|
player.DefaultGadgetRequested -= DefaultGadget;
|
|
if(Rocketship!=null)Rocketship.RiderHit-=player.RocketImpact;
|
|
}
|
|
}
|