XRoom_Unity/Assets/chair.cs

121 lines
3.5 KiB
C#

using UnityEngine;
using UnityEngine.XR;
using Fusion;
using TMPro;
using Fusion.Addons.ConnectionManagerAddon;
namespace BNG
{
public class Chair : NetworkBehaviour
{
public Transform player;
public Transform sitMan;
private UnityEngine.XR.InputDevice leftController;
[Networked] public bool networkedFill { get; set; }
public RTLTMPro.RTLTextMeshPro text;
public bool near;
void Start()
{
if (!text)
text = GetComponentInChildren<RTLTMPro.RTLTextMeshPro>();
leftController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj)
player = playerObj.transform;
}
void Update()
{
if (!Object || !Object.IsValid)
return;
if (!player)
return;
near = Vector3.Distance(transform.position, player.transform.position) < 1.5f;
// Change text color based on proximity
Color nearColor = near ? Color.yellow : Color.white;
nearColor.a = 0.6f;
text.color = nearColor;
if (!ConnectionManager.instance.runner.IsRunning || (player.GetComponent<BNG.SmoothLocomotion>().sit && !networkedFill))
return;
text.gameObject.SetActive(!networkedFill);
if (near)
{
bool isPressed = false;
if (leftController.TryGetFeatureValue(CommonUsages.primaryButton, out bool buttonPressed))
{
isPressed = buttonPressed;
}
if (isPressed || Input.GetKeyDown(KeyCode.T))
{
SetSit();
}
}
}
public void SetSit()
{
if (!Object || !Object.IsValid)
return;
if (!ConnectionManager.instance.runner.IsRunning || (player.GetComponent<BNG.SmoothLocomotion>().sit && !networkedFill))
return;
Debug.Log("Sit/Stand button was pressed!");
if (!player.GetComponent<BNG.SmoothLocomotion>().sit)
{
sitMan = player;
// Align player position with chair
player.GetComponent<BNG.SmoothLocomotion>().sit = true;
player.GetComponent<BNG.PlayerTeleport>().chair = this;
player.GetComponent<Rigidbody>().isKinematic = true;
player.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
SetFill(true); // Chair is now filled
}
else if (networkedFill && sitMan == player && !ConnectionManager.instance.presentation)
{
Invoke("StandUp", 0.3f);
}
}
public void StandUp()
{
if (!Object || !Object.IsValid)
return;
sitMan = null;
SetFill(false); // Chair is now empty
player.GetComponent<BNG.SmoothLocomotion>().sit = false;
player.GetComponent<BNG.SmoothLocomotion>().MaxVerticalVelocity = 10;
player.GetComponent<Rigidbody>().isKinematic = false;
}
void SetFill(bool value)
{
if (!Object || !Object.IsValid)
return;
networkedFill = value;
Debug.Log("SetFill called with value: " + value);
}
}
}