Mercurial > public > geoquiz
comparison GeoQuiz/Logic/CityGame.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 |
---|---|
5 // Created by Dennis Concepción Martín on 29/9/22. | 5 // Created by Dennis Concepción Martín on 29/9/22. |
6 // | 6 // |
7 | 7 |
8 import Foundation | 8 import Foundation |
9 import AVFAudio | 9 import AVFAudio |
10 import SwiftUI | |
11 import MapKit | |
10 | 12 |
11 class CityGame: Game, ObservableObject { | 13 class CityGame: Game, ObservableObject { |
12 | 14 |
13 // Define type of generics | 15 // Define type of generics |
14 typealias T = CityModel.CityData | 16 typealias T = CityModel.CityData |
15 | 17 |
16 var data: [String: T] | 18 var data: [String: T] |
17 var dataAsked = [String: T]() | 19 var dataAsked = [String: T]() |
18 | 20 |
19 // Data | 21 @Published var mapImage: UIImage? = nil |
20 @Published var correctAnswer = (key: String(), value: T(country: String(), lat: Double(), lon: Double())) | 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 } | |
21 | 27 |
22 // User | 28 // User |
23 @Published var userChoices = [String: T]() | 29 @Published var userChoices = [String: T]() |
24 @Published var userScore = 0 | 30 @Published var userScore = 0 |
25 @Published var userLives = 3 | 31 @Published var userLives = 3 |
27 @Published var wrongAnswers = [String: T]() | 33 @Published var wrongAnswers = [String: T]() |
28 | 34 |
29 // Alerts | 35 // Alerts |
30 @Published var alertTitle = String() | 36 @Published var alertTitle = String() |
31 @Published var alertMessage = String() | 37 @Published var alertMessage = String() |
32 @Published var showingNoLivesAlert = false | 38 @Published var showingGameOverAlert = false |
33 @Published var showingEndGameAlert = false | 39 @Published var showingEndGameAlert = false |
34 @Published var showingWrongAnswerAlert = false | 40 @Published var showingWrongAnswerAlert = false |
35 @Published var showingExitGameAlert = false | 41 @Published var showingExitGameAlert = false |
36 | 42 |
37 // Animations | 43 // Animations |
38 @Published var scoreScaleAmount = 1.0 | 44 @Published var scoreScaleAmount = 1.0 |
39 @Published var livesScaleAmount = 1.0 | 45 @Published var livesScaleAmount = 1.0 |
40 | |
41 // Modal views | |
42 @Published var showingBuyLivesView = false | |
43 @Published var showingGameStatsView = false | |
44 | 46 |
45 // Sound effects | 47 // Sound effects |
46 @Published var player: AVAudioPlayer? | 48 @Published var player: AVAudioPlayer? |
47 | 49 |
48 init() { | 50 init() { |
49 let data: CityModel = load("cities.json") | 51 let data: CityModel = load("cities.json") |
50 self.data = data.cities | 52 self.data = data.cities |
51 askQuestion() | 53 askQuestion() |
52 } | 54 } |
53 } | 55 } |
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 |