campfire/Assets/Scripts/BackpackAccessor.cs

52 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
using Valve.VR.InteractionSystem;
public class BackpackAccessor : MonoBehaviour {
public Backpack Backpack;
[Header("Actions")]
public SteamVR_Action_Boolean[] GrabActions;
[Tooltip("Should GrabAction become true or false when grabbing?")]
public bool GrabOnBoolean = false;
public SteamVR_Action_Boolean[] StoreActions;
[Tooltip("Should StoreAction become true or false when dropping an item in the backpack?")]
public bool StoreOnBoolean = false;
private Hand Hand;
private void OnEnable() {
Hand = GetComponent<Hand>();
foreach (SteamVR_Action_Boolean GrabAction in GrabActions) {
GrabAction.AddOnChangeListener(OnGrabActionChange, Hand.handType);
}
foreach (SteamVR_Action_Boolean StoreAction in StoreActions) {
StoreAction.AddOnChangeListener(OnStoreActionChange, Hand.handType);
}
}
private void OnDisable() {
foreach (SteamVR_Action_Boolean GrabAction in GrabActions) {
GrabAction.RemoveOnChangeListener(OnGrabActionChange, Hand.handType);
}
foreach (SteamVR_Action_Boolean StoreAction in StoreActions) {
StoreAction.RemoveOnChangeListener(OnStoreActionChange, Hand.handType);
}
}
private void OnGrabActionChange(SteamVR_Action_Boolean actionIn, SteamVR_Input_Sources inputSources, bool newValue) {
if (newValue == GrabOnBoolean) {
Backpack.Grab(Hand);
}
}
private void OnStoreActionChange(SteamVR_Action_Boolean actionIn, SteamVR_Input_Sources inputSources, bool newValue) {
if (newValue == StoreOnBoolean) {
Backpack.Store(Hand);
}
}
}