0
|
1 //
|
6
|
2 // CityGame.swift
|
0
|
3 // GeoQuiz
|
|
4 //
|
6
|
5 // Created by Dennis Concepción Martín on 29/9/22.
|
0
|
6 //
|
|
7
|
|
8 import Foundation
|
5
|
9 import AVFAudio
|
7
|
10 import SwiftUI
|
|
11 import MapKit
|
0
|
12
|
6
|
13 class CityGame: Game, ObservableObject {
|
|
14
|
3
|
15 // Define type of generics
|
6
|
16 typealias T = CityModel.CityData
|
|
17
|
|
18 var data: [String: T]
|
|
19 var dataAsked = [String: T]()
|
0
|
20
|
7
|
21 @Published var mapImage: UIImage? = nil
|
|
22 @Published var correctAnswer = (key: String(), value: T(country: String(), lat: Double(), lon: Double())) {
|
|
23 willSet {
|
|
24 getMapImage(lat: newValue.value.lat, lon: newValue.value.lon)
|
|
25 }
|
|
26 }
|
3
|
27
|
|
28 // User
|
6
|
29 @Published var userChoices = [String: T]()
|
0
|
30 @Published var userScore = 0
|
|
31 @Published var userLives = 3
|
6
|
32 @Published var correctAnswers = [String: T]()
|
|
33 @Published var wrongAnswers = [String: T]()
|
3
|
34
|
|
35 // Alerts
|
|
36 @Published var alertTitle = String()
|
|
37 @Published var alertMessage = String()
|
7
|
38 @Published var showingGameOverAlert = false
|
3
|
39 @Published var showingEndGameAlert = false
|
0
|
40 @Published var showingWrongAnswerAlert = false
|
4
|
41 @Published var showingExitGameAlert = false
|
0
|
42
|
3
|
43 // Animations
|
|
44 @Published var scoreScaleAmount = 1.0
|
|
45 @Published var livesScaleAmount = 1.0
|
|
46
|
5
|
47 // Sound effects
|
|
48 @Published var player: AVAudioPlayer?
|
|
49
|
0
|
50 init() {
|
6
|
51 let data: CityModel = load("cities.json")
|
|
52 self.data = data.cities
|
0
|
53 askQuestion()
|
|
54 }
|
|
55 }
|
7
|
56
|
|
57 extension CityGame {
|
|
58 func getMapImage(lat: Double, lon: Double) {
|
|
59 let region = MKCoordinateRegion(
|
|
60 center: CLLocationCoordinate2D(
|
|
61 latitude: lat,
|
|
62 longitude: lon
|
|
63 ),
|
|
64 span: MKCoordinateSpan(
|
|
65 latitudeDelta: 1.0,
|
|
66 longitudeDelta: 1.0
|
|
67 )
|
|
68 )
|
|
69
|
|
70 // Map options
|
|
71 let mapOptions = MKMapSnapshotter.Options()
|
|
72 mapOptions.region = region
|
|
73 mapOptions.size = CGSize(width: 600, height: 600)
|
|
74 mapOptions.showsBuildings = true
|
|
75
|
|
76 // Create the snapshotter and run it
|
|
77 let snapshotter = MKMapSnapshotter(options: mapOptions)
|
|
78 snapshotter.start { (snapshot, error) in
|
|
79 if let snapshot = snapshot {
|
|
80 self.mapImage = snapshot.image
|
|
81 } else if let error = error {
|
|
82 print(error.localizedDescription)
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86 }
|
|
87
|