-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
219 lines (199 loc) · 8.33 KB
/
ContentView.swift
File metadata and controls
219 lines (199 loc) · 8.33 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// ContentView.swift
// basketball
//
// Created by tal barmocha on 26/06/2024.
//
import SwiftUI
struct ContentView: View {
@StateObject private var settings = GameSettings()
@State private var timer: Timer? = nil
@State private var showingWinnerPopup = false
@State private var winnerTeam: Int?
@State private var timestarted = false
@State private var savedWinningsScore = 0
var body: some View {
NavigationView {
ZStack{
VStack {
//UPPER BAR
HStack {
Button(action: newGame) {
Text("New Game")
.font(.title3)
.fontWeight(.semibold)
.foregroundColor(.primary)
}
.padding(.horizontal)
Spacer()
HStack{
Text("Win: \(settings.winningScore)").font(.title3)
Text("pts").font(.subheadline).padding(.leading,-5).padding(.top,6)
}
.fontWeight(.semibold)
.foregroundColor(.primary)
NavigationLink(destination: SettingsView(settings: settings)) {
Image(systemName: "gearshape.fill").foregroundStyle(Color.primary).dynamicTypeSize(.xLarge)
}
.padding(5)
.padding(.horizontal)
}
.padding(3)
Spacer()
//TEAM1
ZStack{
VStack {
ScoreButton(title: "+\(settings.pointsToAdd)",secondbotton: false, color: settings.team1Color) {
settings.team1Score += settings.pointsToAdd
checkForWinner()
}
.fontWeight(.bold)
.opacity(0.5)
ScoreButton(title: "+\(settings.threePointerPoints)",secondbotton: true, color: settings.team1Color, action: {
settings.team1Score += settings.threePointerPoints
checkForWinner()})
.fontWeight(.bold)
.opacity(0.5)
}
.background(Color(settings.team1Color))
.cornerRadius(30)
Text("\(settings.team1Score)")
.font(.system(size: 120))
.foregroundStyle(Color.white)
.fontWeight(.heavy)
.frame(height: .leastNormalMagnitude)
}
//TIMER
HStack {
ZStack{
Text("00:00")
.font(.system(size: 50,weight: .semibold))
.padding(.horizontal,20)
.padding(.vertical,5)
.foregroundStyle(Color.Resolved(colorSpace: .sRGB, red: 0.22, green: 0.22, blue: 0.22))
.dynamicTypeSize(.xLarge)
.padding(.leading)
Text(String(format: "%02d:%02d", settings.timeRemaining/60, settings.timeRemaining%60))
.font(.system(size: 50,weight: .semibold))
.padding(.horizontal,20)
.padding(.vertical,5)
.foregroundStyle(Color(.white))
.dynamicTypeSize(.xLarge)
.padding(.leading)
}
TimerControls(settings:settings,startAction: startTimer, pauseAction: pauseTimer, resetAction: resetTimer,timestarted: timestarted)
.padding(.trailing)
}
.background(Color.Resolved(colorSpace: .sRGB, red: 0.22, green: 0.22, blue: 0.22))
.cornerRadius(20)
//TEAM2
ZStack{
VStack {
ScoreButton(title: "+\(settings.pointsToAdd)",secondbotton: false, color: settings.team2Color) {
settings.team2Score += settings.pointsToAdd
checkForWinner()
}
.fontWeight(.bold)
.opacity(0.5)
ScoreButton(title: "+\(settings.threePointerPoints)",secondbotton: true, color: settings.team2Color, action: {
settings.team2Score += settings.threePointerPoints
checkForWinner()})
.fontWeight(.bold)
.opacity(0.5)
}
.background(Color(settings.team2Color))
.cornerRadius(30)
Text("\(settings.team2Score)")
.font(.system(size: 120))
.foregroundStyle(Color.white)
.fontWeight(.heavy)
.frame(height: .leastNormalMagnitude)
}
}
.padding(.horizontal,5)
if showingWinnerPopup {
WinnerPopup(
winnerTeam: winnerTeam ?? 0,
team1Color: settings.team1Color,
team2Color: settings.team2Color
) {
showingWinnerPopup = false
resetGame()
}
}
}
}
.onAppear {
settings.timeRemaining = settings.timerDuration
}
}
func startTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
if settings.timeRemaining > 0 {
settings.timeRemaining -= 1
checkForWinner()
} else {
timer?.invalidate()
checkForWinner()
}
}
timestarted=true
}
func pauseTimer() {
timer?.invalidate()
timestarted=false
}
func resetTimer() {
timer?.invalidate()
settings.timeRemaining = settings.timerDuration
timestarted=false
}
func checkForWinner() {
if settings.team1Score >= settings.winningScore {
winnerTeam = 1
showingWinnerPopup = true
timer?.invalidate()
} else if settings.team2Score >= settings.winningScore {
winnerTeam = 2
showingWinnerPopup = true
timer?.invalidate()
} else if settings.useExtendedWinningRule && settings.team1Score >= (settings.winningScore - settings.pointsToAdd) && settings.team2Score >= (settings.winningScore - settings.pointsToAdd) {
if savedWinningsScore == 0{
savedWinningsScore = settings.winningScore
}
settings.winningScore += settings.pointsToAdd
} else if settings.timeRemaining <= 0 {
if settings.team1Score > settings.team2Score {
winnerTeam = 1
} else if settings.team2Score > settings.team1Score {
winnerTeam = 2
} else {
winnerTeam = 0 // Tie
}
showingWinnerPopup = true
}
}
func newGame() {
resetGame()
showingWinnerPopup = false
}
func resetGame() {
settings.team1Score = 0
settings.team2Score = 0
resetTimer()
if savedWinningsScore != 0{
settings.winningScore = savedWinningsScore
}
}
}
struct BasketballScoreApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
#Preview {
ContentView()
}