comparison GeoQuiz/Logic/Game.swift @ 9:3540c7efc216

implement UserSettings
author Dennis C. M. <dennis@denniscm.com>
date Fri, 07 Oct 2022 18:50:38 +0200
parents e09959b4e4a8
children
comparison
equal deleted inserted replaced
8:e09959b4e4a8 9:3540c7efc216
60 60
61 selector() 61 selector()
62 } 62 }
63 63
64 func answer(_ choice: (key: String, value: T), selector: () -> Void) { 64 func answer(_ choice: (key: String, value: T), selector: () -> Void) {
65 var haptics = Haptics()
66
65 if correctAnswer == choice { 67 if correctAnswer == choice {
66 hapticSuccess() 68 haptics.success()
67 playSound("correctAnswer") 69 playSound("correctAnswer")
68 70
69 withAnimation(.easeIn(duration: 0.5)) { 71 withAnimation(.easeIn(duration: 0.5)) {
70 scoreScaleAmount += 1 72 scoreScaleAmount += 1
71 userScore += 1 73 userScore += 1
74 correctAnswers[correctAnswer.key] = correctAnswer.value 76 correctAnswers[correctAnswer.key] = correctAnswer.value
75 askQuestion { 77 askQuestion {
76 selector() 78 selector()
77 } 79 }
78 } else { 80 } else {
79 hapticError() 81 haptics.error()
80 playSound("wrongAnswer") 82 playSound("wrongAnswer")
81 83
82 withAnimation(.easeIn(duration: 0.5)) { 84 withAnimation(.easeIn(duration: 0.5)) {
83 livesScaleAmount += 1 85 livesScaleAmount += 1
84 userLives -= 1 86 userLives -= 1
115 selector() 117 selector()
116 } 118 }
117 } 119 }
118 120
119 private func playSound(_ filename: String) { 121 private func playSound(_ filename: String) {
120 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else { 122 let user = User()
121 fatalError("Sound file \(filename) couldn't be found")
122 }
123 123
124 do { 124 if user.settings.sound {
125 try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient) 125 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else {
126 try AVAudioSession.sharedInstance().setActive(true) 126 fatalError("Sound file \(filename) couldn't be found")
127 } catch { 127 }
128 fatalError("Couldn't activate session") 128
129 } 129 do {
130 130 try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
131 do { 131 try AVAudioSession.sharedInstance().setActive(true)
132 player = try AVAudioPlayer(contentsOf: soundFileURL) 132 } catch {
133 player?.play() 133 fatalError("Couldn't activate session")
134 } catch { 134 }
135 fatalError("Couldn't play sound effect") 135
136 do {
137 player = try AVAudioPlayer(contentsOf: soundFileURL)
138 player?.play()
139 } catch {
140 fatalError("Couldn't play sound effect")
141 }
136 } 142 }
137 } 143 }
138 } 144 }