Spaces:
Running
Running
apirrone commited on
Commit ยท
7ac5e77
1
Parent(s): b225791
Settle at neutral before accepting input; remove cheat-y sequence strip
Browse files- lib/motor.js +40 -2
- main.js +5 -2
- style.css +0 -18
- views/game.js +4 -16
lib/motor.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
// version uses `enable_motors([...])`/`disable_motors([...])` for the
|
| 8 |
// same thing.
|
| 9 |
|
| 10 |
-
import { sleep, Direction } from "./util.js";
|
| 11 |
|
| 12 |
// Wire-shape: head is row-major flat 4ร4. Identity = base pose.
|
| 13 |
const IDENTITY = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1];
|
|
@@ -59,6 +59,44 @@ export function createMotor({ robot }) {
|
|
| 59 |
await sleep(300 + 150); // return + small pause
|
| 60 |
}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
// Wake from sleep to neutral, then leave the left antenna free for
|
| 63 |
// difficulty selection.
|
| 64 |
async function wakeAndPrepareForSelection() {
|
|
@@ -104,7 +142,7 @@ export function createMotor({ robot }) {
|
|
| 104 |
}
|
| 105 |
|
| 106 |
return {
|
| 107 |
-
goto, showDirection, wakeAndPrepareForSelection,
|
| 108 |
lockLeftAntennaForGame, releaseLeftAntenna, rightAntennaTwitch,
|
| 109 |
TARGETS, NEUTRAL_TARGET,
|
| 110 |
};
|
|
|
|
| 7 |
// version uses `enable_motors([...])`/`disable_motors([...])` for the
|
| 8 |
// same thing.
|
| 9 |
|
| 10 |
+
import { sleep, Direction, flatMatrixToRpyDeg } from "./util.js";
|
| 11 |
|
| 12 |
// Wire-shape: head is row-major flat 4ร4. Identity = base pose.
|
| 13 |
const IDENTITY = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1];
|
|
|
|
| 59 |
await sleep(300 + 150); // return + small pause
|
| 60 |
}
|
| 61 |
|
| 62 |
+
// Block until the head + body + antennas are physically close to
|
| 63 |
+
// neutral. Important between the "showing" and "input" phases:
|
| 64 |
+
// without this, residual tilt from the last animation reads as a
|
| 65 |
+
// player input the instant we start listening, and the user loses
|
| 66 |
+
// without having moved.
|
| 67 |
+
//
|
| 68 |
+
// Resends a goto-neutral after a short timeout in case the daemon
|
| 69 |
+
// missed our previous one, then keeps polling. Bails out after
|
| 70 |
+
// `maxWaitMs` so we never deadlock.
|
| 71 |
+
async function settleAtNeutral({ maxWaitMs = 2500 } = {}) {
|
| 72 |
+
const HEAD_DEG = 4; // tighter than detector's 8ยฐ NEUTRAL_THRESHOLD
|
| 73 |
+
const BODY_DEG = 5;
|
| 74 |
+
const ANT_RAD = 0.15; // tighter than detector's 0.3 ANTENNA_THRESHOLD
|
| 75 |
+
const RETRY_AFTER_MS = 600;
|
| 76 |
+
const t0 = performance.now();
|
| 77 |
+
let lastResend = t0;
|
| 78 |
+
goto(NEUTRAL_TARGET, 0.4);
|
| 79 |
+
while (performance.now() - t0 < maxWaitMs) {
|
| 80 |
+
try { robot.requestState?.(); } catch {}
|
| 81 |
+
const rs = robot.robotState || {};
|
| 82 |
+
const { roll, pitch } = flatMatrixToRpyDeg(rs.head);
|
| 83 |
+
const bodyDeg = ((rs.body_yaw ?? 0) * 180) / Math.PI;
|
| 84 |
+
const ant = Array.isArray(rs.antennas) ? rs.antennas : [0, 0];
|
| 85 |
+
const ok = Math.abs(roll) < HEAD_DEG
|
| 86 |
+
&& Math.abs(pitch) < HEAD_DEG
|
| 87 |
+
&& Math.abs(bodyDeg) < BODY_DEG
|
| 88 |
+
&& Math.abs(ant[0]) < ANT_RAD
|
| 89 |
+
&& Math.abs(ant[1]) < ANT_RAD;
|
| 90 |
+
if (ok) return true;
|
| 91 |
+
if (performance.now() - lastResend > RETRY_AFTER_MS) {
|
| 92 |
+
goto(NEUTRAL_TARGET, 0.4);
|
| 93 |
+
lastResend = performance.now();
|
| 94 |
+
}
|
| 95 |
+
await sleep(60);
|
| 96 |
+
}
|
| 97 |
+
return false;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
// Wake from sleep to neutral, then leave the left antenna free for
|
| 101 |
// difficulty selection.
|
| 102 |
async function wakeAndPrepareForSelection() {
|
|
|
|
| 142 |
}
|
| 143 |
|
| 144 |
return {
|
| 145 |
+
goto, showDirection, settleAtNeutral, wakeAndPrepareForSelection,
|
| 146 |
lockLeftAntennaForGame, releaseLeftAntenna, rightAntennaTwitch,
|
| 147 |
TARGETS, NEUTRAL_TARGET,
|
| 148 |
};
|
main.js
CHANGED
|
@@ -187,8 +187,11 @@ async function runOneGame() {
|
|
| 187 |
audio.playSound(dir);
|
| 188 |
await motor.showDirection(dir);
|
| 189 |
}
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
// Wait for input
|
| 194 |
setStatus("input");
|
|
|
|
| 187 |
audio.playSound(dir);
|
| 188 |
await motor.showDirection(dir);
|
| 189 |
}
|
| 190 |
+
// Wait for the head/body/antennas to physically return to
|
| 191 |
+
// neutral before accepting input โ otherwise residual tilt from
|
| 192 |
+
// the last animation gets registered as the player's first move
|
| 193 |
+
// and they lose without having touched anything.
|
| 194 |
+
await motor.settleAtNeutral();
|
| 195 |
|
| 196 |
// Wait for input
|
| 197 |
setStatus("input");
|
style.css
CHANGED
|
@@ -159,24 +159,6 @@ button:active { transform: translateY(1px); }
|
|
| 159 |
100% { background: var(--panel); color: var(--text); transform: scale(1.0); box-shadow: 0 0 0 transparent; }
|
| 160 |
}
|
| 161 |
|
| 162 |
-
/* Sequence strip โ small dots along the bottom showing the moves so
|
| 163 |
-
* far, with progress through them during the player's turn. */
|
| 164 |
-
.seqstrip {
|
| 165 |
-
display: flex; gap: 6px; justify-content: center; flex-wrap: wrap;
|
| 166 |
-
padding: 14px; margin-top: 8px;
|
| 167 |
-
background: var(--panel);
|
| 168 |
-
border: 1px solid var(--border);
|
| 169 |
-
border-radius: 12px;
|
| 170 |
-
min-height: 24px;
|
| 171 |
-
}
|
| 172 |
-
.seqdot {
|
| 173 |
-
width: 14px; height: 14px;
|
| 174 |
-
border-radius: 50%;
|
| 175 |
-
opacity: 0.4;
|
| 176 |
-
transition: opacity 0.15s, transform 0.15s;
|
| 177 |
-
}
|
| 178 |
-
.seqdot.done { opacity: 1; transform: scale(1.15); }
|
| 179 |
-
|
| 180 |
/* โโโ Leaderboard โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ */
|
| 181 |
|
| 182 |
.leaderboard {
|
|
|
|
| 159 |
100% { background: var(--panel); color: var(--text); transform: scale(1.0); box-shadow: 0 0 0 transparent; }
|
| 160 |
}
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
/* โโโ Leaderboard โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ */
|
| 163 |
|
| 164 |
.leaderboard {
|
views/game.js
CHANGED
|
@@ -79,8 +79,6 @@ export function createGameView() {
|
|
| 79 |
</div>
|
| 80 |
</section>
|
| 81 |
|
| 82 |
-
<footer class="seqstrip" id="seqstrip"></footer>
|
| 83 |
-
|
| 84 |
<section class="leaderboard" id="leaderboard">
|
| 85 |
<div class="lb-head">
|
| 86 |
<span class="group-title">๐ Leaderboard <span class="hint" id="lb-diff">D1</span></span>
|
|
@@ -180,20 +178,10 @@ export function createGameView() {
|
|
| 180 |
setTimeout(() => el.classList.remove("lit"), 500);
|
| 181 |
}
|
| 182 |
|
| 183 |
-
//
|
| 184 |
-
//
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
if (!strip) return;
|
| 188 |
-
strip.innerHTML = "";
|
| 189 |
-
sequence.forEach((dir, i) => {
|
| 190 |
-
const dot = document.createElement("span");
|
| 191 |
-
dot.className = "seqdot";
|
| 192 |
-
dot.style.background = DIRECTION_COLORS[dir];
|
| 193 |
-
if (i < progress) dot.classList.add("done");
|
| 194 |
-
strip.appendChild(dot);
|
| 195 |
-
});
|
| 196 |
-
}
|
| 197 |
|
| 198 |
return {
|
| 199 |
GameView, setDifficulty, setScore, setStatus,
|
|
|
|
| 79 |
</div>
|
| 80 |
</section>
|
| 81 |
|
|
|
|
|
|
|
| 82 |
<section class="leaderboard" id="leaderboard">
|
| 83 |
<div class="lb-head">
|
| 84 |
<span class="group-title">๐ Leaderboard <span class="hint" id="lb-diff">D1</span></span>
|
|
|
|
| 178 |
setTimeout(() => el.classList.remove("lit"), 500);
|
| 179 |
}
|
| 180 |
|
| 181 |
+
// No-op: the colored sequence strip used to live here. Removed
|
| 182 |
+
// because the per-direction colors leaked the upcoming moves and
|
| 183 |
+
// made the game trivially solvable from the screen alone.
|
| 184 |
+
function renderSequenceStrip() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
return {
|
| 187 |
GameView, setDifficulty, setScore, setStatus,
|