-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiPlay.swift
More file actions
146 lines (136 loc) · 5.2 KB
/
MultiPlay.swift
File metadata and controls
146 lines (136 loc) · 5.2 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
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
The play screen for multiplayer.
*/
import SwiftUI
struct MultiPlay: View {
@Environment(GameModel.self) var gameModel
@Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
var body: some View {
HStack(alignment: .top) {
VStack(spacing: 0) {
let progress = Float(gameModel.timeLeft) / Float(GameModel.gameTime)
HStack(alignment: .top) {
Button {
Task {
await dismissImmersiveSpace()
}
gameModel.reset()
} label: {
Label("Back", systemImage: "chevron.backward")
.labelStyle(.iconOnly)
}
.offset(x: -10)
VStack {
Text(verbatim: "\(String(format: "%02d", gameModel.score))")
.font(.system(size: 60))
.bold()
.accessibilityLabel(Text("Score"))
.accessibilityValue(Text("\(gameModel.score)"))
Text("\(fantasyName(for: you, in: sortedByScore)) (you)")
.font(.system(size: 20))
.bold()
.accessibilityHidden(true)
.offset(y: -5)
}
.padding(.leading, 0)
.padding(.trailing, 60)
}
ForEach(gameModel.players.filter { $0.name != you.name }, id: \.name) { player in
HStack {
Text(fantasyName(for: player, in: gameModel.players))
Text("\(player.score)")
}
.opacity(0.5)
}
.font(.headline)
HStack {
Button {
gameModel.isMuted.toggle()
} label: {
Label(
gameModel.isMuted ? "Play music" : "Stop music",
systemImage: gameModel.isMuted ? "speaker.slash.fill" : "speaker.wave.3.fill"
)
.labelStyle(.iconOnly)
}
.padding(.leading, 12)
.padding(.trailing, 10)
ProgressView(value: (progress > 1.0 || progress < 0.0) ? 1.0 : progress)
.contentShape(.accessibility, Capsule().offset(y: -3))
.accessibilityLabel("")
.accessibilityValue(Text("\(gameModel.timeLeft) seconds remaining"))
.tint(Color(uiColor: UIColor(red: 242 / 255, green: 68 / 255, blue: 206 / 255, alpha: 1.0)))
.padding(.vertical, 30)
Button {
gameModel.isPaused.toggle()
gameModel.isMuted.toggle()
} label: {
if gameModel.isPaused {
Label("Play", systemImage: "play.fill")
.labelStyle(.iconOnly)
} else {
Label("Pause", systemImage: "pause.fill")
.labelStyle(.iconOnly)
}
}
.padding(.trailing, 12)
.padding(.leading, 0)
.hidden()
}
.background(
.regularMaterial,
in: .rect(
topLeadingRadius: 0,
bottomLeadingRadius: 12,
bottomTrailingRadius: 12,
topTrailingRadius: 0,
style: .continuous
)
)
.frame(width: 260, height: 70)
.offset(y: 15)
}
.padding(.vertical, 12)
}
.frame(width: 260)
.task {
do {
#if targetEnvironment(simulator)
let shouldAddProjector = true
#else
let shouldAddProjector = gameModel.inputKind == .alternative
#endif
if shouldAddProjector, heart != nil {
try await addFloorBeamMaterials()
}
} catch {
print(error)
}
}
}
var you: Player {
#if targetEnvironment(simulator)
gameModel.players.first!
#else
gameModel.players.first(where: { $0.name == Player.localName })!
#endif
}
var youWon: Bool {
sortedByScore.first!.name == you.name
}
var sortedByScore: [Player] {
gameModel.players.sorted(using: KeyPathComparator(\.score, order: .reverse))
}
}
#Preview {
MultiPlay()
.environment(GameModel())
.glassBackgroundEffect(
in: RoundedRectangle(
cornerRadius: 32,
style: .continuous
)
)
}