2024.js/island.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-01-13 16:34:25 +00:00
import { IslandGrid } from "./island/grid";
import { renderIslands } from "./island/render";
2024-01-13 05:29:49 +00:00
import { canvas2d, h } from "./lib/html";
2024-01-13 01:46:09 +00:00
2024-01-13 04:58:12 +00:00
const BLOWUP = 4;
const WIDTH = 240;
const HEIGHT = 135;
2024-01-13 01:21:58 +00:00
const DEFAULT_SEED = 128;
2024-01-13 01:21:58 +00:00
export function IslandApplet() {
2024-01-13 15:09:33 +00:00
// STATE
let timerId: number;
2024-01-13 19:46:04 +00:00
let ticks = 0;
2024-01-13 15:09:33 +00:00
let islands = new IslandGrid(WIDTH, HEIGHT, DEFAULT_SEED);
// UI
2024-01-13 01:46:09 +00:00
const [canvas, cx] = canvas2d({
2024-01-13 01:21:58 +00:00
width: WIDTH * BLOWUP,
height: HEIGHT * BLOWUP,
2024-01-13 01:46:09 +00:00
});
2024-01-13 01:21:58 +00:00
cx.scale(BLOWUP, BLOWUP);
const seedInput = h("input", { type: "number", valueAsNumber: DEFAULT_SEED });
2024-01-13 05:29:49 +00:00
const seedLabel = h("label", {}, "Seed:", seedInput);
const generateButton = h(
"button",
{
onclick: () => {
clearInterval(timerId);
2024-01-13 19:46:04 +00:00
ticks = 0;
2024-01-13 05:29:49 +00:00
islands = new IslandGrid(WIDTH, HEIGHT, seedInput.valueAsNumber);
timerId = setInterval(function tick() {
islands.step();
islands.step();
islands.step();
2024-01-13 19:46:04 +00:00
ticks += 3;
if (islands.done) {
clearInterval(timerId);
islands.deepenWater();
}
renderIslands(islands, cx);
}, 1000 / 30);
2024-01-13 05:29:49 +00:00
},
},
"Generate"
);
2024-01-13 03:31:48 +00:00
2024-01-13 05:29:49 +00:00
renderIslands(islands, cx);
2024-01-13 01:21:58 +00:00
2024-01-13 05:29:49 +00:00
return [canvas, seedLabel, generateButton];
2024-01-13 01:21:58 +00:00
}
Object.assign(globalThis, { IslandApplet });