using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.Interactables;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
public class KeysPuzzle : MonoBehaviour
{
public static int totalInsertedKeys = 0;
public static int requiredKeys = 3;
public Animator doorAnimator;
public GameObject currentBattery;
private XRGrabInteractable grabInteractable;
private XRSocketInteractor socketInteractor;
private bool isKeyInserted = false;
void Start()
{
socketInteractor = GetComponent<XRSocketInteractor>();
if (socketInteractor != null)
{
socketInteractor.selectEntered.AddListener(OnKeyInserted);
}
// Disable battery grab until puzzle is solved
grabInteractable = currentBattery.GetComponent<XRGrabInteractable>();
grabInteractable.enabled = false;
}
public void OnKeyInserted(SelectEnterEventArgs args)
{
// Prevent multiple triggers
if (isKeyInserted)
return;
isKeyInserted = true;
totalInsertedKeys++;
Debug.Log("Keys inserted: " + totalInsertedKeys);
// When enough keys are inserted, complete the puzzle
if (totalInsertedKeys == requiredKeys)
{
CompleteTask();
}
}
private void CompleteTask()
{
doorAnimator.SetBool("character_nearby", true);
grabInteractable.enabled = true; // Enable grabbing the battery
}
}