-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialSwitch.cs
78 lines (67 loc) · 1.61 KB
/
DialSwitch.cs
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
using UnityEngine;
using System.Collections;
using System;
public class DialSwitch : MonoBehaviour {
public GameObject offBG;
public GameObject onBG;
public GameObject dial;
public LookingAt lookingAt;
public float lookTime;
private float lookPct;
private bool looking;
private bool flipped;
public bool on = false;
public event Action<bool> OnSwitchChange;
void OnEnable () {
lookingAt.OnLookingAt += HandleOnLookingAt;
lookingAt.OnLookedAway += HandleOnLookedAway;
}
void OnDisable () {
lookingAt.OnLookingAt -= HandleOnLookingAt;
lookingAt.OnLookedAway -= HandleOnLookedAway;
}
void HandleOnLookedAway (GameObject seenObj) {
if (seenObj.Equals (gameObject)) {
looking = false;
flipped = false;
dial.SetActive (false);
dial.GetComponent<Renderer> ().material.SetFloat ("_Cutoff", 1);
}
}
void HandleOnLookingAt (GameObject seenObj, float timeSeen) {
if (seenObj.Equals (gameObject)) {
if (!looking && !flipped) {
looking = true;
dial.SetActive (true);
}
lookPct = timeSeen / lookTime;
if (lookPct < 1) {
//Debug.Log("look pct = "+lookPct);
dial.GetComponent<Renderer> ().material.SetFloat ("_Cutoff", 1 - lookPct);
} else {
if (!flipped) {
on = !on;
flipped = true;
//Debug.Log("FLIP SWITCH");
dial.SetActive (false);
setSwitchView ();
//do flip switch
if (OnSwitchChange != null)
OnSwitchChange (on);
}
}
}
}
private void setSwitchView () {
onBG.SetActive (on);
offBG.SetActive (!on);
}
void Awake () {
dial.SetActive (false);
setSwitchView ();
}
void Start () {
}
void Update () {
}
}