Pre-release
AdventureJS Docs Downloads Bundler Devlog Score: 0 Moves: 0
Tutorial explaining the basics of collections substances in AdventureJS. tutorial, substance collections

Working With...Collections

A collection is like a piece of scenery. It's a special kind of asset that only exists to point to a group of other assets. For instance, imagine a desk with drawers, where the drawers are unique assets. We can create a drawers collection asset to let the player refer to the drawers without having the game ask them which drawer did you mean?

Example Code

In this example code, we define a desk, three separate drawers, and a drawers collection. Take special note of the targets property of the Collection asset.

MyGame.createAsset({
  class: "Collection",
  name: "desk drawers",
  place: { attached: "desk" },
  targets: "top drawer, middle drawer, bottom drawer",
  synonyms: ["drawers", "three drawers"],
  description: "There are three drawers in the desk: top, bottom, and middle. ",
  is: { listed: false },
});

MyGame.createAsset({
  class: "Drawer",
  name: "top drawer",
  synonyms: "top desk drawer, top drawer lock, lock",
  description: "The top drawer is { top drawer [is] open [or] closed }. ",
  adjectives: "desk",
  place: { attached: "desk" },
  is: { closed: true, listed: false },
});

MyGame.createAsset({
  class: "Drawer",
  name: "middle drawer",
  synonyms: "middle desk drawer",
  description: "The middle drawer is { middle drawer [is] open [or] closed }. ",
  adjectives: "desk",
  place: { attached: "desk" },
  is: { closed: true, listed: false },
});
    
MyGame.createAsset({
  class: "Drawer",
  name: "bottom drawer",
  synonyms: "bottom desk drawer",
  description: "The bottom drawer is { bottom drawer [is] open [or] closed }. ",
  adjectives: "desk",
  place: { attached: "desk" },
  is: { closed: true, listed: false },
});

MyGame.createAsset({
  class: "Desk",
  name: "desk",
  place: { in: "Wizard's Office" },
  description: `The wizard's desk has three drawers stacked vertically. The top drawer is { top drawer [is] open [or] closed }, the middle drawer is { middle drawer [is] open [or] closed }, and the bottom drawer is { bottom drawer [is] open [or] closed }. `,
});

Expanding Descriptions

You may have already noticed how we're using placeholders to print whether each drawer is open or closed. Using a bit of custom code, we can take that a bit further and show the state of all three drawers in a more complex sentence.

MyGame.createAsset({
  class: "Collection",
  name: "desk drawers",
  place: { attached: "desk" },
  targets: "top drawer, middle drawer, bottom drawer",
  synonyms: ["drawers", "three drawers"],
  description: function () {
    var openCount = [];
    var closedCount = [];
    var complicatedMsg =
      "The desk has three drawers stacked vertically. ";

    MyGame.$("top drawer").is.closed
      ? closedCount.push("top drawer")
      : openCount.push("top drawer");
    MyGame.$("middle drawer").is.closed
      ? closedCount.push("middle drawer")
      : openCount.push("middle drawer");
    MyGame.$("bottom drawer").is.closed
      ? closedCount.push("bottom drawer")
      : openCount.push("bottom drawer");

    if (0 === openCount.length) {
      complicatedMsg += "All three are closed.";
    } else if (0 === closedCount.length) {
      complicatedMsg += "All three are open.";
    } else if (2 == openCount.length) {
      complicatedMsg +=
        `The ${openCount[0]} and the ${openCount[1]} are open, while the ${closedCount[0]} is closed. `;
    } else if (2 == closedCount.length) {
      complicatedMsg +=
        `The ${closedCount[0]} and the ${closedCount[1]} are closed, while the ${openCount[0]} is open. `;
    }

    return complicatedMsg;
  },
});

Now our output might look something like this:

The desk has three drawers stacked vertically. The top drawer and the bottom drawer are open, while the middle drawer is closed.

Other Collection Scenarios

Here are some other collection scenarios to get you thinking about potential uses.

// here we have several similar cobblestones
MyGame.createAsset({
  class: "Collection",
  name: "cobblestones",
  place: { in: "Dungeon Antechamber" },
  targets: "pitted cobblestone, weathered cobblestone, cracked cobblestone",
  synonyms: [ ],
  is: { listed: false, known: true },
  description: "The entire room seems to be laid in cobblestones. Not just the floor, but also the walls and ceiling. The stones around the frame of the wood planked door draw your eye. Three in particular stand out: a cracked cobblestone, a weathered cobblestone, and a pitted cobblestone. ",
});

// This sink has two faucets and we want each one to be manipulable
// but we also want the player to be able to "examine faucets"
MyGame.createAsset({
  class: "Collection",
  name: "sink handles",
  synonyms: ["porcelain handles"],
  place: { attached: "vessel sink" },
  targets: ["sink's hot water handle", "sink's cold water handle"],
  is: { listed: false },
  description: "Two porcelain sink handles, hot and cold. ",
});

// We want to let the player examine all the furnishings in the room
// We're not using the word "furniture" because Furniture is a class
// and that would actually prompt us for which furniture we meant 
MyGame.createAsset({
  class: "Collection",
  name: "furnishings",
  description: "A bed, a desk, and a table. ",
  place: { in: "Hotel Room" },
  targets: "uncomfortable looking bed, desk, table",
});

Further Reference