Pre-release
AdventureJS Docs Downloads Bundler Devlog Score: 0 Moves: 0
Code snippets for AdventureJS. snippets

Code Snippets

Here are some random code snippets that you might find useful.

  • Player floating in water room

    When moving a player into a water room, you might want the game to present the player as being afloat. This snippet creates a water room with a nest that the player is automatically moved into, so that the status bar will display Inland Ocean (floating in the water).

    MyGame.createAsset({
      class: "Room",
      name: "Inland Ocean",
      description: `Welcome to the Inland Ocean. `,
      definite_article: "the", // so it appears as "the Inland Ocean"
      exits: {
        // north: "Sandy Beach",
      },
      contains: "salt water", // turns the room into a reservoir
      // Because this room is an ocean, when the player
      // arrives here, we want to indicate that they're
      // floating in the ocean, and we want to treat the
      // ocean like it's a nest that the player is in.
      // We can force the player to be nested on arrival
      // by setting room.nest_player to true.
      // Now, when player arrives here, they will be
      // "(floating in the salt water)"
      nest_player: true,
      nests: { in_posture: "float", in_can_swim: true },
    });
    
  • Water asset teleports player to water room

    A player standing on a beach can reasonably expect to have access to both sand and water. A Sandy Beach room has a sand reservoir, from which players can "get sand". But what about water? If we only wanted the player to be able to look at the water, then we would just create a piece of scenery. But if we want to respond to "get water", then we can create a reservoir asset to provide water. But if the beach leads to an open water room, then the player should be able to say "enter water" to go to the water room, and that's where our reservoir breaks down. So we're going create a reservoir and add a teleporter to it. Anything a player can enter (go in, on, under, behind) can be turned into a teleporter by setting the asset's nest.target property.

    MyGame.createAsset({
      class: "Waterbody",
      id: "ocean view",
      name: "ocean",
      synonyms: "ocean",
      place: { in: "Sandy Beach" },
      description: `
        The inland ocean laps gently against the sandy beach, 
        and extends to the far horizon. 
      `,
    
      // contains is a shortcut method for setting an asset
      // vessel with an infinite quantity of substance
      contains: "salt water",
    
      // We're using the nest shorthand to set the target
      // property of a nest, which turns that nest into
      // a teleporter. If the player tries to enter this asset,
      // they will be moved to the target location.
      nests: {
        in_target: "Inland Ocean",
      },
    });
    
  • Add reservoir to an exit

    We've placed the player on a beach, which has an exit that leads to a water room. There's a reasonable expectation that player should be able to access sand and water. They can "go water" or "swim in water" or use a compass direction to enter the water room, but in order to respond to "get water", we need to provide a reservoir. So we'll turn the exit itself into a reservoir.

    MyGame.createAsset({
      class: "Exit",
      // Exits usually aren't named, but we want to
      // name this exit after its destination.
      name: "Inland Ocean",
      direction: "south",
      place: { in: "Eastern Beach" },
      destination: "Inland Ocean",
      description: `
        The Inland Ocean laps against the shore, 
        and extends south to the far horizon. 
      `,
      // By setting contains, we're adding a vessel
      // to this exit and turning it into a reservoir.
      // It's abnormal, but not illegal.
      contains: "salt water",
    });