-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnAnimate.cs
More file actions
210 lines (181 loc) · 5.97 KB
/
OnAnimate.cs
File metadata and controls
210 lines (181 loc) · 5.97 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Animation))]
public class OnAnimate : MonoBehaviour
{
public ExperimentState expState;
public Animation scaleAnimation;
public Animation textAnimation;
public TMPro.TMP_Text scoreText;
public AudioSource[] scoreAccumulationSound;
public Renderer indicatorRend;
public float waitTime = 4f;
public int bonusPoints = 4;
public GameObject TreasureChest;
public GameObject[] RewardRings;
private int accumulatedReward;
private bool onTargetStay = false;
private Coroutine soundRoutine;
private float startTime = 0f;
private float elapsedTime = 0f;
private bool bonusScore = false;
private Material _mat;
private Transform scaledObj;
private Vector3 originalScale;
[Range(0f, 0.5f)]
public float rewardDelay1 = 0.2f;
[Range(0f, 0.5f)]
public float rewardDelay2 = 0.2f;
private void Start()
{
if (expState.group == 0)
{
scoreText.text = "";
}
foreach (GameObject rewardRing in RewardRings)
{
rewardRing.SetActive(false);
}
scaledObj = transform;
originalScale = transform.localScale;
TreasureChest.SetActive(false);
_mat = indicatorRend.GetComponent<Renderer>().material;
//scaleAnimation = GetComponent<Animation>();
}
private void Update()
{
if (onTargetStay)
elapsedTime = Time.time - startTime;
else
elapsedTime = 0f;
//Debug.Log("T: " + elapsedTime);
if (expState.group != 0) // Only if you are in group one should there be a bonus round
{
// TernaryOperator approach to if statement
bonusScore = elapsedTime >= waitTime ? true : false;
}
}
private void OnTriggerEnter()
{
if (expState.group != 0) // Only if you are in group one should there be a bonus round
{
//scaleAnimation.Play();
textAnimation.Play();
//scoreAccumulationSound.Play();
startTime = Time.time;
StartCoroutine(PlaySound());
}
}
private void OnTriggerStay()
{
if (expState.group != 0) // Only if you are in group one should there be a bonus round
{
//scaleAnimation.Play();
textAnimation.Play();
if (!onTargetStay)
{
//StartCoroutine(ScaleAnimation());
StartCoroutine(PlaySound());
onTargetStay = true;
}
}
}
private void OnTriggerExit()
{
if (expState.group != 0) // Only if you are in group one should there be a bonus round
{
onTargetStay = false;
}
}
private IEnumerator PlaySound()
{
if (bonusScore)
{
_mat.SetColor("_EmissionColor", Color.red * 6.0f);
expState.addtoScore = bonusPoints;
expState.score += expState.addtoScore;
accumulatedReward += bonusPoints;
scoreText.text = "+" + bonusPoints.ToString();
scoreAccumulationSound[1].Play();
yield return new WaitForSecondsRealtime(rewardDelay1);
if (accumulatedReward == 12)
{
RewardRings[0].SetActive(true);
}
if (accumulatedReward == 20)
{
RewardRings[1].SetActive(true);
}
if (accumulatedReward == 32)
{
accumulatedReward = 0;
StartCoroutine(RewardChest());
elapsedTime = 0f;
bonusScore = false;
}
}
else
{
// Reset accumulated reward
accumulatedReward = 0;
foreach (GameObject rewardRing in RewardRings)
{
rewardRing.SetActive(false);
}
_mat.SetColor("_EmissionColor", Color.black * 1f);
expState.addtoScore = 1;
expState.score += expState.addtoScore;
scoreText.text = "+1";
scoreAccumulationSound[0].Play();
yield return new WaitForSecondsRealtime(rewardDelay2);
}
onTargetStay = false;
//yield return null;
}
private IEnumerator RewardChest()
{
RewardRings[2].SetActive(true);
TreasureChest.SetActive(true);
expState.score += 100;
scoreText.text = "+100";
yield return new WaitForSecondsRealtime(1.5f);
TreasureChest.SetActive(false);
foreach (GameObject rewardRing in RewardRings)
{
rewardRing.SetActive(false);
}
bonusScore = false;
elapsedTime = 0f;
}
private IEnumerator ScaleAnimation()
{
while (!onTargetStay)
yield return null;
float scaleto = 1.25f;
float scaleVal = originalScale.z;
float scaleDiff = originalScale.z - scaleto;
if (scaleDiff >= 0f)
{
while (transform.localScale.z < scaleto)
{
scaleVal += 0.01f;
scaledObj.localScale = new Vector3(originalScale.x,
originalScale.y,
originalScale.z * Mathf.Sin(scaleVal));
transform.SetParent(scaledObj);
yield return null;
}
}
//else //if(dialDiff > 0)
//{
// while (yRot > TargetAngle)
// {
// yRot -= expState.angularSpeed;
// Quaternion toRot = Quaternion.Euler(0f, yRot, 0f);
// transform.localRotation = toRot;
// yield return null;
// }
//}
//yield return null;
}
}