using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
[RequireComponent(typeof(AudioSource))]
public class SocketDoorManager : MonoBehaviour
{
public GameObject[] specificDoorCollider;
public GameObject[] specificDoorAnimationCollider;
public NavMeshObstacle[] navMeshObstacle;
private XRSocketInteractor socketInteractor;
private AudioSource audioSource;
public static int batteryCount = 0;
public bool isFinalSocket;
public AudioClip insertBatterySound;
void Start()
{
socketInteractor = GetComponent();
audioSource = GetComponent();
if (!isFinalSocket)
{
socketInteractor.selectEntered.AddListener(OnBatteryInserted);
}
else
{
socketInteractor.enabled = false;
}
}
void Update()
{
// Enable final socket when all 3 batteries are placed
if (isFinalSocket && batteryCount == 3 && !socketInteractor.enabled)
{
socketInteractor.enabled = true;
}
}
private void OnBatteryInserted(SelectEnterEventArgs args)
{
// Play insertion sound
if (insertBatterySound != null)
{
audioSource.PlayOneShot(insertBatterySound);
}
else
{
Debug.LogWarning("No AudioClip assigned for battery insertion sound.");
}
if (specificDoorCollider != null)
{
if (!isFinalSocket)
{
OpenDoor();
}
batteryCount++;
Debug.Log("Battery count: " + batteryCount);
}
}
private void OpenDoor()
{
for (int i = 0; i < specificDoorCollider.Length; i++)
{
specificDoorCollider[i].SetActive(false); // Disable door blocker
specificDoorAnimationCollider[i].SetActive(true); // Enable animated version
navMeshObstacle[i].enabled = false; // Clear navigation obstacle
}
}
private void OnDestroy()
{
if (socketInteractor != null)
{
socketInteractor.selectEntered.RemoveListener(OnBatteryInserted);
}
}
}