-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermissionScreen.js
More file actions
219 lines (200 loc) · 7.26 KB
/
Copy pathpermissionScreen.js
File metadata and controls
219 lines (200 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// permissionScreen.js
/*
Permission management screen that handles:
- Location permission requests
- Compass permission requests
- Permission status display
- Fallback options
*/
/*jshint esversion: 8 */
let permissionScreen = {
needsLocation: true,
needsCompass: true,
message: "",
compassButton: null,
locationButton: null,
continueButton: null,
draw: function() {
if (DEV_MODE) {
startNewGame();
return;
}
if (drawCloseButton()) {
this.cleanup();
return;
}
// Title
textAlign(CENTER, CENTER);
noStroke();
textSize(32);
fill(getTextColor());
text("Permissions Needed", width/2, height/6);
if (window.isSecureContext) {
// Subtitle
textSize(20);
fill(getTextColor());
text("This game needs access to:", width/2, height/3 - 40);
this.createButtons();
} else {
textSize(20);
fill(255, 0, 0);
text("This game requires a secure connection (HTTPS).", width/2, height/3);
text("Please use HTTPS or localhost to play.", width/2, height/3 + 30);
}
// Update button colors but keep status indication
if (this.locationButton) {
const isGranted = localStorage.getItem('locationPermission') === 'granted';
this.locationButton.style('background-color', getPermissionStatusColor(isGranted));
this.locationButton.style('color', 'var(--text-color)');
this.locationButton.style('font-family', 'Helvetica, Arial, sans-serif');
}
if (this.compassButton) {
const isGranted = orientationService.hasPermission;
this.compassButton.style('background-color', getPermissionStatusColor(isGranted));
this.compassButton.style('color', 'var(--text-color)');
this.compassButton.style('font-family', 'Helvetica, Arial, sans-serif');
}
if (this.message) {
textSize(16);
fill(255, 0, 0);
textAlign(CENTER, CENTER);
text(this.message, width/2, height - 100);
}
// Only proceed to game if we have permissions and they were granted normally
if (playerLocation && orientationService.hasPermission &&
localStorage.getItem('locationPermission') === 'granted') {
this.cleanup();
currentQuestionIndex = 0;
resetLockStates();
pickNewQuestion();
goToScreen("game");
}
},
createButtons: function() {
const centerY = height/2 - 75; // Center point for buttons
const spacing = 70; // Space between buttons
if (this.needsLocation && !this.locationButton) {
this.locationButton = createButton('Location');
this.styleButton(this.locationButton, centerY);
this.locationButton.mousePressed(async () => {
this.locationButton.attribute('disabled', '');
try {
const position = await this.requestLocation();
playerLocation = {
lat: position.coords.latitude,
lon: position.coords.longitude
};
this.message = "";
this.locationButton.removeAttribute('disabled');
} catch (error) {
this.locationButton.removeAttribute('disabled');
this.message = "Location access denied. Please check browser settings.";
}
});
}
if (this.needsCompass && !this.compassButton) {
this.compassButton = createButton('Compass');
this.styleButton(this.compassButton, centerY + spacing);
this.compassButton.mousePressed(async () => {
this.compassButton.attribute('disabled', '');
this.compassButton.style('opacity', '0.5');
try {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
await new Promise(resolve => setTimeout(resolve, 100));
const response = await DeviceOrientationEvent.requestPermission();
if (response === 'granted') {
orientationService.hasPermission = true;
localStorage.setItem('compassPermission', 'granted');
window.addEventListener('deviceorientation', orientationService.handleOrientation.bind(orientationService),
{ capture: true, passive: true }
);
this.message = "";
} else {
throw new Error("Permission denied");
}
} else {
orientationService.hasPermission = true;
localStorage.setItem('compassPermission', 'granted');
}
} catch (error) {
this.message = "Please try again or check browser settings";
}
this.compassButton.removeAttribute('disabled');
this.compassButton.style('opacity', '1');
});
}
if (!this.continueButton && DEV_MODE) {
this.continueButton = createButton('Start without access');
this.styleButton(this.continueButton, centerY + spacing * 2);
this.continueButton.style('background-color', 'var(--button-bg)');
this.continueButton.style('color', 'var(--text-color)');
this.continueButton.mousePressed(() => {
if (!playerLocation) {
playerLocation = { lat: 0, lon: 0 };
}
orientationService.hasPermission = true;
localStorage.setItem('compassPermission', 'granted');
this.cleanup();
currentQuestionIndex = 0;
resetLockStates();
pickNewQuestion();
goToScreen("game"); // Continue directly to game
});
}
},
styleButton: function(button, yPosition) {
const offset = getCanvasOffset();
const btnWidth = 200;
button.style('font-size', '18px');
button.style('padding', '8px');
button.style('border', 'none');
button.style('border-radius', '10px');
button.style('cursor', 'pointer');
button.style('width', btnWidth + 'px');
button.style('height', '50px');
button.style('touch-action', 'auto');
button.style('pointer-events', 'auto');
button.style('z-index', '1000');
button.style('font-family', 'Helvetica, Arial, sans-serif');
button.style('transition', 'opacity 0.2s');
button.position(width/2 - btnWidth/2 + offset.x, yPosition);
button.elt.addEventListener('touchstart', function(e) {
e.stopPropagation();
}, { passive: true });
},
cleanup: function() {
if (this.locationButton) {
this.locationButton.remove();
this.locationButton = null;
}
if (this.compassButton) {
this.compassButton.remove();
this.compassButton = null;
}
if (this.continueButton) {
this.continueButton.remove();
this.continueButton = null;
}
},
requestLocation() {
return new Promise((resolve, reject) => {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
position => {
localStorage.setItem('locationPermission', 'granted');
resolve(position);
},
error => {
localStorage.setItem('locationPermission', 'denied');
reject(error);
}
);
} else {
reject(new Error("Geolocation not available"));
}
});
},
handleClick: function() {
// Called from mousePressed in main.js if needed
}
};