forked from oculus-samples/Unity-CrypticCabinet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockSpinner.cs
More file actions
81 lines (71 loc) · 2.9 KB
/
ClockSpinner.cs
File metadata and controls
81 lines (71 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System.Collections;
using CrypticCabinet.Audio;
using Oculus.Interaction;
using UnityEngine;
namespace CrypticCabinet.Puzzles.Clock
{
/// <summary>
/// Represents the clock spinner the user can interact with to update the clock hands' positions.
/// </summary>
public class ClockSpinner : MonoBehaviour
{
[SerializeField] private Transform m_handleRoot;
[SerializeField] private Transform m_handleTarget;
[SerializeField] private float m_spinsPerFullRotation = 3;
[SerializeField] private bool m_reverseHandle;
[SerializeField] private ClockHandMover m_clockHandMover;
[SerializeField] private ClockSpinnerAudio m_clockSpinnerAudioScript;
[SerializeField] private float m_audioSourceCooldownInterval;
[SerializeField] private Grabbable m_spinnerGrabbable;
private bool m_audioIsPlaying;
private Vector3 m_lastRight;
private void Start()
{
m_lastRight = m_handleRoot.worldToLocalMatrix.MultiplyVector(m_handleTarget.right);
if (m_spinnerGrabbable != null)
{
m_spinnerGrabbable.WhenPointerEventRaised += OnSpinnerEventRaised;
}
}
private void OnDestroy()
{
if (m_spinnerGrabbable != null)
{
m_spinnerGrabbable.WhenPointerEventRaised -= OnSpinnerEventRaised;
}
}
private void Update()
{
var currentRight = m_handleRoot.worldToLocalMatrix.MultiplyVector(m_handleTarget.right);
var handleMovementAmount = Vector3.SignedAngle(currentRight, m_lastRight, Vector3.up);
var absHandMovement = Mathf.Abs(handleMovementAmount);
if (absHandMovement > 0.01f)
{
var remappedMovementAngle = Mathf.InverseLerp(0, 360 * m_spinsPerFullRotation, absHandMovement);
var movementAngle = handleMovementAmount < 0 ? -remappedMovementAngle : remappedMovementAngle;
movementAngle = m_reverseHandle ? -movementAngle : movementAngle;
m_clockHandMover.UpdateHands(movementAngle);
if (absHandMovement > 1.2f && !m_audioIsPlaying)
{
m_audioIsPlaying = true;
m_clockSpinnerAudioScript.PlayAudio();
_ = StartCoroutine(nameof(AudioCooldownTimer));
}
}
m_lastRight = currentRight;
}
private IEnumerator AudioCooldownTimer()
{
yield return new WaitForSeconds(m_audioSourceCooldownInterval);
m_audioIsPlaying = false;
}
private void OnSpinnerEventRaised(PointerEvent obj)
{
if (obj.Type == PointerEventType.Unselect)
{
m_clockHandMover.SpinnerReleased();
}
}
}
}