@adatechnology/game-interactables 0.3.0 → 0.4.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';
@@ -565,6 +567,8 @@ interface Interactable {
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;
567
569
  /** False when the interaction cannot be used right now (see `reason`). `world.target` skips these unless `includeUnavailable`. */
570
+ /** 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. */
571
+ hands?: CarryHands;
568
572
  available: boolean;
569
573
  /** 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. */
570
574
  reason?: string;
@@ -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,51 @@ 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
+ available: true,
1272
+ act: (actor) => takeAt(o.id, c.id, slot, actor ?? DEFAULT_ACTOR)
1273
+ };
1274
+ if (options.canInteract?.(item, DEFAULT_ACTOR) === false) {
1275
+ item.available = false;
1276
+ item.reason = "locked";
1277
+ }
1278
+ out.push(item);
1279
+ });
1280
+ }
1281
+ return out;
1282
+ }
1283
+ function takeAt(objectId, containerId, slot, actor) {
1284
+ const o = objs.get(objectId);
1285
+ if (!o) return false;
1286
+ const probe = contentTakes(o).find((k) => k.id === `${objectId}:${TAKE_PREFIX}${containerId}.${slot}`);
1287
+ if (!probe || options.canInteract?.(probe, actor) === false) return false;
1288
+ return take(objectId, containerId, slot, actor) !== null;
1289
+ }
1243
1290
  function interactables() {
1244
1291
  const out = [];
1245
- for (const o of objs.values()) for (const e of interactablesOf(o)) out.push(e.item);
1292
+ for (const o of objs.values()) {
1293
+ for (const e of interactablesOf(o)) out.push(e.item);
1294
+ out.push(...contentTakes(o));
1295
+ }
1246
1296
  return out;
1247
1297
  }
1248
1298
  function target(eye, opts = {}) {
@@ -1252,11 +1302,11 @@ var GameInteractables = (() => {
1252
1302
  let best = null;
1253
1303
  let bestScore = Infinity;
1254
1304
  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];
1305
+ for (const item of [...interactablesOf(o).map((e) => e.item), ...contentTakes(o)]) {
1306
+ if (!item.available && !opts.includeUnavailable) continue;
1307
+ if (opts.kinds && !opts.kinds.includes(item.kind)) continue;
1308
+ if (opts.filter && !opts.filter(item)) continue;
1309
+ const v = [item.pos[0] - eye.x, item.pos[1] - eye.y, item.pos[2] - eye.z];
1260
1310
  const d = Math.hypot(v[0], v[1], v[2]);
1261
1311
  if (d > reach) continue;
1262
1312
  const cos = d < 1e-6 ? 1 : clamp((v[0] * dir[0] + v[1] * dir[1] + v[2] * dir[2]) / d, -1, 1);
@@ -1265,7 +1315,7 @@ var GameInteractables = (() => {
1265
1315
  const score = angle / cone * 0.6 + d / reach * 0.4;
1266
1316
  if (score < bestScore) {
1267
1317
  bestScore = score;
1268
- best = e.item;
1318
+ best = item;
1269
1319
  }
1270
1320
  }
1271
1321
  }
@@ -1276,6 +1326,10 @@ var GameInteractables = (() => {
1276
1326
  for (const o of objs.values()) {
1277
1327
  if (!interactableId.startsWith(o.id + ":")) continue;
1278
1328
  const local = interactableId.slice(o.id.length + 1);
1329
+ if (local.startsWith(TAKE_PREFIX)) {
1330
+ const at = contentTakes(o).find((k) => k.id === interactableId);
1331
+ if (at) return at.act(actor);
1332
+ }
1279
1333
  if (!o.interactions.some((i) => i.id === local)) continue;
1280
1334
  found = interactablesOf(o).find((e) => e.spec.id === local) ?? null;
1281
1335
  if (found) break;