Files
roblox-bot-player/web-demo/player.js
2026-02-20 08:53:27 -08:00

64 lines
1.8 KiB
JavaScript

import init, {
setup_graphics,
CompleteBot,
CompleteMap,
PlaybackHead,
} from "./pkg/strafesnet_roblox_bot_player_wasm_module.js";
await init(); // load the wasm module
const b = await fetch("bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot");
const m = await fetch("bhop_marble_5692093612.snfm");
const canvas = document.getElementById("viewport");
const graphics = await setup_graphics(canvas);
const bot = new CompleteBot(new Uint8Array(await b.arrayBuffer()));
const map = new CompleteMap(new Uint8Array(await m.arrayBuffer()));
const playback = new PlaybackHead(0);
graphics.change_map(map);
const startTime = performance.now();
const timer = document.getElementById("timer");
const duration = document.getElementById("duration");
const MODE_MAIN = 0;
function timer_text(t) {
const h = Math.floor(t / 3600);
const m = Math.floor((t % 3600) / 60);
const s = Math.floor(t % 60);
const ms = Math.floor((t % 1) * 1000);
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}.${String(ms).padStart(3, "0")}`;
}
duration.textContent = timer_text(bot.run_duration(MODE_MAIN));
function animate(now) {
const elapsedMs = now - startTime;
const elapsedSec = elapsedMs / 1000; // wasm expects seconds
// Advance the playback head to the current time
playback.advance_time(bot, elapsedSec);
// update the timer text
const time = playback.get_run_time(bot, elapsedSec, MODE_MAIN);
timer.textContent = timer_text(time);
// Render the frame that the bot is at that time
graphics.render(bot, playback, elapsedSec);
// Keep the loop going
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
function resize() {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
graphics.resize(canvas.width, canvas.height);
}
window.addEventListener("resize", resize);
resize();