-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabySimulation.cs
229 lines (194 loc) · 5.71 KB
/
BabySimulation.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BabySimulation : MonoBehaviour
{
public Text heartRateText; // Used to display heart rate
public Text respiratoryRateText; // Used to display respiratory rate
public Text timerText; // Used to display time
public AudioSource breathAudioSource; // Sound source
public GameObject successDialog; // Success dialog
public GameObject reassessDialog; // Reassess dialog
public float delayToShowDialog = 5f; // Delay to show the dialog
private Animator babyAnimator;
private float lastSqueezeTime;
private int validBreathsInLast6Seconds;
private int failedAttempts;
private int heartRate = 80;
private int respiratoryRate = 20;
private float timer = 0f;
private bool levelCompleted = false; // 添加一个标志以确保只发送一次数据
private const int maxHeartRate = 130;
public GameObject failureIcon; // Failure icon
public AudioSource failureAudioSource; // Failure sound source
public float minSqueezeDurationForValidBreath = 0.4f;
public float failureIconDisplayDuration = 1f;
public int gamelevel = 2;
public int finishedTime = 0;
public int isRight = 0;
public long idcode = 201;
void Start()
{
babyAnimator = GetComponent<Animator>();
InvokeRepeating("EvaluateBreaths", 6f, 6f);
if (breathAudioSource == null || successDialog == null || reassessDialog == null || failureIcon == null || failureAudioSource == null)
{
Debug.LogError("One or more required components not assigned!");
}
}
void Update()
{
UpdateTimer();
UpdateUI();
if (Input.GetMouseButtonDown(0))
{
StartSqueeze();
}
if (Input.GetMouseButtonUp(0))
{
EndSqueeze();
}
if (timer > delayToShowDialog)
{
if (heartRate > 60)
{
ShowSuccessDialog();
}
else
{
ShowReassessDialog();
}
// 设置标志为true,表示关卡已完成
levelCompleted = true;
}
}
void ShowSuccessDialog()
{
if (successDialog != null)
{
successDialog.SetActive(true);
}
isRight = 1;
SaveLevelDetails();
}
void ShowReassessDialog()
{
if (reassessDialog != null)
{
reassessDialog.SetActive(true);
}
isRight = 0;
SaveLevelDetails();
}
void StartSqueeze()
{
lastSqueezeTime = Time.time;
babyAnimator.SetTrigger("Squeeze");
PlayBreathSound();
}
void EndSqueeze()
{
float squeezeDuration = Time.time - lastSqueezeTime;
if (squeezeDuration >= minSqueezeDurationForValidBreath)
{
int increaseAmount = Random.Range(4, 9);
heartRate += increaseAmount;
heartRate = Mathf.Min(heartRate, maxHeartRate);
validBreathsInLast6Seconds++;
}
else
{
PlayFailureSound();
ShowFailureIcon();
StartCoroutine(HideFailureIconAfterDelay());
}
babyAnimator.SetTrigger("Inflate");
StopBreathSound();
}
void PlayBreathSound()
{
if (breathAudioSource != null)
{
breathAudioSource.Play();
}
}
void StopBreathSound()
{
if (breathAudioSource != null)
{
breathAudioSource.Stop();
}
}
void PlayFailureSound()
{
if (failureAudioSource != null)
{
failureAudioSource.Play();
}
}
void ShowFailureIcon()
{
if (failureIcon != null)
{
failureIcon.SetActive(true);
}
}
IEnumerator HideFailureIconAfterDelay()
{
yield return new WaitForSeconds(failureIconDisplayDuration);
if (failureIcon != null)
{
failureIcon.SetActive(false);
}
}
void EvaluateBreaths()
{
if (validBreathsInLast6Seconds >= 4)
{
Debug.Log("Breaths are valid. No penalty.");
validBreathsInLast6Seconds = 0;
}
else
{
int decreaseAmount = Mathf.Max((4 - validBreathsInLast6Seconds) * Random.Range(2, 6) * failedAttempts, 30);
heartRate -= decreaseAmount;
heartRate = Mathf.Max(heartRate, 30);
Debug.Log("Breaths are not valid. Heart rate decreased by: " + decreaseAmount);
failedAttempts++;
validBreathsInLast6Seconds = 0;
}
}
void UpdateUI()
{
if (heartRateText != null)
{
heartRateText.text = heartRate.ToString();
}
if (respiratoryRateText != null)
{
respiratoryRateText.text = respiratoryRate.ToString();
}
}
//存储游戏完成情况
void SaveLevelDetails()
{
// 只有在关卡完成并且数据还没有被发送时才发送数据
if (levelCompleted)
{
finishedTime = (failedAttempts == 0) ? 0 : 1;
DataSender.Instance.SaveLevelDetails(gamelevel, finishedTime, isRight, SavePlayer.Ins.PlayerId);
}
}
void UpdateTimer()
{
timer += Time.deltaTime;
if (timerText != null)
{
int hours = Mathf.FloorToInt(timer / 3600);
int minutes = Mathf.FloorToInt((timer % 3600) / 60);
int seconds = Mathf.FloorToInt(timer % 60);
timerText.text = hours.ToString("D2") + ": " + minutes.ToString("D2") + ": " + seconds.ToString("D2");
}
}
}