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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.Typewriter
   * @augments adventurejs.Thing
   * @class adventurejs.Typewriter
   * @ajsconstruct MyGame.createAsset({ "class":"Typewriter", "name":"foo", [...] })
   * @ajsconstructedby adventurejs.Game#createAsset
   * @ajsnavheading WritingClasses
   * @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 A typewriter that players can type on.
   * @ajstangiblecontainer in
   * @classdesc
   * <strong>Typewriter</strong> is a subclass of
   * {@link adventurejs.Thing|Thing} with a
   * verb_hooks for the
   * {@link adventurejs.Verb|Verb} {@link type}.
   * (For more information about verb hooks, see
   * <a href="/doc/Scripting_VerbPhases.html">Verb Phases</a>.)
   * It accepts input in the form of
   * <code class="property">type "foo" on typewriter</code> where "foo" is a
   * string with quotes around it. Keyboard can have its
   * <code class="property">typing_target_id</code> set to an
   * {@link adventurejs.Asset|Asset} with its
   * <code class="property">is.typing_target</code> property set to true,
   * and the input string will display on it. See the following
   * transcript to get an idea how this plays out.
   * </p>
   * <pre class="display border outline">
   * <span class="input">&gt; x typewriter</span>
   * It's a typewriter.
   *
   * <span class="input">&gt; type "foo" on typewriter</span>
   * There's no paper in the typewriter. You bang out "foo"
   * on the keys but nothing comes of it.
   *
   * <span class="input">&gt; open middle drawer</span>
   * You open the middle drawer. In the middle drawer you see a blue sheet.
   *
   * <span class="input">&gt; take blue sheet</span>
   * You take the blue sheet from the middle drawer.
   *
   * <span class="input">&gt; put blue sheet in typewriter</span>
   * You put the blue sheet in the typewriter.
   *
   * <span class="input">&gt; type "cordon bleu" on typewriter</span>
   * You type "cordon bleu" on the blue sheet.
   *
   * <span class="input">&gt; x blue sheet</span>
   * It's a blue sheet of paper. Printed on the blue sheet is a phrase: "cordon bleu".
   * </pre>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Typewriter",
   *   name: "typewriter",
   *   place: { on: "desk" },
   *   descriptions: { look: "It's an old fashioned Corona typewriter. ", },
   *   dov: { take: false, },
   * });
   * MyGame.createAsset({
   *   class: "Paper",
   *   name: "blue sheet",
   *   descriptions: { look: "It's a blue sheet of paper. ", },
   *   place: { in: "middle drawer" },
   * });
   * </code></pre>
   **/
  class Typewriter extends adventurejs.Thing {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Typewriter";

      this.setDOVs(["type", "take", "give"]);
      this.setIOV("type");

      this.aspects.in = new adventurejs.Aspect("in", this.game_name).set({
        parent_id: this.id,
        classes_allowed: ["Paper"],
        list_in_examine: true,
        maxcount: 1,
      });

      /**
       * Typewriter needs a sheet of paper in it.
       *
       * doAfterTry lets the verb's standard checks run
       * before checking specifics for this object
       *
       * doBeforeSuccess preempts doSuccess because
       * we want to override the standard success
       */
      // @TODO dov? iov?
      this.dov.type.doAfterTry = function (params) {
        var input = this.game.getInput();
        var direct_object = input.getAsset(1);
        if (direct_object.aspects.in.contents.length == 0) {
          var msg =
            "There's no paper in " +
            direct_object.articlename +
            ". $(We) bang ";
          if (0 < input.strings.length) {
            msg += "out " + input.strings[0] + " ";
          }
          msg += "on the keys but nothing comes of it. ";
          this.game.dictionary.verbs[params.verb].handleFailure(
            msg,
            direct_object
          );
          return null;
        }

        return true;
      };

      this.dov.type.doBeforeSuccess = function (params) {
        var input = this.game.getInput();
        var direct_object = input.getAsset(1);
        var msg = "";

        var indirect_object = this.game.getAsset(
          direct_object.aspects.in.contents[0]
        );

        // state change?
        if (indirect_object && input.strings.length) {
          indirect_object.written_strings.push(input.strings[0]);
        }

        // compose output
        msg += `$(We) type `;
        msg += input.strings.length ? `${input.strings[0]} ` : ``;
        msg += `on ${
          indirect_object
            ? indirect_object.articlename
            : direct_object.articlename
        }. `;

        // print output
        this.game.dictionary.verbs[params.verb].handleSuccess(
          msg,
          direct_object
        );
        return null;
      };
    }
  }
  adventurejs.Typewriter = Typewriter;
})();