XRoom_Unity/xroom/Assets/chair.cs
2025-05-26 11:51:25 +03:30

86 lines
3.0 KiB
C#

using UnityEngine;
using UnityEngine.XR;
using Fusion; // اضافه کردن فضای نام 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 Variable برای همگام‌سازی مقدار fill
[Networked] public bool networkedFill { get; set; }
public RTLTMPro.RTLTextMeshPro text;
void Start()
{
if (!text)
text =GetComponentInChildren<RTLTMPro.RTLTextMeshPro>();
leftController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public bool near;
void Update()
{
// bool near = Vector3.Distance(transform.position, player.transform.position) < distance;
// تنظیم رنگ با آلفای 0.6
//Color nearColor = near ? Color.yellow : Color.white;
//nearColor.a = 0.6f; // تنظیم مقدار آلفا به 0.6
//text.color = nearColor;
// if (!ConnectionManager.instance.runner.IsRunning || (player.GetComponent<BNG.SmoothLocomotion>().sit && !networkedFill))
// return;
// text.gameObject.SetActive(!networkedFill);
//if (near)
//{
// if ((leftController.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out bool isPressed) && isPressed) || Input.GetKeyDown(KeyCode.T))
// {
// }
//}
}
public void SetSit()
{
if (!ConnectionManager.instance.runner.IsRunning || (player.GetComponent<BNG.SmoothLocomotion>().sit && !networkedFill))
return;
Debug.Log("X button was pressed!");
if (!player.GetComponent<BNG.SmoothLocomotion>().sit)
{
sitMan = player;
player.position = new Vector3(transform.position.x, player.position.y, transform.position.z);
player.GetComponent<BNG.SmoothLocomotion>().sit = true;
player.GetComponent<BNG.PlayerTeleport>().chair = this;
SetFill(true); // وقتی که پر شود
}
else if (networkedFill && sitMan == player&& !ConnectionManager.instance.presentation)
{
Invoke("standUp", 0.3f);
}
}
public void standUp(){
sitMan=null;
SetFill(false); // وقتی که خالی شود
player.GetComponent<BNG.SmoothLocomotion>().sit = false;
}
// متد برای تنظیم مقدار fill و همگام‌سازی آن با دیگر کلاینت‌ها
void SetFill(bool value)
{
// if (HasInputAuthority) // بررسی این که آیا مالک این شیء هستید یا خیر
{
networkedFill = value; // مقدار شبکه‌ای را تنظیم می‌کند
}
}
}
}