@adatechnology/game-interactables 0.3.1 → 0.5.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Thumbnail } from '@adatechnology/game-contracts';
1
+ import { CarryHands, Thumbnail } from '@adatechnology/game-contracts';
2
2
 
3
3
  /**
4
4
  * Contract of @adatechnology/game-interactables.
@@ -475,6 +475,8 @@ interface WorldOptions {
475
475
  onEvent?: (event: WorldEvent) => void;
476
476
  /** Return false to cancel an interaction (locked, needs a tool, tutorial...). */
477
477
  canInteract?: (interaction: Interactable, actor: string) => boolean | void;
478
+ /** Each stack in an open container offers a `take` of its own at its slot (`interactables()`, `target`, `act`). Default true; false leaves only the declared interactions. */
479
+ takeContents?: boolean;
478
480
  }
479
481
  type WorldEvent = {
480
482
  type: 'part';
@@ -564,6 +566,10 @@ interface Interactable {
564
566
  verb: string;
565
567
  /** Localized thing the verb applies to, e.g. "the fridge" / "a geladeira" (falls back to the object type name). Named `noun` because `object` is the owning object's id. */
566
568
  noun: string;
569
+ /** Hands the thing taken needs (an item inside a piece of furniture: 1, the other hand stays free), as a destruction part taken by hand. Only on `take` of a contained item. */
570
+ hands?: CarryHands;
571
+ /** The catalogue id of the item a `take` of a contained stack hands over (`jeans`), for the host to put it in the hand. Only on `take` of a contained item. */
572
+ itemType?: string;
567
573
  /** False when the interaction cannot be used right now (see `reason`). `world.target` skips these unless `includeUnavailable`. */
568
574
  available: boolean;
569
575
  /** Locale key explaining why it is unavailable (`requires_open`, `no_power`, `locked`, `already_lit`, `needs_ignition`, `turn_on_first`); `world.t(reason)` is the text. */
@@ -300,7 +300,10 @@ var GameInteractables = (() => {
300
300
  // src/world.ts
301
301
  var DEFAULT_ACTOR = "player";
302
302
  var DEFAULT_RADIUS = 1.2;
303
+ var TAKE_PREFIX = "take.";
304
+ var TAKE_RADIUS = 0.6;
303
305
  var AUTO_RANGE = 2;
306
+ var SOCKET_BEHIND = 0.05;
304
307
  var SMOKE_TAIL = 20;
305
308
  var FLUSH_SECONDS = 2;
306
309
  var ACTOR_HEIGHT = 1.7;
@@ -619,7 +622,11 @@ var GameInteractables = (() => {
619
622
  plugs.clear();
620
623
  const sockets = [];
621
624
  for (const o of objs.values()) {
622
- for (const s of o.spec.sockets ?? []) sockets.push({ id: gid(o, s.id), local: s.id, pos: toWorld(o, s.pos), circuit: s.circuit });
625
+ for (const s of o.spec.sockets ?? []) {
626
+ const pos = toWorld(o, s.pos);
627
+ const ahead = toWorld(o, [s.pos[0], s.pos[1], s.pos[2] + 1]);
628
+ sockets.push({ id: gid(o, s.id), local: s.id, pos, circuit: s.circuit, facing: [ahead[0] - pos[0], ahead[1] - pos[1], ahead[2] - pos[2]] });
629
+ }
623
630
  }
624
631
  const used = /* @__PURE__ */ new Set();
625
632
  const autos = [];
@@ -645,6 +652,7 @@ var GameInteractables = (() => {
645
652
  let bestD = AUTO_RANGE;
646
653
  for (const s of sockets) {
647
654
  if (used.has(s.id)) continue;
655
+ if ((from[0] - s.pos[0]) * s.facing[0] + (from[1] - s.pos[1]) * s.facing[1] + (from[2] - s.pos[2]) * s.facing[2] < -SOCKET_BEHIND) continue;
648
656
  const dd = dist3(from, s.pos);
649
657
  if (dd <= bestD) {
650
658
  best = s;
@@ -1240,9 +1248,52 @@ var GameInteractables = (() => {
1240
1248
  return { item, spec: i, o };
1241
1249
  });
1242
1250
  }
1251
+ function contentTakes(o) {
1252
+ if (options.takeContents === false) return [];
1253
+ const out = [];
1254
+ for (const c of o.spec.containers ?? []) {
1255
+ const list = o.contents.get(c.id);
1256
+ if (!list || !list.some((st) => st !== null) || !accessible(o.id, c.id)) continue;
1257
+ list.forEach((stack, slot) => {
1258
+ if (!stack || options.items?.[stack.type]?.pickable === false || stack.data?.pickable === false) return;
1259
+ const verb = t("prompt.take");
1260
+ const noun = table[`item.${stack.type}`] ?? humanize(stack.type);
1261
+ const item = {
1262
+ id: `${o.id}:${TAKE_PREFIX}${c.id}.${slot}`,
1263
+ object: o.id,
1264
+ kind: "take",
1265
+ pos: slotPosition(o.id, c.id, slot),
1266
+ radius: TAKE_RADIUS,
1267
+ label: `${verb} ${noun}`,
1268
+ verb,
1269
+ noun,
1270
+ hands: 1,
1271
+ itemType: stack.type,
1272
+ available: true,
1273
+ act: (actor) => takeAt(o.id, c.id, slot, actor ?? DEFAULT_ACTOR)
1274
+ };
1275
+ if (options.canInteract?.(item, DEFAULT_ACTOR) === false) {
1276
+ item.available = false;
1277
+ item.reason = "locked";
1278
+ }
1279
+ out.push(item);
1280
+ });
1281
+ }
1282
+ return out;
1283
+ }
1284
+ function takeAt(objectId, containerId, slot, actor) {
1285
+ const o = objs.get(objectId);
1286
+ if (!o) return false;
1287
+ const probe = contentTakes(o).find((k) => k.id === `${objectId}:${TAKE_PREFIX}${containerId}.${slot}`);
1288
+ if (!probe || options.canInteract?.(probe, actor) === false) return false;
1289
+ return take(objectId, containerId, slot, actor) !== null;
1290
+ }
1243
1291
  function interactables() {
1244
1292
  const out = [];
1245
- for (const o of objs.values()) for (const e of interactablesOf(o)) out.push(e.item);
1293
+ for (const o of objs.values()) {
1294
+ for (const e of interactablesOf(o)) out.push(e.item);
1295
+ out.push(...contentTakes(o));
1296
+ }
1246
1297
  return out;
1247
1298
  }
1248
1299
  function target(eye, opts = {}) {
@@ -1252,11 +1303,11 @@ var GameInteractables = (() => {
1252
1303
  let best = null;
1253
1304
  let bestScore = Infinity;
1254
1305
  for (const o of objs.values()) {
1255
- for (const e of interactablesOf(o)) {
1256
- if (!e.item.available && !opts.includeUnavailable) continue;
1257
- if (opts.kinds && !opts.kinds.includes(e.item.kind)) continue;
1258
- if (opts.filter && !opts.filter(e.item)) continue;
1259
- const v = [e.item.pos[0] - eye.x, e.item.pos[1] - eye.y, e.item.pos[2] - eye.z];
1306
+ for (const item of [...interactablesOf(o).map((e) => e.item), ...contentTakes(o)]) {
1307
+ if (!item.available && !opts.includeUnavailable) continue;
1308
+ if (opts.kinds && !opts.kinds.includes(item.kind)) continue;
1309
+ if (opts.filter && !opts.filter(item)) continue;
1310
+ const v = [item.pos[0] - eye.x, item.pos[1] - eye.y, item.pos[2] - eye.z];
1260
1311
  const d = Math.hypot(v[0], v[1], v[2]);
1261
1312
  if (d > reach) continue;
1262
1313
  const cos = d < 1e-6 ? 1 : clamp((v[0] * dir[0] + v[1] * dir[1] + v[2] * dir[2]) / d, -1, 1);
@@ -1265,7 +1316,7 @@ var GameInteractables = (() => {
1265
1316
  const score = angle / cone * 0.6 + d / reach * 0.4;
1266
1317
  if (score < bestScore) {
1267
1318
  bestScore = score;
1268
- best = e.item;
1319
+ best = item;
1269
1320
  }
1270
1321
  }
1271
1322
  }
@@ -1276,6 +1327,10 @@ var GameInteractables = (() => {
1276
1327
  for (const o of objs.values()) {
1277
1328
  if (!interactableId.startsWith(o.id + ":")) continue;
1278
1329
  const local = interactableId.slice(o.id.length + 1);
1330
+ if (local.startsWith(TAKE_PREFIX)) {
1331
+ const at = contentTakes(o).find((k) => k.id === interactableId);
1332
+ if (at) return at.act(actor);
1333
+ }
1279
1334
  if (!o.interactions.some((i) => i.id === local)) continue;
1280
1335
  found = interactablesOf(o).find((e) => e.spec.id === local) ?? null;
1281
1336
  if (found) break;