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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.Keyboard
   * @augments adventurejs.Thing
   * @class adventurejs.Keyboard
   * @ajsconstruct MyGame.createAsset({ "class":"Keyboard", "name":"foo", [...] })
   * @ajsconstructedby adventurejs.Game#createAsset
   * @ajsnavheading ElectronicsClasses
   * @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 computer keyboard that players can type on.
   * @classdesc
   * <p>
   * <strong>Keyboard</strong> is a subclass of
   * {@link adventurejs.Thing|Thing} with a
   * verb phase 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 keyboard</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; type "It was the best of times, it was the BLURST of times?" on keyboard</span>
   * You type "It was the best of times, it was the BLURST of times?"
   * on the keyboard. Your input appears on the screen as you type.
   *
   * <span class="input">&gt; x screen</span>
   * It's a fancy monitor. A line of input is visible on it:
   * "It was the best of times, it was the BLURST of times?"
   * </pre>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Keyboard",
   *   name: "keyboard",
   *   place: { on: "desk" },
   *   descriptions: { look: "It's a keyboard. You can type on it. ", },
   *   dov: { take: false },
   *   typing_target_id: "screen",
   * });
   * MyGame.createAsset({
   *   class: "Screen",
   *   name: "screen",
   *   place: { on: "desk" },
   *   descriptions: { look: "It's a fancy monitor. ", },
   *   dov: { take: true },
   * });
   * </code></pre>
   **/
  class Keyboard extends adventurejs.Thing {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Keyboard";
      this.setDOV("type");
      this.setIOV("type");

      /**
       * Keyboard can operate on a separate typing_target_id.
       *
       * 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 or iov or both? or already handled by verb?
      // "type on keyboard" = dov
      // "type foo on keyboard" = iov?
      this.dov.type.doBeforeSuccess = function (params) {
        console.warn("CUSTOM type");
        var input = this.game.getInput();
        var direct_object = input.getAsset(1);
        var indirect_object = this.game.getAsset(
          direct_object.typing_target_id
        );
        var msg = "";

        // 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 ${direct_object.articlename}. `;
        msg += indirect_object
          ? `$(Our) input appears on ${indirect_object.articlename} as $(we) type. `
          : ``;

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