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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.Key.SingleUseKey
   * @augments adventurejs.Key
   * @class adventurejs.SingleUseKey
   * @ajsconstruct MyGame.createAsset({ "class":"SingleUseKey", "name":"foo", [...] })
   * @ajsconstructedby adventurejs.Game#createAsset
   * @ajsnavheading KeyClasses
   * @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 single use key can unlock a lock. Once.
   * @tutorial CreateKey
   * @classdesc
   * <p>
   * <strong>SingleUseKey</strong> is a subclass of
   * {@link adventurejs.Key|Key}
   * that can be used to unlock locked
   * {@link adventurejs.Asset|Assets}.
   * SingleUseKey uses
   * <code class="property">asset.iov.pick.then_destroy</code>,
   * to destroy the asset after one use.
   * The interaction is determined by properties on both objects.
   * The SingleUseKey will have a list of Asset
   * <a href="#id">IDs</a> it can unlock in its
   * <code class="property">asset.iov.unlock.with_assets</code>,
   * and the locked object will also have the
   * SingleUseKey ID listed in its
   * <code class="property">asset.dov.unlock.with_assets</code>.
   * (You can set just one or the other in your game file.
   * As long as one is set, the SingleUseKey
   * and Lock will be associated during
   * initialization.) The locked object will also have
   * asset.dov.lock.enabled set to true. See the example below to create a key and
   * a locked chest.
   * </p>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Key",
   *   name: "brass key",
   *   place: { in: "Treasure Room" },
   *   iov: { unlock: { with_assets: ['treasure chest'], }, },
   * });
   * MyGame.createAsset({
   *   class: "Chest",
   *   name: "treasure chest",
   *   place: { in: "Treasure Room" },
   *   is_locked: true,
   *   dov: { unlock: { with_assets: ['brass key'], }, },
   * });
   * </code></pre>
   **/
  class SingleUseKey extends adventurejs.Key {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "SingleUseKey";

      this.setIOV({ unlock: { then_destroy: true } });
    }
  }

  adventurejs.SingleUseKey = SingleUseKey;
})();