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

  /**
   * @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_targets</code> property set to one or more
   * {@link adventurejs.Asset|Assets},
   * and the input string will display on them. See the following
   * transcript to get an idea how this plays out.
   * </p>
   * <pre class="display border outline">
   * <span class="ajs-player-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="ajs-player-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_targets: ["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.
       *
       * 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
       */

      this.iov.type.doBeforeSuccess = function (params) {
        var input = this.game.getInput();
        var tool_asset = input.verb_params.tool_asset;
        var msg = "";

        // state change?
        if (tool_asset.typing_targets.length && input.strings.length) {
          for (let item in tool_asset.typing_targets) {
            let id = tool_asset.typing_targets[item];
            let asset = this.game.getAsset(id);
            asset.typed_strings.push(input.strings[0]);
          }
        }

        // compose output
        msg += `{We} type `;
        msg += input.strings.length ? `"${input.strings[0]}" ` : ``;
        msg += `on ${tool_asset.articlename}. `;
        msg += tool_asset.typing_targets.length
          ? `{Our} input appears on ${this.game.getPrintableObjectList({
              article: "definite",
              objects: tool_asset.typing_targets,
            })} as {we} type. `
          : ``;

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