Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// print.js
(function () {
  /*global adventurejs A*/
  "use strict";

  var p = adventurejs.Game.prototype;

  /**
   * Prep the supplied string for printing to the display.
   * @method adventurejs.Game#print
   * @memberOf adventurejs.Game
   * @param {String} msg
   * @param {String} classes
   */
  p.print = function Game_print(msg, classes) {
    if ("undefined" === typeof msg) msg = "";
    if ("undefined" === typeof classes) classes = "";

    /**
     * Extra output can be conditionally prepended/appended by sub functions,
     * like background events such as running water, or by custom code
     * from author.
     * If any prepended/appended output is found, print it, then move it,
     * to ensure it doesn't get printed again, while still
     * keeping a record of it.
     */

    if (this.input?.printer?.override.length) {
      msg = this.input.printer.override.join(" ");
      //reset override
      this.input.printer.overriden = this.input.printer.overriden.concat(
        this.input.printer.override
      );
      this.input.printer.override = [];
    }

    if (this.input?.printer?.prepend.length) {
      msg = this.input.printer.prepend.join(" ") + msg;
      this.input.printer.prepended = this.input.printer.prepended.concat(
        this.input.printer.prepend
      );
      this.input.printer.prepend = [];
    }

    if (this.input?.printer?.append.length) {
      msg += this.input.printer.append.join(" ");
      this.input.printer.appended = this.input.printer.appended.concat(
        this.input.printer.appended
      );
      this.input.printer.append = [];
    }

    if (!msg) return;
    // condense multiple spaces
    msg = msg.replace(/  +/g, " ");

    // expand compressed prepositions
    // and hope noone uses "fromin" as an asset name?
    msg = msg.replace(/ outfrombehind /g, " out from behind ");
    msg = msg.replace(/ outfromunder /g, " out from under ");
    msg = msg.replace(/ infront /g, " in front ");
    msg = msg.replace(/ frombehind /g, " from behind ");
    msg = msg.replace(/ fromin /g, " from in ");
    msg = msg.replace(/ fromon /g, " from on ");
    msg = msg.replace(/ fromunder /g, " fromunder ");

    this.game.log("log", "high", msg, "Print");
    msg = A.substituteCustomTemplates.call(this, msg);
    msg = A.substituteHTMLTags.call(this, msg);
    // send to display
    if (msg) this.display.print(msg, classes);
  };
})();