-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiScore.swift
113 lines (97 loc) · 3.18 KB
/
MultiScore.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
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
The score screen for multiplayer.
*/
import SwiftUI
struct MultiScore: View {
@Environment(GameModel.self) var gameModel
@Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
var body: some View {
VStack(spacing: 10) {
Image("greatJob")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 450, height: 200, alignment: .center)
.accessibilityHidden(true)
if youWon {
Text("You won!")
.font(.system(size: 30, weight: .bold))
.offset(y: -13)
} else {
Text("\(fantasyName(for: sortedByScore.first!, in: gameModel.players)) won!")
.font(.system(size: 30, weight: .bold))
.offset(y: -13)
}
ForEach(sortedByScore, id: \.name) { player in
HStack {
Text("\(sortedByScore.firstIndex(where: { $0.name == player.name })! + 1). " + fantasyName(for: player, in: gameModel.players)
+ (player.name == you.name
? "(you) \(player.score)"
: " \(player.score)"))
}
.font(.headline)
}
.frame(minWidth: 0, maxWidth: 135, alignment: .leading)
.offset(y: -13)
Group {
Button {
playAgain()
} label: {
Text("Play Again")
.frame(maxWidth: .infinity)
}
Button {
Task {
await goBackToStart()
}
} label: {
Text("Back to Main Menu")
.frame(maxWidth: .infinity)
}
}
.frame(width: 220)
}
.padding(15)
.frame(width: 634, height: 550)
}
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))
}
func playAgain() {
let inputChoice = gameModel.inputKind
gameModel.reset()
if beamIntermediate.parent == nil {
spaceOrigin.addChild(beamIntermediate)
}
gameModel.isPlaying = true
gameModel.isInputSelected = true
gameModel.isCountDownReady = true
gameModel.inputKind = inputChoice
}
@MainActor
func goBackToStart() async {
await dismissImmersiveSpace()
gameModel.reset()
}
}
#Preview {
MultiScore()
.environment(GameModel())
.glassBackgroundEffect(
in: RoundedRectangle(
cornerRadius: 32,
style: .continuous
)
)
}