Skip to content

Instantly share code, notes, and snippets.

@esnya
Last active July 23, 2021 06:28
Show Gist options
  • Save esnya/548668662f03cbe1e654779dd6388c56 to your computer and use it in GitHub Desktop.
Save esnya/548668662f03cbe1e654779dd6388c56 to your computer and use it in GitHub Desktop.
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using UdonRadioCommunication;
public class PassengerSeat : UdonSharpBehaviour
{
public EngineController EngineControl;
public GameObject LeaveButton;
public GameObject SeatAdjuster;
public Transceiver transceiver;
public UdonSharpBehaviour[] onEnterEventTargets = {};
public string[] onEnterEventNames = {};
public UdonSharpBehaviour[] onLeaveEventTargets = {};
public string[] onLeaveEventNames = {};
private LeaveVehicleButton LeaveButtonControl;
private Transform PlaneMesh;
private LayerMask Planelayer;
private void Start()
{
Assert(EngineControl != null, "Start: EngineControl != null");
Assert(LeaveButton != null, "Start: LeaveButton != null");
Assert(SeatAdjuster != null, "Start: SeatAdjuster != null");
LeaveButtonControl = LeaveButton.GetComponent<LeaveVehicleButton>();
PlaneMesh = EngineControl.PlaneMesh.transform;
Planelayer = PlaneMesh.gameObject.layer;
}
private void SendEvents(UdonSharpBehaviour[] targets, string[] names)
{
for (int i = 0; i < targets.Length; i++)
{
var target = targets[i];
if (target == null) continue;
target.SendCustomEvent(names[i]);
}
}
private void Interact()
{
EngineControl.PassengerEnterPlaneLocal();
if (LeaveButton != null) { LeaveButton.SetActive(true); }
if (SeatAdjuster != null) { SeatAdjuster.SetActive(true); }
EngineControl.localPlayer.UseAttachedStation();
}
public override void OnStationEntered(VRCPlayerApi player)
{
//voice range change to allow talking inside cockpit (after VRC patch 1008)
if (player != null)
{
LeaveButtonControl.SeatedPlayer = player.playerId;
if (player.isLocal)
{
transceiver.exclusive = false;
transceiver.Activate();
transceiver.StartTalking();
SendEvents(onEnterEventTargets, onEnterEventNames);
foreach (LeaveVehicleButton crew in EngineControl.LeaveButtons)
{
VRCPlayerApi guy = VRCPlayerApi.GetPlayerById(crew.SeatedPlayer);
if (guy != null)
{
SetVoiceInside(guy);
}
}
}
else if (EngineControl.Piloting || EngineControl.Passenger)
{
SetVoiceInside(player);
}
}
}
public override void OnStationExited(VRCPlayerApi player)
{
if (player.isLocal)
{
transceiver.StopTalking();
transceiver.Deactivate();
SendEvents(onLeaveEventTargets, onLeaveEventNames);
}
PlayerExitPlane(player);
}
public override void OnPlayerLeft(VRCPlayerApi player)
{
if (player.playerId == LeaveButtonControl.SeatedPlayer)
{
PlayerExitPlane(player);
}
}
public void PlayerExitPlane(VRCPlayerApi player)
{
LeaveButtonControl.SeatedPlayer = -1;
if (player != null)
{
SetVoiceOutside(player);
if (player.isLocal)
{
EngineControl.PassengerExitPlaneLocal();
//undo voice distances of all players inside the vehicle
foreach (LeaveVehicleButton crew in EngineControl.LeaveButtons)
{
VRCPlayerApi guy = VRCPlayerApi.GetPlayerById(crew.SeatedPlayer);
if (guy != null)
{
SetVoiceOutside(guy);
}
}
if (LeaveButton != null) { LeaveButton.SetActive(false); }
if (SeatAdjuster != null) { SeatAdjuster.SetActive(false); }
}
}
}
private void SetVoiceInside(VRCPlayerApi Player)
{
Player.SetVoiceDistanceNear(999999);
Player.SetVoiceDistanceFar(1000000);
Player.SetVoiceGain(.6f);
}
private void SetVoiceOutside(VRCPlayerApi Player)
{
Player.SetVoiceDistanceNear(0);
Player.SetVoiceDistanceFar(25);
Player.SetVoiceGain(15);
}
private void Assert(bool condition, string message)
{
if (!condition)
{
Debug.LogWarning("Assertion failed : '" + GetType() + " : " + message + "'", this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment