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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.WritingImplement.Pen
   * @augments adventurejs.WritingImplement
   * @class adventurejs.Pen
   * @ajsconstruct MyGame.createAsset({ "class":"Pen", "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 writing implement.
   * @tutorial Tangibles_AboutTangibles
   * @ajsdemo WritingDemo, Office, Playroom, Classroom, Library, Scorecard
   * @ajscss Styles
   * @classdesc
   * <p>
   * <strong>Pen</strong> is a subclass of
   * {@link adventurejs.WritingImplement|WritingImplement}.
   * Pens and other WritingImplements have their
   * <code class="property">iov.write.enabled</code>
   * property set to true, which allows a player to write on things
   * with <code class="property">asset.dov.write.enabled</code>
   * set to true, such as {@link adventurejs.Paper|Paper}.
   * </p>
   * <p>
   * All that distinguishes Pen from other WritingImplements
   * is that its
   * <code class="property">attached</code>
   * {@link adventurejs.Aspect|Aspect}
   * allows an instance of class
   * {@link adventurejs.PenCap|PenCap} to be attached.
   * </p>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Pen",
   *   name: "red pen",
   *   article: "a",
   *   adjectives: "red",
   *   place: { on: "table" },
   *   descriptions: { look: "It's a pen with red ink. ", },
   * });
   * MyGame.createAsset({
   *   class: "PenCap",
   *   name: "pen cap",
   *   article: "a",
   *   place: { on: "table" },
   *   descriptions: { look: "It's a cap for a pen. ", },
   * });
   * MyGame.createAsset({
   *   class: "Paper",
   *   name: "construction paper",
   *   descriptions: { look: "It's a pink sheet of construction paper. ", },
   *   adjectives: ["pink"]
   *   place: { on: "bottom drawer" },
   * });
   * </code></pre>
   * <p>
   * The above example uses
   * <code class="property">with_classes</code>
   * to limit what a
   * player can put in without having to write custom
   * failure code for every object. To learn more, see
   * <a href="/doc/Tangibles_Aspects.html">How to Use Aspects</a>.
   * </p>
   **/
  class Pen extends adventurejs.WritingImplement {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Pen";

      this.noun = "pen";
      this.plural = "pens";
      this.singlePluralPairs = [["pen", "pens"]];

      this.aspects.attached = new adventurejs.Aspect(
        "attached",
        this.game_name,
        this.id
      ).set({
        list_contents_in_room: true,
        list_contents_in_examine: true,
        know_contents_with_parent: true,
        see_contents_with_parent: true,
        contents_limits: { count: 1 },
        with_classes: ["PenCap"],
      });

      // if a pen has a cap we want to prevent it from writing
      // we hook into doAfterTry so that doTry runs first and
      // catches any other blockers first
      this.setIOV({
        write: {
          doAfterTry: () => {
            if (this.hasContentsAtAspect("attached")) {
              let cap_id = this.getContentsAt("attached")[0];
              let cap = this.game.getAsset(cap_id);
              if (cap.hasClass("PenCap")) {
                this.game.debug(
                  `D1381 | ${this.name}.js | ${this.id} can't write with ${cap.id} attached`
                );
                let msg = `{We'll} have to remove ${cap.articlename} from ${this.articlename} first. `;
                this.game.getVerb("write").handleFailure(msg);
                return null;
              }
            }
          },
        },
      });
    }
  }
  adventurejs.Pen = Pen;
})();