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

  var p = AdventureJS.Game.prototype;

  /**
   *
   * @method AdventureJS.Game#renderIfPlayerIs
   * @memberOf AdventureJS.Game
   * @param {String} token
   */
  p.renderIfPlayerIs = function Game_renderIfPlayerIs(token) {
    let rendered = "";
    let [, condition, substitution] = token.match(/^\[(.*?)\]\s*(.*)$/);
    let preposition, asset, wearing;
    const player = this.game.getPlayer();

    // condition
    // strip out "if player is"
    condition = condition.replace("if player is ", "");
    condition = condition.replace(" attached to ", " attached ");

    if (condition.startsWith("wearing ")) {
      wearing = true;
    }

    preposition = ["in ", "on ", "under ", "behind ", "attached "].find(
      (preposition) => condition.startsWith(preposition)
    );

    if (preposition) {
      // strip out preposition
      condition = condition.replace(preposition, "");
      preposition = preposition.trim();

      // what remains should be an asset name or id
      asset = this.game.getAsset(condition);

      // if it's not an asset,
      // we don't know how to handle this (yet)
      if (!asset) return "";

      if (player.$is(preposition, asset)) {
        rendered = substitution;
      }
    } else if (wearing) {
      condition = condition.replace("wearing ", "");

      // what remains should be an asset name or id
      asset = this.game.getAsset(condition);

      // if it's not an asset,
      // we don't know how to handle this (yet)
      if (!asset) return "";

      if (player.$is("wearing", asset)) {
        rendered = substitution;
      }
    }

    return rendered;
  }; //
})();