diff 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
line wrap: on
line diff
--- a/GeoQuiz/Logic/Game.swift	Thu Oct 06 11:14:34 2022 +0200
+++ b/GeoQuiz/Logic/Game.swift	Fri Oct 07 18:50:38 2022 +0200
@@ -62,8 +62,10 @@
     }
     
     func answer(_ choice: (key: String, value: T), selector: () -> Void) {
+        var haptics = Haptics()
+        
         if correctAnswer == choice {
-            hapticSuccess()
+            haptics.success()
             playSound("correctAnswer")
             
             withAnimation(.easeIn(duration: 0.5)) {
@@ -76,7 +78,7 @@
                 selector()
             }
         } else {
-            hapticError()
+            haptics.error()
             playSound("wrongAnswer")
 
             withAnimation(.easeIn(duration: 0.5)) {
@@ -117,22 +119,26 @@
     }
     
     private func playSound(_ filename: String) {
-        guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else {
-            fatalError("Sound file \(filename) couldn't be found")
-        }
+        let user = User()
         
-        do {
-            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
-            try AVAudioSession.sharedInstance().setActive(true)
-        } catch {
-            fatalError("Couldn't activate session")
-        }
-        
-        do {
-            player = try AVAudioPlayer(contentsOf: soundFileURL)
-            player?.play()
-        } catch {
-            fatalError("Couldn't play sound effect")
+        if user.settings.sound {
+            guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else {
+                fatalError("Sound file \(filename) couldn't be found")
+            }
+            
+            do {
+                try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
+                try AVAudioSession.sharedInstance().setActive(true)
+            } catch {
+                fatalError("Couldn't activate session")
+            }
+            
+            do {
+                player = try AVAudioPlayer(contentsOf: soundFileURL)
+                player?.play()
+            } catch {
+                fatalError("Couldn't play sound effect")
+            }
         }
     }
 }