-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype2.html
More file actions
315 lines (295 loc) · 10.3 KB
/
prototype2.html
File metadata and controls
315 lines (295 loc) · 10.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bakeoff 1</title>
<style type="text/css">
body {
height: 100vh;
width: 100vw;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* Show system cursor for reference */
cursor: none;
}
body.active {
border-left: 3px #f06 solid;
}
footer {
width: 100vw;
text-align: center;
position: absolute;
bottom: 0;
padding: 0.75em;
border-top: 1px #ddd solid;
}
svg {
border: 1px #ddd solid;
}
#header {
position: fixed;
top: 10px;
left: 50%;
transform: translateX(-50%);
text-align: center;
}
#timer, #finalScore {
font-size: 24px;
margin-top: 5px;
}
#customCursor {
position: fixed;
z-index: 1000;
width: 20px;
height: 20px;
background: red;
border-radius: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
}
</style>
<!-- svg.js library -->
<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<!-- Bakeoff framework -->
<script src="https://dhcs-s25-bakeoff1.glitch.me/framework.js"></script>
</head>
<body>
<div id="header">
<div id="timer">Time: 0.00 s</div>
<div id="finalScore">Score:</div>
</div>
<div id="main"></div>
<div id="customCursor"></div>
<script type="text/javascript">
/********************************
* Global Variables & Setup
********************************/
const tasksLength = 10;
let targets = [];
let isGameRunning = false;
let misClicks = 0;
let timerInterval;
let startTime;
let activeSquare = null;
let lastSquare = null;
let connectingLine = null;
// Use ignoreClick as a dictionary keyed by task number.
let ignoreClick = {};
// New task counter.
let currentTask = 0;
// Record the time a click occurred.
let clickTime = 0;
// Create SVG canvas (constants from framework: canvasSize, numberOfSquaresWide, numberOfSquaresTall, buttonSize, padding, margin)
let svg = SVG().addTo('#main').size(canvasSize, canvasSize);
// Initialize squares with stored original data.
for (let i = 0; i < numberOfSquaresTall * numberOfSquaresWide; i++) {
let x = (i % numberOfSquaresWide) * (padding + buttonSize) + margin;
let y = Math.floor(i / numberOfSquaresWide) * (padding + buttonSize) + margin;
let square = svg.rect(buttonSize, buttonSize);
square.move(x, y);
square.fill("#ccc");
square.data('origX', x);
square.data('origY', y);
square.data('origSize', buttonSize);
square.data('index', i);
// Set transform-origin to center.
square.attr({ "transform-origin": "50% 50%" });
targets[i] = square;
}
// Initialize Judge.
const judge = new Judge(tasksLength, targets, "teamName");
const timerDiv = document.getElementById("timer");
const finalScoreDiv = document.getElementById("finalScore");
const customCursor = document.getElementById("customCursor");
/********************************
* Custom Cursor Logic (No Enlargement)
********************************/
let mouseX = window.innerWidth / 2,
mouseY = window.innerHeight / 2,
targetX = window.innerWidth / 2,
targetY = window.innerHeight / 2,
cursorX = window.innerWidth / 2,
cursorY = window.innerHeight / 2;
function updateCursorPosition(x, y) {
customCursor.style.left = x + "px";
customCursor.style.top = y + "px";
}
updateCursorPosition(cursorX, cursorY);
let lastMoveTime = 0;
document.addEventListener("mousemove", (e) => {
const now = Date.now();
if (now - lastMoveTime > 16) {
lastMoveTime = now;
mouseX = e.clientX;
mouseY = e.clientY;
// Snap custom cursor target to the nearest square center if within 50px,
// otherwise follow the mouse.
if (isGameRunning) {
const svgRect = svg.node.getBoundingClientRect();
let closest = null;
let minDist = Infinity;
targets.forEach((sq) => {
const origX = sq.data('origX');
const origY = sq.data('origY');
const centerX = svgRect.left + origX + buttonSize / 2;
const centerY = svgRect.top + origY + buttonSize / 2;
const d = Math.hypot(mouseX - centerX, mouseY - centerY);
if (d < minDist) {
minDist = d;
closest = { x: centerX, y: centerY };
}
});
if (closest && minDist < 50) {
targetX = closest.x;
targetY = closest.y;
} else {
targetX = mouseX;
targetY = mouseY;
}
} else {
targetX = mouseX;
targetY = mouseY;
}
}
});
document.addEventListener("click", (e) => {
// Only handle native (user-initiated) clicks.
if (!e.isTrusted || ignoreClick[currentTask]) {
// Reset ignoreClick for this task.
ignoreClick[currentTask] = false;
return;
}
console.log("Native click received.");
});
function animateCursor() {
const dx = targetX - cursorX;
const dy = targetY - cursorY;
if (Math.hypot(dx, dy) < 1) {
cursorX = targetX;
cursorY = targetY;
} else {
cursorX += dx * 0.2;
cursorY += dy * 0.2;
}
updateCursorPosition(cursorX, cursorY);
requestAnimationFrame(animateCursor);
}
animateCursor();
/********************************
* Judge Event Handlers
********************************/
judge.on("start", () => {
isGameRunning = true;
misClicks = 0;
currentTask = 0;
// Initialize ignoreClick dictionary for all tasks.
for (let i = 0; i < tasksLength; i++) {
ignoreClick[i] = false;
}
startTime = Date.now();
timerInterval = setInterval(() => {
let elapsed = Date.now() - startTime;
timerDiv.innerText = "Time: " + (elapsed / 1000).toFixed(2) + " s";
}, 50);
console.log("Game started.");
setTimeout(() => {
if (judge.events["newTask"]) judge.events["newTask"]();
}, 10);
});
judge.on("reset", () => {
if (timerInterval) clearInterval(timerInterval);
isGameRunning = true;
misClicks = 0;
currentTask = 0;
for (let i = 0; i < tasksLength; i++) {
ignoreClick[i] = false;
}
startTime = Date.now();
timerDiv.innerText = "Time: 0.00 s";
finalScoreDiv.innerText = "Score:";
targets.forEach(sq => {
sq.fill("#ccc");
});
if (connectingLine) {
connectingLine.remove();
connectingLine = null;
}
lastSquare = null;
activeSquare = null;
console.log("Reset event triggered. Next squares:", judge.getNextTwoTasks());
targetX = window.innerWidth / 2;
targetY = window.innerHeight / 2;
cursorX = targetX;
cursorY = window.innerHeight / 2;
updateCursorPosition(cursorX, cursorY);
setTimeout(() => {
if (judge.events["newTask"]) judge.events["newTask"]();
}, 10);
});
judge.on("newTask", () => {
if (!isGameRunning) return;
if (connectingLine) {
connectingLine.remove();
connectingLine = null;
}
targets.forEach(sq => {
sq.fill("#ccc");
});
// Tie ignoreClick to the current task.
ignoreClick[currentTask] = true;
setTimeout(() => { ignoreClick[currentTask] = false; }, 0);
let nextSquareIndex = judge.getNextTwoTasks()[0];
if (typeof nextSquareIndex !== "undefined") {
if (lastSquare) {
let oldBox = lastSquare.bbox();
let newBox = targets[nextSquareIndex].bbox();
connectingLine = svg.line(oldBox.cx, oldBox.cy, newBox.cx, newBox.cy)
.stroke({ color: '#1F51FF', width: 6 })
.attr({ "pointer-events": "none" });
}
activeSquare = targets[nextSquareIndex];
activeSquare.fill("#f06");
activeSquare.front();
lastSquare = activeSquare;
}
console.log("New task event. Next squares:", judge.getNextTwoTasks());
});
judge.on("correctSquare", () => {
if (connectingLine) {
connectingLine.remove();
connectingLine = null;
}
// When a correct square is registered, tie ignoreClick for this task.
ignoreClick[currentTask] = true;
console.log("Correct square clicked.");
});
judge.on("wrongSquare", () => {
misClicks++;
console.log("Wrong square clicked. Total mis-clicks:", misClicks);
});
judge.on("stop", () => {
if (!isGameRunning) return;
isGameRunning = false;
clearInterval(timerInterval);
targets.forEach(sq => { /* No resizing needed */ });
let elapsed = Date.now() - startTime;
let finalScore = (elapsed / 1000) * (1 + (0.1 * misClicks));
finalScoreDiv.innerText = "Score: " + finalScore.toFixed(2);
console.log("Game stopped. Final score:", finalScore.toFixed(2));
});
judge.on("testOver", () => {
if (!isGameRunning) return;
isGameRunning = false;
clearInterval(timerInterval);
targets.forEach(sq => { /* No resizing needed */ });
let elapsed = Date.now() - startTime;
let finalScore = (elapsed / 1000) * (1 + (0.1 * misClicks));
finalScoreDiv.innerText = "Score: " + finalScore.toFixed(2);
console.log("Test over. Final score:", finalScore.toFixed(2));
});
</script>
</body>
</html>