comparison GeoQuiz/Logic/Game.swift @ 7:d945e52b0704

implement dynamic map
author Dennis C. M. <dennis@denniscm.com>
date Tue, 04 Oct 2022 18:54:24 +0200
parents 1946bbfde4af
children e09959b4e4a8
comparison
equal deleted inserted replaced
6:1946bbfde4af 7:d945e52b0704
27 var wrongAnswers: [String: T] { get set } 27 var wrongAnswers: [String: T] { get set }
28 28
29 // Alerts 29 // Alerts
30 var alertTitle: String { get set } 30 var alertTitle: String { get set }
31 var alertMessage: String { get set } 31 var alertMessage: String { get set }
32 var showingNoLivesAlert: Bool { get set } 32 var showingGameOverAlert: Bool { get set }
33 var showingEndGameAlert: Bool { get set } 33 var showingEndGameAlert: Bool { get set }
34 var showingWrongAnswerAlert: Bool { get set } 34 var showingWrongAnswerAlert: Bool { get set }
35 var showingExitGameAlert: Bool { get set } 35 var showingExitGameAlert: Bool { get set }
36 36
37 // Animations 37 // Animations
38 var scoreScaleAmount: Double { get set } 38 var scoreScaleAmount: Double { get set }
39 var livesScaleAmount: Double { get set } 39 var livesScaleAmount: Double { get set }
40
41 // Modal views
42 var showingBuyLivesView: Bool { get set }
43 var showingGameStatsView: Bool { get set }
44 40
45 // Sound effects 41 // Sound effects
46 var player: AVAudioPlayer? { get set } 42 var player: AVAudioPlayer? { get set }
47 } 43 }
48 44
51 dataAsked.count 47 dataAsked.count
52 } 48 }
53 49
54 func askQuestion() { 50 func askQuestion() {
55 guard questionCounter < data.count else { 51 guard questionCounter < data.count else {
56 alertTitle = "Amazing!" 52 alertTitle = "⭐️ Congratulations ⭐️"
57 alertMessage = "You've completed the game." 53 alertMessage = "You completed the game."
58 showingEndGameAlert = true 54 showingEndGameAlert = true
59 55
60 return 56 return
61 } 57 }
62 58
87 83
88 self.userChoices = userChoices 84 self.userChoices = userChoices
89 } 85 }
90 86
91 func answer(_ choice: (key: String, value: T)) { 87 func answer(_ choice: (key: String, value: T)) {
92 guard userLives > 0 else {
93 alertTitle = "Not enough lives!"
94 alertMessage = "Please buy more lives to keep playing"
95 showingNoLivesAlert = true
96
97 return
98 }
99
100 if correctAnswer == choice { 88 if correctAnswer == choice {
101 hapticSuccess() 89 hapticSuccess()
102 playSound("correctAnswer") 90 playSound("correctAnswer")
103 91
104 withAnimation(.easeIn(duration: 0.5)) { 92 withAnimation(.easeIn(duration: 0.5)) {
114 102
115 withAnimation(.easeIn(duration: 0.5)) { 103 withAnimation(.easeIn(duration: 0.5)) {
116 livesScaleAmount += 1 104 livesScaleAmount += 1
117 userLives -= 1 105 userLives -= 1
118 } 106 }
119
120 alertTitle = "Wrong!"
121 alertMessage = "You have \(userLives) lives left"
122 showingWrongAnswerAlert = true
123 107
124 wrongAnswers[choice.key] = choice.value 108 wrongAnswers[choice.key] = choice.value
109
110 if userLives == 0 {
111 alertTitle = "🤕 Game over 🤕"
112 alertMessage = "Get up and try again."
113 showingGameOverAlert = true
114 } else {
115 alertTitle = "🔴 Wrong 🔴"
116 alertMessage = "You have \(userLives) lives left."
117 showingWrongAnswerAlert = true
118 }
125 } 119 }
126 120
127 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in 121 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
128 withAnimation(.easeIn(duration: 0.5)) { 122 withAnimation(.easeIn(duration: 0.5)) {
129 scoreScaleAmount = 1 123 scoreScaleAmount = 1
130 livesScaleAmount = 1 124 livesScaleAmount = 1
131 } 125 }
132 } 126 }
127 }
128
129 func reset() {
130 dataAsked = [String: T]()
131 userScore = 0
132 userLives = 3
133 correctAnswers = [String: T]()
134 wrongAnswers = [String: T]()
135 askQuestion()
133 } 136 }
134 137
135 private func playSound(_ filename: String) { 138 private func playSound(_ filename: String) {
136 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else { 139 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else {
137 fatalError("Sound file \(filename) couldn't be found") 140 fatalError("Sound file \(filename) couldn't be found")