-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameView.swift
410 lines (373 loc) · 12.8 KB
/
GameView.swift
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// GameView.swift
// SwiftPlaygrounds
//
// Created by A. Zheng (github.com/aheze) on 1/10/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
import SwiftUI
enum Player {
case red
case blue
var color: Color {
switch self {
case .red:
return .red
case .blue:
return .blue
}
}
}
struct Question {
var text: String
var answer: Int
}
struct GameView: View {
@AppStorage("targetScore") var targetScore = 5
@State var redScore = 0
@State var blueScore = 0
@State var redQuestion = Question(text: "2 + 2", answer: 4)
@State var blueQuestion = Question(text: "2 + 2", answer: 4)
@State var winner: Player?
@State var currentGameID = UUID()
var body: some View {
VStack {
GameSideView(player: .red, targetScore: targetScore, score: $redScore, question: $redQuestion, winner: $winner, currentGameID: $currentGameID)
.scaleEffect(x: -1, y: -1)
GameSideView(player: .blue, targetScore: targetScore, score: $blueScore, question: $blueQuestion, winner: $winner, currentGameID: $currentGameID)
}
.overlay {
CenterView(
targetScore: $targetScore,
redScore: $redScore,
blueScore: $blueScore,
redQuestion: $redQuestion,
blueQuestion: $blueQuestion
)
.offset(getCenterViewOffset())
}
.overlay {
VStack {
VStack {
if let winner = winner {
switch winner {
case .red:
Text("Red wins!")
.foregroundColor(.red)
.bold()
Text("Score: \(redScore)")
case .blue:
Text("Blue wins!")
.foregroundColor(.blue)
.bold()
Text("Score: \(blueScore)")
}
}
}
Button {
redScore = 0
blueScore = 0
redQuestion = Question(text: "2 + 2", answer: 4)
blueQuestion = Question(text: "2 + 2", answer: 4)
currentGameID = UUID()
withAnimation(.spring()) {
winner = nil
}
} label: {
Text("Play Again?")
}
.font(.title)
.foregroundColor(.white)
.padding()
.background(.green)
.cornerRadius(12)
}
.font(.largeTitle)
.padding()
.frame(maxWidth: 300, maxHeight: 300)
.background(.ultraThickMaterial)
.cornerRadius(16)
.offset(x: 0, y: winner == nil ? 1000 : 0)
.opacity(winner == nil ? 0 : 1)
}
}
func getCenterViewOffset() -> CGSize {
let difference = redScore - blueScore
let offset = difference * 10
if let winner = winner {
switch winner {
case .red:
return CGSize(width: 0, height: 600)
case .blue:
return CGSize(width: 0, height: -600)
}
} else {
return CGSize(width: 0, height: offset)
}
}
}
struct CenterView: View {
@Binding var targetScore: Int
@Binding var redScore: Int
@Binding var blueScore: Int
@Binding var redQuestion: Question
@Binding var blueQuestion: Question
@State var showingSettings = false
var body: some View {
ZStack {
HStack {
VStack(spacing: 16) {
Text("Score: \(redScore)")
.foregroundColor(.red)
.scaleEffect(x: -1, y: -1)
Text("Score: \(blueScore)")
.foregroundColor(.blue)
}
.padding()
Spacer()
}
.background(
Rectangle()
.fill(.green)
.frame(height: 5)
)
.overlay(
HStack {
Spacer()
Button {
withAnimation {
showingSettings.toggle()
}
} label: {
VStack {
if showingSettings {
Image(systemName: "xmark")
} else {
Image(systemName: "gearshape.fill")
}
}
.font(.title2.bold())
.foregroundColor(.green)
.frame(width: 40, height: 40)
.overlay(
Circle()
.stroke(Color.green, lineWidth: 5)
)
}
.background(Color(uiColor: .systemBackground))
.cornerRadius(20)
.padding(.trailing, 10)
}
)
VStack(spacing: 0) {
QuestionSideView(question: $redQuestion)
.scaleEffect(x: -1, y: -1)
.padding()
Rectangle()
.fill(.green)
.opacity(0.5)
.frame(height: 1)
QuestionSideView(question: $blueQuestion)
.padding()
}
.frame(maxWidth: 150)
.background(Color(uiColor: .systemBackground))
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(Color.green, lineWidth: 5)
)
}
.overlay(
VStack {
Text("Change Target Score (\(targetScore))")
HStack {
Group {
Button("5") { withAnimation { showingSettings = false }; targetScore = 5 }
Button("10") { withAnimation { showingSettings = false }; targetScore = 10 }
Button("20") { withAnimation { showingSettings = false }; targetScore = 20 }
}
.padding()
.background(Color(uiColor: .systemBackground))
.cornerRadius(12)
}
}
.padding()
.background(.regularMaterial)
.cornerRadius(16)
.opacity(showingSettings ? 1 : 0)
.offset(x: 0, y: showingSettings ? 0 : 80)
)
}
}
struct QuestionSideView: View {
@Binding var question: Question
var body: some View {
Text(question.text)
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.semibold)
}
}
struct GameButton: View {
let color: Color
let number: Int
let action: () -> Void
var body: some View {
Button(action: action) {
Text("\(number)")
.font(.title)
.bold()
.foregroundColor(color)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.ultraThinMaterial)
.cornerRadius(16)
}
}
}
struct GameSideView: View {
let player: Player
let targetScore: Int
@Binding var score: Int
@Binding var question: Question
@State var bubbles = [Bubble]()
@Binding var winner: Player?
@Binding var currentGameID: UUID
var body: some View {
VStack {
HStack {
GameButton(color: player.color, number: 1) { validate(attemptedAnswer: 1) }
GameButton(color: player.color, number: 2) { validate(attemptedAnswer: 2) }
GameButton(color: player.color, number: 3) { validate(attemptedAnswer: 3) }
}
HStack {
GameButton(color: player.color, number: 4) { validate(attemptedAnswer: 4) }
GameButton(color: player.color, number: 5) { validate(attemptedAnswer: 5) }
GameButton(color: player.color, number: 6) { validate(attemptedAnswer: 6) }
}
HStack {
GameButton(color: player.color, number: 7) { validate(attemptedAnswer: 7) }
GameButton(color: player.color, number: 8) { validate(attemptedAnswer: 8) }
GameButton(color: player.color, number: 9) { validate(attemptedAnswer: 9) }
}
}
.padding()
.padding(.top, 150)
.background {
ZStack {
ForEach(bubbles) { bubble in
Circle()
.fill(
LinearGradient(
colors: [
Color(uiColor: bubble.color),
Color(uiColor: bubble.color.offset(by: 0.1)),
],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.opacity(bubble.opacity)
.frame(width: bubble.length, height: bubble.length)
.offset(bubble.offset)
.transition(.scale)
}
}
}
.onChange(of: winner) { _ in
if let winner = winner {
let currentID = currentGameID
for index in 0 ..< 60 {
DispatchQueue.main.asyncAfter(deadline: .now() + CGFloat(index) / 15) {
if currentID == currentGameID {
let color = winner == .blue ? UIColor.systemBlue : UIColor.systemRed
let colorIndex = index - 30
addBubble(colorOffset: CGFloat(colorIndex) / 300, overrideColor: color)
}
}
}
} else {
withAnimation {
bubbles = []
}
}
}
}
func validate(attemptedAnswer: Int) {
if question.answer == attemptedAnswer {
withAnimation(.spring()) {
score += 1
}
addBubble(colorOffset: CGFloat(score) / 18)
} else {
withAnimation(.spring()) {
if score >= 1 {
score -= 1
}
}
removeLastBubble()
}
if score >= targetScore {
withAnimation(.spring()) {
winner = player
}
} else {
question = getQuestion()
}
}
func getQuestion() -> Question {
let isAddition = Bool.random()
if isAddition {
let firstNumber = Int.random(in: 1 ... 4)
let secondNumber = Int.random(in: 0 ... (9 - firstNumber))
let text = "\(firstNumber) + \(secondNumber)"
let answer = firstNumber + secondNumber
return Question(text: text, answer: answer)
} else {
let firstNumber = Int.random(in: 5 ... 9)
let secondNumber = Int.random(in: 0 ... (firstNumber - 1))
let text = "\(firstNumber) - \(secondNumber)"
let answer = firstNumber - secondNumber
return Question(text: text, answer: answer)
}
}
func addBubble(colorOffset: CGFloat, overrideColor: UIColor? = nil) {
let uiColor: UIColor
if let overrideColor = overrideColor {
uiColor = overrideColor.offset(by: colorOffset)
} else {
uiColor = UIColor(player.color).offset(by: colorOffset)
}
let newBubble = Bubble(
length: CGFloat.random(in: 100 ... 300),
offset: CGSize(
width: CGFloat.random(in: -240 ... 240),
height: CGFloat.random(in: -240 ... 240)
),
color: uiColor,
opacity: 1
)
withAnimation(.spring(
response: 0.8,
dampingFraction: 0.6,
blendDuration: 0.8
)) {
bubbles.append(newBubble)
}
withAnimation(.easeOut(duration: 10)) {
if let bubbleIndex = bubbles.firstIndex(where: { $0.id == newBubble.id }) {
bubbles[bubbleIndex].opacity = 0.2
}
}
}
func removeLastBubble() {
withAnimation(.spring(
response: 0.8,
dampingFraction: 0.6,
blendDuration: 0.8
)) {
if bubbles.count >= 1 {
bubbles.removeLast()
}
}
}
}