Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// Wall.js
(function () {
  /*global adventurejs A*/

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.Wall
   * @augments adventurejs.Thing
   * @class adventurejs.Wall
   * @ajsconstruct MyGame.createAsset({ "class":"Wall", "name":"foo", [...] })
   * @ajsconstructedby adventurejs.Game#createAsset
   * @ajsnavheading RoomAssets
   * @param {String} game_name The name of the top level game object.
   * @param {String} name A name for the object, to be serialized and used as ID.
   * @summary When the world slips you a Jeffrey, stroke the furry wall.
   * @tutorial NextSteps_GlobalScenery
   * @classdesc
   * <p>
   * <strong>Wall</strong> is a subclass of
   * {@link adventurejs.Thing|Thing},
   * with no particular special properties. However, the
   * {@link adventurejs.Game|Game}
   * instance creates a set of global walls which can be
   * referred to from any Room in the Game, and can be customized
   * globally or by
   * {@link adventurejs.Room|Room} or
   * {@link adventurejs.Zone|Zone}. Each of the
   * predefined global walls corresponds to one of twelve
   * directions: the four cardinal directions,
   * or north, south, east, west;
   * the four ordinal directions, or northeast, northwest,
   * southeast, southwest; fore and aft; and port and starboard.
   * This provides
   * a way to respond to player input like "examine north wall",
   * with a cogent response without coding unique objects for
   * each Room. Authors can enable or disable global walls
   * per Room, per Zone, or per Game.
   * The Wall class was written with the assumption that this
   * is the only way they'd be used. It's possible to construct
   * unique instances of Walls, though if you want them to do
   * anything more you may need to write custom code for it.
   * To learn more, see
   * <a href="/doc/NextSteps_GlobalScenery.html">Global Scenery</a>.
   * </p>
   * <h3 class="examples">Example:</h3>
   * <p>Here is an example of a simple wall constructor.</p>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   "class":"Wall",
   *   "direction":"north",
   *   "place":{in:"South Room"},
   * })
   * </code></pre>
   * <p>Here is an example of how you would enable specified walls
   * for a Room.</p>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Room",
   *   name: "Ship's Cabin",
   *   descriptions: {
   *     look: "The tiny cabin has walls fore and aft, and to port and starboard. ",
   *   },
   *   room_scenery: {
   *     fore_wall: {
   *       enabled: true,
   *       descriptions: {
   *         look: "Though it has no windows, you know that beyond
   *         the fore wall lies the bow of the ship. ",
   *       },
   *     },
   *     aft_wall: {
   *       enabled: true,
   *       descriptions: {
   *         look: "Though the wall has no windows, you know that
   *         the Captain's quarters lie beyond the aft wall. ",
   *       },
   *     },
   *     port_wall: {
   *       enabled: true,
   *       descriptions: {
   *         look: "Through a porthole in the port wall you can see far off land. ",
   *       },
   *     },
   *     starboard_wall: {
   *       enabled: true,
   *       descriptions: {
   *         look: "Nothing but sea is visible through the porthole in the starboard wall. ",
   *       },
   *     },
   *   },
   * });
   * </code></pre>
   **/
  class Wall extends adventurejs.Tangible {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Wall";
      this.is.listed_in_room = false;
      this.singlePluralPairs = [["wall", "walls"]];
    }

    validate(game) {
      super.validate(game);

      // check the wall's direction
      // @todo can we have a wall without a direction?
      if (this.direction && !this.is.global) {
        var msg = `Wall ${this.name} has no direction. `;
        console.error(msg);
        return false;
      }

      if (!this.name) {
        this.name = this.direction + " wall";
      }

      // add all synonyms for this wall's direction, as adjectives
      // so player can refer to, eg, "east wall" or "e wall"
      // console.warn("this.direction");
      // console.warn(this);
      // console.warn(this.direction);
      var directionSynonyms =
        this.game.dictionary.direction_lookup[this.direction].synonyms;
      for (var i = 0; i < directionSynonyms.length; i++) {
        this.adjectives.push(directionSynonyms[i]);
      }

      // also add direction adjectives so player can refer to, eg, "eastern wall"
      var directionAdjectives =
        this.game.dictionary.direction_lookup[this.direction].adjectives;
      for (var i = 0; i < directionAdjectives.length; i++) {
        this.adjectives.push(directionAdjectives[i]);
      }

      /**
       * Global walls are a special case which exist to catch
       * provide descriptions for walls where authors haven't
       * made custom walls. They don't need further validation.
       */
      if (this.is.global) {
        return true;
      }

      /**
       * TODO
       * What other validation does a wall need?
       */
    }
  }
  adventurejs.Wall = Wall;
})();