@adatechnology/game-interactables 0.6.11 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/dist/index.d.ts +125 -2
- package/dist/index.global.js +283 -2
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +283 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -194,7 +194,31 @@ var locales = {
|
|
|
194
194
|
needs_ignition: "Light it first",
|
|
195
195
|
turn_on_first: "Turn it on first",
|
|
196
196
|
empty: "Empty",
|
|
197
|
-
full: "Full"
|
|
197
|
+
full: "Full",
|
|
198
|
+
"use.put_in": "Put {item} in {name}",
|
|
199
|
+
"use.put_in.none": "Put what you hold in {name}",
|
|
200
|
+
"use.empty": "Empty {name}",
|
|
201
|
+
"use.fill": "Fill the {bowl} bowl",
|
|
202
|
+
"use.program.stop": "Stop {name} ({left} left)",
|
|
203
|
+
"use.relieve.urine": "Pee",
|
|
204
|
+
"use.relieve.feces": "Poo",
|
|
205
|
+
"bowl.food": "food",
|
|
206
|
+
"bowl.water": "water",
|
|
207
|
+
hand_empty: "Your hand is empty",
|
|
208
|
+
not_fit: "{item} does not fit in {name}",
|
|
209
|
+
not_accepted: "{item} does not go in the {bowl} bowl",
|
|
210
|
+
bowl_full: "The {bowl} bowl is already full",
|
|
211
|
+
needs_closed: "Close it first",
|
|
212
|
+
already_soiled: "Flush first",
|
|
213
|
+
done_put_in: "{item} went into {name}",
|
|
214
|
+
done_empty: "{name} emptied ({n} things)",
|
|
215
|
+
done_fill: "The {bowl} bowl is full",
|
|
216
|
+
done_program: "{name}: {program} started ({time})",
|
|
217
|
+
done_program_end: "{name}: {program} finished",
|
|
218
|
+
done_program_stop: "{name} stopped",
|
|
219
|
+
done_relieve_urine: "You peed in {name}",
|
|
220
|
+
done_relieve_feces: "You pooed in {name}",
|
|
221
|
+
done_clean: "{name} flushed clean"
|
|
198
222
|
},
|
|
199
223
|
"pt-BR": {
|
|
200
224
|
"prompt.open": "Abrir {name}",
|
|
@@ -221,7 +245,31 @@ var locales = {
|
|
|
221
245
|
needs_ignition: "Acenda primeiro",
|
|
222
246
|
turn_on_first: "Ligue primeiro",
|
|
223
247
|
empty: "Vazio",
|
|
224
|
-
full: "Cheio"
|
|
248
|
+
full: "Cheio",
|
|
249
|
+
"use.put_in": "P\xF4r {item} em {name}",
|
|
250
|
+
"use.put_in.none": "P\xF4r o que est\xE1 na m\xE3o em {name}",
|
|
251
|
+
"use.empty": "Esvaziar {name}",
|
|
252
|
+
"use.fill": "Encher o pote de {bowl}",
|
|
253
|
+
"use.program.stop": "Parar {name} (faltam {left})",
|
|
254
|
+
"use.relieve.urine": "Fazer xixi (1)",
|
|
255
|
+
"use.relieve.feces": "Fazer coc\xF4 (2)",
|
|
256
|
+
"bowl.food": "ra\xE7\xE3o",
|
|
257
|
+
"bowl.water": "\xE1gua",
|
|
258
|
+
hand_empty: "A m\xE3o est\xE1 vazia",
|
|
259
|
+
not_fit: "{item} n\xE3o cabe em {name}",
|
|
260
|
+
not_accepted: "{item} n\xE3o vai no pote de {bowl}",
|
|
261
|
+
bowl_full: "O pote de {bowl} j\xE1 est\xE1 cheio",
|
|
262
|
+
needs_closed: "Feche primeiro",
|
|
263
|
+
already_soiled: "D\xEA a descarga primeiro",
|
|
264
|
+
done_put_in: "{item} foi para dentro de {name}",
|
|
265
|
+
done_empty: "{name} esvaziado ({n} coisas)",
|
|
266
|
+
done_fill: "O pote de {bowl} est\xE1 cheio",
|
|
267
|
+
done_program: "{name}: {program} come\xE7ou ({time})",
|
|
268
|
+
done_program_end: "{name}: {program} terminou",
|
|
269
|
+
done_program_stop: "{name} parou",
|
|
270
|
+
done_relieve_urine: "Voc\xEA fez xixi em {name}",
|
|
271
|
+
done_relieve_feces: "Voc\xEA fez coc\xF4 em {name}",
|
|
272
|
+
done_clean: "Descarga dada: {name} limpo"
|
|
225
273
|
}
|
|
226
274
|
};
|
|
227
275
|
function pickLocale(locale) {
|
|
@@ -2236,15 +2284,248 @@ function attachLeaf(world, options, request) {
|
|
|
2236
2284
|
);
|
|
2237
2285
|
return createLeafDriver({ world, leaf, frame, ownsFrame, joint, onDispose: request.onDispose, drive: { isHinge, axis, pivot, centre, range, speed, base: rotation } });
|
|
2238
2286
|
}
|
|
2287
|
+
|
|
2288
|
+
// src/fits-volume.ts
|
|
2289
|
+
function fitsInVolume(size, half) {
|
|
2290
|
+
const thing = [...size].sort((a, b) => b - a);
|
|
2291
|
+
const room = half.map((h) => h * 2).sort((a, b) => b - a);
|
|
2292
|
+
return thing.every((side, i) => side <= (room[i] ?? 0));
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
// src/object-uses.ts
|
|
2296
|
+
var OPEN = 0.5;
|
|
2297
|
+
var PREFIX = "use.";
|
|
2298
|
+
var SOIL_RANK = { clean: 0, urine: 1, feces: 2 };
|
|
2299
|
+
var clock = (seconds) => {
|
|
2300
|
+
const s = Math.max(0, Math.ceil(seconds));
|
|
2301
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
2302
|
+
};
|
|
2303
|
+
var accepts = (bowl, hand) => (bowl.accepts ?? []).some((id) => hand.itemId === id || hand.itemId.endsWith(`_${id}`));
|
|
2304
|
+
var YES = { available: true };
|
|
2305
|
+
var no = (reason) => ({ available: false, reason });
|
|
2306
|
+
function createUses(options) {
|
|
2307
|
+
const { spec, world, objectId } = options;
|
|
2308
|
+
const all = spec.uses ?? [];
|
|
2309
|
+
const containers = new Map((spec.containers ?? []).map((c) => [c.id, c]));
|
|
2310
|
+
const bowls = new Map((spec.bowls ?? []).map((b) => [b.id, b]));
|
|
2311
|
+
const level = new Map((spec.bowls ?? []).map((b) => [b.id, Math.min(b.capacity, b.level ?? 0)]));
|
|
2312
|
+
const soil = new Map((spec.basins ?? []).filter((b) => b.soils).map((b) => [b.id, "clean"]));
|
|
2313
|
+
const cleans = new Map((spec.fixtures ?? []).flatMap((f) => f.cleans ? [[f.id, f.cleans]] : []));
|
|
2314
|
+
let program = null;
|
|
2315
|
+
let lastHost = null;
|
|
2316
|
+
const typeKey = `type.${spec.type}`;
|
|
2317
|
+
const name = () => {
|
|
2318
|
+
const text = world.t(typeKey);
|
|
2319
|
+
return text === typeKey ? spec.type.replace(/_/g, " ") : text.charAt(0).toLowerCase() + text.slice(1);
|
|
2320
|
+
};
|
|
2321
|
+
const accessOpen = (host, part) => !part || host.partOpening(part) >= OPEN;
|
|
2322
|
+
const verdict = (use, host) => {
|
|
2323
|
+
const hand = host.hand();
|
|
2324
|
+
switch (use.do) {
|
|
2325
|
+
case "program":
|
|
2326
|
+
if (!world.power.isPowered(objectId, use.device)) return no("no_power");
|
|
2327
|
+
return use.access && host.partOpening(use.access) >= OPEN ? no("needs_closed") : YES;
|
|
2328
|
+
case "put_in": {
|
|
2329
|
+
const c = containers.get(use.container);
|
|
2330
|
+
if (!hand) return no("hand_empty");
|
|
2331
|
+
if (!c || !accessOpen(host, c.access)) return no("requires_open");
|
|
2332
|
+
return fitsInVolume(hand.size, c.volume.half) ? YES : no("not_fit");
|
|
2333
|
+
}
|
|
2334
|
+
case "empty":
|
|
2335
|
+
return accessOpen(host, containers.get(use.container)?.access) ? YES : no("requires_open");
|
|
2336
|
+
case "fill": {
|
|
2337
|
+
const bowl = bowls.get(use.bowl);
|
|
2338
|
+
if (!bowl) return no("empty");
|
|
2339
|
+
if ((level.get(bowl.id) ?? 0) >= bowl.capacity - 1e-6) return no("bowl_full");
|
|
2340
|
+
if (!hand) return no("hand_empty");
|
|
2341
|
+
if (bowl.holds === "water" && hand.liquid?.kind === "water" && hand.liquid.closed) return no("requires_open");
|
|
2342
|
+
const fits = bowl.holds === "water" ? hand.liquid?.kind === "water" && hand.liquid.litres > 0 : accepts(bowl, hand);
|
|
2343
|
+
return fits ? YES : no("not_accepted");
|
|
2344
|
+
}
|
|
2345
|
+
case "relieve":
|
|
2346
|
+
return YES;
|
|
2347
|
+
}
|
|
2348
|
+
};
|
|
2349
|
+
const textVars = (use, host) => ({
|
|
2350
|
+
name: name(),
|
|
2351
|
+
item: host.hand()?.name ?? "",
|
|
2352
|
+
bowl: world.t(`bowl.${use.do === "fill" ? bowls.get(use.bowl)?.holds ?? "food" : "food"}`)
|
|
2353
|
+
});
|
|
2354
|
+
const labelOf = (use, host) => world.t(use.do === "put_in" && !host.hand() ? "use.put_in.none" : use.label, textVars(use, host));
|
|
2355
|
+
const runUse = (use, host) => {
|
|
2356
|
+
const vars = textVars(use, host);
|
|
2357
|
+
switch (use.do) {
|
|
2358
|
+
case "program": {
|
|
2359
|
+
world.setDevice(objectId, use.device, true);
|
|
2360
|
+
program = { use, left: use.seconds };
|
|
2361
|
+
return { ok: true, text: world.t("done_program", { ...vars, program: world.t(use.label), time: clock(use.seconds) }) };
|
|
2362
|
+
}
|
|
2363
|
+
case "put_in": {
|
|
2364
|
+
const c = containers.get(use.container);
|
|
2365
|
+
return host.placeHand(c.volume.center) ? { ok: true, text: world.t("done_put_in", vars) } : { ok: false, text: world.t("not_fit", vars) };
|
|
2366
|
+
}
|
|
2367
|
+
case "empty": {
|
|
2368
|
+
const c = containers.get(use.container);
|
|
2369
|
+
const [cx, cy, cz] = c.volume.center;
|
|
2370
|
+
const [hx, hy, hz] = c.volume.half;
|
|
2371
|
+
const n = host.emptyInside([cx - hx, cy - hy, cz - hz], [cx + hx, cy + hy, cz + hz]);
|
|
2372
|
+
return { ok: n > 0, text: world.t(n > 0 ? "done_empty" : "empty", { ...vars, n }) };
|
|
2373
|
+
}
|
|
2374
|
+
case "fill":
|
|
2375
|
+
return fill(bowls.get(use.bowl), host, vars);
|
|
2376
|
+
case "relieve": {
|
|
2377
|
+
const now = soil.get(use.basin) ?? "clean";
|
|
2378
|
+
soil.set(use.basin, SOIL_RANK[use.kind] > SOIL_RANK[now] ? use.kind : now);
|
|
2379
|
+
return { ok: true, text: world.t(`done_relieve_${use.kind}`, vars) };
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
};
|
|
2383
|
+
const fill = (bowl, host, vars) => {
|
|
2384
|
+
const now = level.get(bowl.id) ?? 0;
|
|
2385
|
+
const added = bowl.holds === "water" ? host.pourHand(bowl.capacity - now) : bowl.capacity - now;
|
|
2386
|
+
level.set(bowl.id, Math.min(bowl.capacity, now + added));
|
|
2387
|
+
return { ok: added > 0, text: world.t(added > 0 ? "done_fill" : "not_accepted", vars) };
|
|
2388
|
+
};
|
|
2389
|
+
const stopProgram = () => {
|
|
2390
|
+
if (!program) return "";
|
|
2391
|
+
world.setDevice(objectId, program.use.device, false);
|
|
2392
|
+
program = null;
|
|
2393
|
+
return world.t("done_program_stop", { name: name() });
|
|
2394
|
+
};
|
|
2395
|
+
const visible = (use) => use.do !== "program" || program === null;
|
|
2396
|
+
return {
|
|
2397
|
+
has: (id) => id.startsWith(PREFIX),
|
|
2398
|
+
list(host) {
|
|
2399
|
+
lastHost = host;
|
|
2400
|
+
const entries = [];
|
|
2401
|
+
if (program) entries.push({ id: `${PREFIX}stop`, label: world.t("use.program.stop", { name: name(), left: clock(program.left) }), available: true });
|
|
2402
|
+
for (const use of all.filter(visible)) {
|
|
2403
|
+
const v = verdict(use, host);
|
|
2404
|
+
entries.push({ id: `${PREFIX}${use.id}`, label: labelOf(use, host), available: v.available, ...v.available ? {} : { reason: world.t(v.reason, textVars(use, host)) } });
|
|
2405
|
+
}
|
|
2406
|
+
return entries;
|
|
2407
|
+
},
|
|
2408
|
+
run(id, host) {
|
|
2409
|
+
lastHost = host;
|
|
2410
|
+
if (id === `${PREFIX}stop`) return { ok: program !== null, text: stopProgram() };
|
|
2411
|
+
const use = all.find((u) => `${PREFIX}${u.id}` === id);
|
|
2412
|
+
if (!use || !visible(use)) return { ok: false, text: "" };
|
|
2413
|
+
const v = verdict(use, host);
|
|
2414
|
+
return v.available ? runUse(use, host) : { ok: false, text: world.t(v.reason, textVars(use, host)) };
|
|
2415
|
+
},
|
|
2416
|
+
update(dt) {
|
|
2417
|
+
if (!program) return;
|
|
2418
|
+
program.left -= dt;
|
|
2419
|
+
if (program.left > 0) return;
|
|
2420
|
+
const done = world.t("done_program_end", { name: name(), program: world.t(program.use.label) });
|
|
2421
|
+
world.setDevice(objectId, program.use.device, false);
|
|
2422
|
+
program = null;
|
|
2423
|
+
lastHost?.say(done);
|
|
2424
|
+
},
|
|
2425
|
+
flushed(fixture) {
|
|
2426
|
+
const basin = cleans.get(fixture);
|
|
2427
|
+
if (!basin || soil.get(basin) === "clean") return;
|
|
2428
|
+
soil.set(basin, "clean");
|
|
2429
|
+
lastHost?.say(world.t("done_clean", { name: name() }));
|
|
2430
|
+
},
|
|
2431
|
+
state: () => ({
|
|
2432
|
+
bowls: Object.fromEntries(level),
|
|
2433
|
+
soil: Object.fromEntries(soil),
|
|
2434
|
+
program: program ? { use: program.use.id, device: program.use.device, left: program.left } : null
|
|
2435
|
+
})
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
// src/object-controls.ts
|
|
2440
|
+
var CONTROLLED_OBJECT = "object";
|
|
2441
|
+
var MAINS = "mains";
|
|
2442
|
+
var MAINS_TANK = "water";
|
|
2443
|
+
var HOST_KINDS = /* @__PURE__ */ new Set(["toggle_part", "open_container", "take", "read", "sit", "toggle_breaker"]);
|
|
2444
|
+
var PLUG_FALLBACK = [0, 0.3, 0];
|
|
2445
|
+
function mainsSpec(spec) {
|
|
2446
|
+
const plugged = (spec.devices ?? []).filter((d) => d.socket !== void 0 && !d.circuit);
|
|
2447
|
+
return { type: MAINS, sockets: plugged.map((d, i) => ({ id: `socket_${i}`, pos: d.plug ?? PLUG_FALLBACK })), tanks: [{ id: MAINS_TANK, capacity: 1e9 }] };
|
|
2448
|
+
}
|
|
2449
|
+
function createObjectControls(options) {
|
|
2450
|
+
const { spec } = options;
|
|
2451
|
+
const forwarded = /* @__PURE__ */ new Map();
|
|
2452
|
+
const said = [];
|
|
2453
|
+
let host = null;
|
|
2454
|
+
const world = createWorld({
|
|
2455
|
+
locale: options.locale ?? "en",
|
|
2456
|
+
...options.strings ? { strings: options.strings } : {},
|
|
2457
|
+
grid: options.mains?.power ?? true,
|
|
2458
|
+
takeContents: false,
|
|
2459
|
+
onEvent: (event) => {
|
|
2460
|
+
if (event.type === "message") said.push(event.text);
|
|
2461
|
+
if (event.type === "part" && event.object === CONTROLLED_OBJECT) forwardPart(event.part, event.target >= 0.5);
|
|
2462
|
+
if (event.type === "fixture" && event.object === CONTROLLED_OBJECT && event.flowing) uses.flushed(event.fixture);
|
|
2463
|
+
}
|
|
2464
|
+
});
|
|
2465
|
+
const forwardPart = (part, open) => {
|
|
2466
|
+
if (forwarded.get(part) === open) return;
|
|
2467
|
+
forwarded.set(part, open);
|
|
2468
|
+
host?.setPart(part, open);
|
|
2469
|
+
};
|
|
2470
|
+
world.add(MAINS, mainsSpec(spec), { x: 0, y: 0, z: 0, ry: 0 });
|
|
2471
|
+
if (options.mains?.water ?? true) world.water.setDefaultTank(`${MAINS}:${MAINS_TANK}`);
|
|
2472
|
+
world.add(CONTROLLED_OBJECT, spec, { x: 0, y: 0, z: 0, ry: 0 });
|
|
2473
|
+
const uses = createUses({ spec, world, objectId: CONTROLLED_OBJECT });
|
|
2474
|
+
const programmedDevices = new Set((spec.uses ?? []).flatMap((u) => u.do === "program" ? [u.device] : []));
|
|
2475
|
+
const switchesOf = (device) => [
|
|
2476
|
+
`device.${device}`,
|
|
2477
|
+
...(spec.interactions ?? []).flatMap((i) => i.action.do === "toggle_device" && i.action.device === device ? [i.id] : [])
|
|
2478
|
+
];
|
|
2479
|
+
const programmed = new Set([...programmedDevices].flatMap(switchesOf).map((id) => `${CONTROLLED_OBJECT}:${id}`));
|
|
2480
|
+
const own = (i) => i.object === CONTROLLED_OBJECT && !HOST_KINDS.has(i.kind) && !programmed.has(i.id);
|
|
2481
|
+
const interactions = () => world.interactables().filter(own);
|
|
2482
|
+
const lend = (lent) => {
|
|
2483
|
+
host = lent;
|
|
2484
|
+
};
|
|
2485
|
+
return {
|
|
2486
|
+
list(lent) {
|
|
2487
|
+
lend(lent);
|
|
2488
|
+
const fromWorld = interactions().map((i) => ({ id: i.id, label: i.label, available: i.available, ...i.reason ? { reason: world.t(i.reason) } : {} }));
|
|
2489
|
+
return [...fromWorld, ...uses.list(lent)];
|
|
2490
|
+
},
|
|
2491
|
+
run(id, lent) {
|
|
2492
|
+
lend(lent);
|
|
2493
|
+
if (uses.has(id)) return uses.run(id, lent);
|
|
2494
|
+
const found = interactions().find((i) => i.id === id);
|
|
2495
|
+
if (!found) return { ok: false, text: "" };
|
|
2496
|
+
said.length = 0;
|
|
2497
|
+
const ok = world.act(id);
|
|
2498
|
+
return { ok, text: said.length > 0 ? said.join(" \xB7 ") : found.label };
|
|
2499
|
+
},
|
|
2500
|
+
update(dt, lent) {
|
|
2501
|
+
lend(lent);
|
|
2502
|
+
world.update(dt);
|
|
2503
|
+
uses.update(dt);
|
|
2504
|
+
},
|
|
2505
|
+
state() {
|
|
2506
|
+
const screens = (spec.devices ?? []).filter((d) => d.kind === "screen" && world.power.isOn(CONTROLLED_OBJECT, d.id) && world.power.isPowered(CONTROLLED_OBJECT, d.id)).map((d) => ({ device: d.id, channel: world.power.channel(CONTROLLED_OBJECT, d.id) }));
|
|
2507
|
+
const emitters = world.emitters().filter((e) => e.object === CONTROLLED_OBJECT && e.kind !== "water");
|
|
2508
|
+
return { ...uses.state(), screens, emitters };
|
|
2509
|
+
},
|
|
2510
|
+
dispose() {
|
|
2511
|
+
host = null;
|
|
2512
|
+
world.remove(CONTROLLED_OBJECT);
|
|
2513
|
+
world.remove(MAINS);
|
|
2514
|
+
}
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2239
2517
|
export {
|
|
2518
|
+
CONTROLLED_OBJECT,
|
|
2240
2519
|
FLUSH_RUN_SECONDS,
|
|
2241
2520
|
alignY,
|
|
2242
2521
|
buildChain,
|
|
2243
2522
|
chainPoints,
|
|
2244
2523
|
createEnginePartPhysics,
|
|
2245
2524
|
createFixtureEmitters,
|
|
2525
|
+
createObjectControls,
|
|
2246
2526
|
createWorld,
|
|
2247
2527
|
disposeChain,
|
|
2528
|
+
fitsInVolume,
|
|
2248
2529
|
fitsSize,
|
|
2249
2530
|
fitsSlot,
|
|
2250
2531
|
format,
|