-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryModeView.swift
More file actions
204 lines (187 loc) · 7.62 KB
/
Copy pathMemoryModeView.swift
File metadata and controls
204 lines (187 loc) · 7.62 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
import SwiftUI
struct MemoryModeView: View {
private let audioManager = AudioManager.shared
@State private var topPadEffects: [VibrateEffectsType] = []
@State private var bottomPadEffects: [VibrateEffectsType] = VibrateEffects.rememberModeEffects
@State private var answers: [VibrateEffectsType] = []
@State private var difficulty: String = "Easy"
@State private var pressedIndex: [Int?] = []
@State private var isPressing: Bool = false
@State private var isIntroductionPlaying: Bool = false
@State private var isTopPadTapped: Bool = false
@State private var bottomPadScales: [CGFloat] = [1, 1, 1, 1, 1, 1]
var body: some View {
ZStack {
LinearGradient(gradient: Gradient(colors: [Color.blue, Color.purple]),
startPoint: .topLeading, endPoint: .bottomTrailing)
.ignoresSafeArea()
.overlay(Color.white.opacity(0.1))
VStack(spacing: 30) {
Button(action: {
withAnimation {
changeDifficulty()
}
}) {
HStack {
Image(systemName: "slider.horizontal.3")
Text(difficulty)
.font(.title2)
.fontWeight(.bold)
}
.padding()
.frame(width: 180, height: 50)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.green))
.foregroundColor(.white)
.shadow(radius: 5)
}
VStack(spacing: 30) {
RoundedRectangle(cornerRadius: 20)
.fill(LinearGradient(gradient: Gradient(colors: [Color.blue.opacity(0.8), Color.cyan.opacity(0.8)]),
startPoint: .topLeading, endPoint: .bottomTrailing))
.frame(width: 120, height: 120)
.shadow(radius: 10)
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onChanged { _ in
playContinuousVibrate()
}
.onEnded { _ in
isPressing = false
}
)
HStack(spacing: 20) {
ForEach(0..<bottomPadEffects.count, id: \.self) { index in
RoundedRectangle(cornerRadius: 20)
.fill(LinearGradient(gradient: Gradient(colors: [Color.purple.opacity(0.8), Color.pink.opacity(0.8)]),
startPoint: .topLeading, endPoint: .bottomTrailing))
.frame(width: 100, height: 100)
.shadow(radius: 10)
.scaleEffect(bottomPadScales[index])
.gesture(TapGesture().onEnded {
bottomPadEffects[index].playVibrate()
bottomPadScales[index] = 1.1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
bottomPadScales[index] = 1.0
}
})
.simultaneousGesture(
LongPressGesture()
.sequenced(before: TapGesture())
.onEnded { _ in
checkAnswer(index: index)
bottomPadScales[index] = 1.1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
bottomPadScales[index] = 1.0
}
}
)
}
}
}
}
VStack {
HStack {
Spacer()
Button(action: {
controlIntroductionPlaying()
}) {
Circle()
.frame(width: 25, height: 25)
.foregroundStyle(Color.black.opacity(0.3))
.padding()
}
.padding()
}
Spacer()
}
}
.onAppear {
setTopPad()
}
}
func setTopPad() {
topPadEffects.removeAll()
switch difficulty {
case "Easy":
topPadEffects = Array(VibrateEffects.easyEffects.shuffled().prefix(3))
case "Medium":
topPadEffects = Array(VibrateEffects.mediumEffects.shuffled().prefix(4))
case "Hard":
topPadEffects = Array(VibrateEffects.rememberModeEffects.shuffled().prefix(5))
default:
break
}
}
func changeDifficulty() {
withAnimation(.easeInOut(duration: 0.3)) {
switch difficulty {
case "Easy":
difficulty = "Medium"
case "Medium":
difficulty = "Hard"
case "Hard":
difficulty = "Easy"
default:
break
}
}
audioManager.introduce(IntroductionMessages.difficultyChange(difficulty))
setTopPad()
}
func playContinuousVibrate() {
if !isPressing {
isPressing = true
for (index, effect) in topPadEffects.enumerated() {
playVibrate(index: index, effect: effect)
}
}
}
private func playVibrate(index: Int, effect: VibrateEffectsType) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 1.0) {
if isPressing {
effect.playVibrate()
}
}
}
func checkAnswer(index: Int) {
pressedIndex.append(index)
let capacity = setCapacity()
chooseAnswer(capacity: capacity, index: index)
compareAnswer(capacity: capacity, index: index)
}
private func setCapacity() -> Int {
switch difficulty {
case "Easy": return 3
case "Medium": return 4
case "Hard": return 5
default: return 0
}
}
private func chooseAnswer(capacity: Int, index: Int) {
if answers.count < capacity {
answers.append(bottomPadEffects[index])
audioManager.introduce(IntroductionMessages.selected)
}
}
private func compareAnswer(capacity: Int, index: Int) {
if answers.count == capacity {
if topPadEffects == answers {
audioManager.introduce(IntroductionMessages.correcct)
setTopPad()
} else {
audioManager.introduce(IntroductionMessages.memoryModeWrong)
}
answers.removeAll()
pressedIndex.removeAll()
}
}
func controlIntroductionPlaying() {
if !isIntroductionPlaying {
isIntroductionPlaying = true
audioManager.introduce(IntroductionMessages.memoryModeIntroduction)
return
}
isIntroductionPlaying = false
audioManager.stopSpeaking()
}
}