Skip to content

Commit

Permalink
Add docs and make internal methods not public
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Dec 8, 2024
1 parent 49b5ba0 commit 2ceeb19
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 25 deletions.
83 changes: 68 additions & 15 deletions Packages/idv.jlchntoz.vrcage/Runtime/AntiGravityEngine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UdonSharp;
using UnityEngine;
using UnityEngine.UIElements;
using VRC.SDKBase;

namespace JLChnToZ.VRC.AGE {
Expand All @@ -11,9 +12,20 @@ namespace JLChnToZ.VRC.AGE {
[RequireComponent(typeof(global::VRC.SDK3.Components.VRCPlayerObject))]
[AddComponentMenu("/Anti Gravity Engine/Anti Gravity Engine")]
public class AntiGravityEngine : AntiGravityEngineBase {
[NonSerialized] public bool autoReattach;
[NonSerialized] public bool detachOnRespawn;
[NonSerialized] public float lerpScale = 10;
[NonSerialized]
#if COMPILER_UDONSHARP
public
#else
internal
#endif
bool autoReattach, detachOnRespawn;
[NonSerialized]
#if COMPILER_UDONSHARP
public
#else
internal
#endif
float lerpScale = 10;
[UdonSynced] Vector3 leftHandPosition, rightHandPosition;
[UdonSynced] int leftHandRotationBits, rightHandRotationBits;
Transform anchor;
Expand All @@ -29,6 +41,7 @@ public class AntiGravityEngine : AntiGravityEngineBase {
Quaternion leftHandRotation, rightHandRotation;
Matrix4x4 baseMatrix = Matrix4x4.identity;

/// <summary>Can the player moves freely?</summary>
public bool Mobility {
get => mobility;
set {
Expand All @@ -40,12 +53,22 @@ public bool Mobility {
}
}

/// <summary>The local to world matrix of the left hand.</summary>
public Matrix4x4 LeftHandMatrix => baseMatrix * Matrix4x4.TRS(leftHandPosition, leftHandRotation, Vector3.one);

/// <summary>The local to world matrix of the right hand.</summary>
public Matrix4x4 RightHandMatrix => baseMatrix * Matrix4x4.TRS(rightHandPosition, rightHandRotation, Vector3.one);

/// <summary>The position of the left hand in world space.</summary>
public Vector3 LeftHandPosition => baseMatrix.MultiplyPoint3x4(leftHandPosition);

/// <summary>The position of the right hand in world space.</summary>
public Vector3 RightHandPosition => baseMatrix.MultiplyPoint3x4(rightHandPosition);

/// <summary>The rotation of the left hand in world space.</summary>
public Quaternion LeftHandRotation => owner.GetRotation() * leftHandRotation;

/// <summary>The rotation of the right hand in world space.</summary>
public Quaternion RightHandRotation => owner.GetRotation() * rightHandRotation;

void Start() {
Expand Down Expand Up @@ -116,50 +139,77 @@ void UpdateAnchorPosition() => anchor.SetPositionAndRotation(
owner.GetRotation()
);

public void _UpdateMobility() =>
#if COMPILER_UDONSHARP
public
#endif
void _UpdateMobility() =>
station.PlayerMobility = mobility ?
VRCStation.Mobility.Mobile :
VRCStation.Mobility.Immobilize;

public bool Use() {
#if COMPILER_UDONSHARP
public
#else
internal
#endif
bool _Use() {
if (Networking.IsOwner(gameObject)) {
UpdateAnchorPosition();
if (isManualSync) RequestSerialization();
_UncheckedUse();
UncheckedUse();
return true;
}
return false;
}

public bool UseAt(Vector3 position, Quaternion rotation) {

#if COMPILER_UDONSHARP
public
#else
internal
#endif
bool UseAt(Vector3 position, Quaternion rotation) {
if (Networking.IsOwner(gameObject)) {
anchor.SetPositionAndRotation(position, rotation);
owner.TeleportTo(position, rotation);
if (isManualSync) RequestSerialization();
_UncheckedUse();
UncheckedUse();
return true;
}
return false;
}

void _UncheckedUse() {
void UncheckedUse() {
SendCustomEventDelayedFrames(nameof(_DeferUse), 0);
station.PlayerMobility = VRCStation.Mobility.Mobile;
}

public void _DeferUse() {
#if COMPILER_UDONSHARP
public
#endif
void _DeferUse() {
if (!Networking.IsOwner(gameObject)) return;
station.UseStation(owner);
Debug.Log("DeferUse");
SendCustomEventDelayedFrames(nameof(_UpdateMobility), 0);
}

public void Exit() {
#if COMPILER_UDONSHARP
public
#else
internal
#endif
void _Exit() {
station.ExitStation(owner);
if (Networking.IsOwner(gameObject) && isManualSync) RequestSerialization();
}

public void TeleportTo(Vector3 position, Quaternion rotation) {

#if COMPILER_UDONSHARP
public
#else
internal
#endif
void TeleportTo(Vector3 position, Quaternion rotation) {
if (!Networking.IsOwner(gameObject)) {
Networking.LocalPlayer.TeleportTo(position, rotation);
return;
Expand All @@ -169,12 +219,15 @@ public void TeleportTo(Vector3 position, Quaternion rotation) {
SendCustomEventDelayedFrames(nameof(_DeferTeleport), 0);
}

public void _DeferTeleport() {
#if COMPILER_UDONSHARP
public
#endif
void _DeferTeleport() {
if (!Networking.IsOwner(gameObject)) return;
station.ExitStation(owner);
owner.TeleportTo(teleportPosition, teleportRotation);
anchor.SetPositionAndRotation(teleportPosition, teleportRotation);
_UncheckedUse();
UncheckedUse();
}

public override void OnPreSerialization() {
Expand Down
11 changes: 11 additions & 0 deletions Packages/idv.jlchntoz.vrcage/Runtime/AntiGravityEngineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ namespace JLChnToZ.VRC.AGE {
/// <summary>The base class for Anti Gravity Engine components.</summary>
public abstract partial class AntiGravityEngineBase : UdonSharpBehaviour {
[SerializeField, HideInInspector] protected AntiGravityManager manager;
/// <summary>Is current sync mode is manual?</summary>
[SerializeField, HideInInspector] internal bool isManualSync;
/// <summary>The handler of the position and rotation.</summary>
[NonSerialized] internal protected AntiGravityHandler positionHandler;
[UdonSynced] internal byte selectedHandler;
/// <summary>The position of the object.</summary>
[UdonSynced(UdonSyncMode.Smooth)] protected Vector3 position;
/// <summary>The rotation of the object.</summary>
/// <remarks>This value is packed, use <see cref="UnpackRotation"/> and <see cref="PackQuaternion"/> to convert.</remarks>
[UdonSynced] protected int rotationBits;

public override void OnDeserialization() {
positionHandler = selectedHandler > 0 ? manager.GetHandlerOf(selectedHandler - 1) : null;
}

/// <summary>Serilize the position and rotation with selected handler.</summary>
/// <param name="position">The position to serialize. The value will be converted from absolute to relative.</param>
/// <param name="rotation">The rotation to serialize. The value will be converted from absolute to relative.</param>
protected bool SerializePosition(ref Vector3 position, ref Quaternion rotation) {
if (!Utilities.IsValid(positionHandler)) return false;
positionHandler.absolutePosition = position;
Expand All @@ -33,6 +41,9 @@ protected bool SerializePosition(ref Vector3 position, ref Quaternion rotation)
return true;
}

/// <summary>Deserialize the position and rotation with selected handler.</summary>
/// <param name="position">The position to deserialize. The value will be converted from relative to absolute.</param>
/// <param name="rotation">The rotation to deserialize. The value will be converted from relative to absolute.</param>
protected bool DeserializePosition(ref Vector3 position, ref Quaternion rotation) {
if (!Utilities.IsValid(positionHandler)) return false;
positionHandler.relativePosition = position;
Expand Down
53 changes: 48 additions & 5 deletions Packages/idv.jlchntoz.vrcage/Runtime/AntiGravityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ public partial class AntiGravityManager : UdonSharpBehaviour {
AntiGravityEngine localInstance;
DataDictionary instanceMap;

/// <summary>
/// Get the instance of <see cref="AntiGravityEngine"/> from a player.
/// </summary>
/// <param name="player">The player to get the instance from.</param>
/// <returns>The instance of <see cref="AntiGravityEngine"/> from the player.</returns>
public AntiGravityEngine GetInstanceFromPlayer(VRCPlayerApi player) {
if (Utilities.IsValid(instanceMap) && instanceMap.TryGetValue(player.playerId, TokenType.Reference, out var instance))
return (AntiGravityEngine)instance.Reference;
return null;
}

/// <summary>
/// Get the handler of the specified index.
/// </summary>
/// <param name="index">The index of the handler.</param>
/// <returns>The handler of the specified index.</returns>
public AntiGravityHandler GetHandlerOf(int index) {
if (index < 0 || index >= handlers.Length) return null;
return handlers[index];
Expand Down Expand Up @@ -53,25 +63,40 @@ public override void OnPlayerRestored(VRCPlayerApi player) {
if (autoUseOnLogin) SendCustomEventDelayedSeconds(nameof(Use), 3F);
}

/// <summary>
/// Attach local player to the Anti Gravity Engine with default or previous selected handler.
/// </summary>
/// <returns><langword>true</langword> if the player is successfully attached to the Anti Gravity Engine; otherwise, <langword>false</langword>.</returns>
public bool Use() {
Start();
if (!Utilities.IsValid(localInstance)) return false;
localInstance.Exit();
localInstance._Exit();
if (localInstance.selectedHandler == 0) SelectHandlerForLocal(initialSelectedHandler);
if (localInstance.Use()) return true;
if (localInstance._Use()) return true;
return false;

}

/// <summary>
/// Attach local player to the Anti Gravity Engine with specified handler.
/// </summary>
/// <param name="handlerIndex">The index of the handler to use.</param>
/// <returns><langword>true</langword> if the player is successfully attached to the Anti Gravity Engine; otherwise, <langword>false</langword>.</returns>
public bool Use(int handlerIndex) {
Start();
if (!Utilities.IsValid(localInstance)) return false;
localInstance.Exit();
localInstance._Exit();
SelectHandlerForLocal(handlerIndex);
if (localInstance.Use()) return true;
if (localInstance._Use()) return true;
return false;
}

/// <summary>
/// Attach local player to the Anti Gravity Engine with default or previous selected handler at specified position and rotation.
/// </summary>
/// <param name="position">The position to attach the player.</param>
/// <param name="rotation">The rotation to attach the player.</param>
/// <returns><langword>true</langword> if the player is successfully attached to the Anti Gravity Engine; otherwise, <langword>false</langword>.</returns>
public bool UseAt(Vector3 position, Quaternion rotation) {
Start();
if (!Utilities.IsValid(localInstance)) return false;
Expand All @@ -80,6 +105,13 @@ public bool UseAt(Vector3 position, Quaternion rotation) {
return true;
}

/// <summary>
/// Attach local player to the Anti Gravity Engine with specified handler at specified position and rotation.
/// </summary>
/// <param name="handlerIndex">The index of the handler to use.</param>
/// <param name="position">The position to attach the player.</param>
/// <param name="rotation">The rotation to attach the player.</param>
/// <returns><langword>true</langword> if the player is successfully attached to the Anti Gravity Engine; otherwise, <langword>false</langword>.</returns>
public bool UseAt(int handlerIndex, Vector3 position, Quaternion rotation) {
Start();
if (!Utilities.IsValid(localInstance)) return false;
Expand All @@ -88,6 +120,14 @@ public bool UseAt(int handlerIndex, Vector3 position, Quaternion rotation) {
return true;
}

/// <summary>
/// Teleport local player to specified position and rotation.
/// </summary>
/// <param name="position">The position to teleport the player.</param>
/// <param name="rotation">The rotation to teleport the player.</param>
/// <remarks>
/// This method will only work if the player is already attached to the Anti Gravity Engine.
/// </remarks>
public void TeleportTo(Vector3 position, Quaternion rotation) {
Start();
if (Utilities.IsValid(localInstance))
Expand All @@ -96,9 +136,12 @@ public void TeleportTo(Vector3 position, Quaternion rotation) {
Networking.LocalPlayer.TeleportTo(position, rotation);
}

/// <summary>
/// Exit the Anti Gravity Engine.
/// </summary>
public void Exit() {
Start();
localInstance.Exit();
localInstance._Exit();
}

void SelectHandlerForLocal(int index) {
Expand Down
19 changes: 15 additions & 4 deletions Packages/idv.jlchntoz.vrcage/Runtime/AntiGravityObjectSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class AntiGravityObjectSync : AntiGravityEngineBase {
[SerializeField] int initialSelectedHandler = -1;
/// <summary>If <see langword="true"/>, the object will be pickupable.</summary>
public bool pickupable = true;
/// <summary>The lerp scale of the object.</summary>
[NonSerialized] public float lerpScale = 10;
[NonSerialized]
#if COMPILER_UDONSHARP
public
#else
internal
#endif
float lerpScale = 10;
AntiGravityEngine playerAttachedAGE;
VRCPlayerApi ageOwner;
[UdonSynced] byte hand;
Expand Down Expand Up @@ -165,7 +170,10 @@ public override void OnDrop() {
SendCustomEventDelayedFrames(nameof(_UpdatePickupState), 0);
}

public void _UpdatePickupState() {
#if COMPILER_UDONSHARP
public
#endif
void _UpdatePickupState() {
if (localOnly || Networking.IsOwner(gameObject))
Hand = Utilities.IsValid(pickup) && pickup.IsHeld ?
pickup.currentHand : VRC_Pickup.PickupHand.None;
Expand All @@ -192,6 +200,9 @@ public override void OnDeserialization() {
}
}

public void _Drop() => pickup.Drop();
#if COMPILER_UDONSHARP
public
#endif
void _Drop() => pickup.Drop();
}
}
2 changes: 1 addition & 1 deletion Packages/idv.jlchntoz.vrcage/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "idv.jlchntoz.vrcage",
"displayName": "Anti Gravity Engine for VRChat",
"version": "0.2.0",
"version": "0.2.1",
"description": "This is an advanced player/pickupable gimmicks physics system for VRChat worlds.",
"gitDependencies": {},
"vpmDependencies": {
Expand Down

0 comments on commit 2ceeb19

Please sign in to comment.