2025-11-18 22:29:02 +02:00

148 lines
4.6 KiB
C#

using FMOD.Studio;
using FMODUnity;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit;
public class MenuTeleportButton : MonoBehaviour
{
public Sprite NormalSprite;
public Sprite HoverSprite;
public String TargetName;
public XRBaseControllerInteractor LeftXRInteractor; // Reference to XR controller interactor (e.g., Ray Interactor)
public XRBaseControllerInteractor RightXRInteractor;
public XROrigin Player;
public TeleportationProvider teleportationProvider; // Reference to TeleportationProvider
private Button button;
private TeleportLocation target; // Target teleport position
private EventInstance TeleportingSound;
FMOD.Studio.Bus SpecialBus; //FMOD bus variable
private static MenuTeleportButton lastSelectedButton = null;
private static bool initialized = false;
// External state: map must be held/visible
public static bool MapIsOpen = false;
private Menu menu;
private void Awake()
{
TeleportingSound = AudioManager.Instance.CreateInstance(FMODEvents.Instance.Teleport); //initialise the instance
SpecialBus = FMODUnity.RuntimeManager.GetBus("bus:/LogicalMute");
}
void Start()
{
button = GetComponent<Button>();
menu = FindObjectOfType<Menu>();
// Subscribe to button events
button.onClick.AddListener(TeleportPlayer);
TeleportLocation[] locations = FindObjectsOfType<TeleportLocation>(); // Fetch all teleport locations in the scene.
foreach (TeleportLocation location in locations) // Find the target location.
{
if (location.Name.Equals(TargetName))
{
target = location;
break;
}
}
if (target == null)
{
Debug.LogError("Teleport target of " + TargetName + " not found.");
}
}
public void SetStateSelected()
{
if (button != null && HoverSprite != null)
{
button.targetGraphic.GetComponent<Image>().sprite = HoverSprite;
// --- Only play hover sound if selection actually changed ---
//if (initialized && lastSelectedButton != this)
//{
// if (!Menu.IsMapOpen) return;
// if (!menu.MapTab.activeSelf) return; // ensures only map page buttons make sound
// AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
//}
if (!Menu.IsMapOpen) return;
if (!menu.MapTab.activeSelf) return; // ensures only map page buttons make sound
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
//lastSelectedButton = this;
//initialized = true;
}
}
public void SetStateDefault()
{
// Refresh the button state.
button.interactable = false;
button.interactable = true;
if (NormalSprite != null)
{
button.targetGraphic.GetComponent<Image>().sprite = NormalSprite;
}
}
private void TeleportPlayer()
{
if (target == null || Player == null || teleportationProvider == null)
{
Debug.LogWarning("Teleportation failed: Target, Player, or TeleportationProvider is missing.");
return;
}
// Prepare teleport request
TeleportRequest request = new TeleportRequest
{
destinationPosition = target.transform.position,
destinationRotation = target.transform.rotation,
matchOrientation = MatchOrientation.TargetUpAndForward
};
// Queue teleport request
bool success = teleportationProvider.QueueTeleportRequest(request);
if (!success)
{
Debug.LogWarning("Teleport request failed to queue.");
}
// Refresh the button state
button.interactable = false;
button.interactable = true;
StartCoroutine(MuteBusForSeconds(2.0f));
TeleportingSound.start(); //playing 2d oneshot
}
private IEnumerator MuteBusForSeconds(float duration)
{
// Lower volume to 0 instantly
SpecialBus.setVolume(0f);
Debug.Log("[MenuTeleportButton] Muting LogicalMute bus...");
yield return new WaitForSeconds(duration);
// Restore volume
SpecialBus.setVolume(1f);
Debug.Log("[MenuTeleportButton] Unmuted LogicalMute bus.");
}
}