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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Character
   * @augments adventurejs.Tangible
   * @class adventurejs.Character
   * @ajsconstruct MyGame.createAsset({ "class":"Character", "name":"foo", [...] })
   * @ajsconstructedby adventurejs.Game#createAsset
   * @ajsnavheading CharacterClasses
   * @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 Base class for all character types.
   * @tutorial Characters_NPCs
   * @tutorial CreatePlayer
   * @ajstangiblecontainer in
   * @todo more methods, better example
   * @classdesc
   * <p>
   * <strong>Character</strong> is anything that moves or speaks
   * or takes or gives. {@link adventurejs.Player|Player} and
   * {@link adventurejs.NPC|NPCs} are both Characters.
   * Character can sense things, hold things, be asked things,
   * be told things...
   * </p>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   "class": "Character",
   *   "name": "Yurtle",
   *   "synonyms": ["turtle"],
   *   "place": {in: "Pond"},
   * })
   * </code></pre>
   */
  class Character extends adventurejs.Tangible {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Character";

      this.is = new adventurejs.Character_Is("is", this.game_name, this.id).set(
        {
          parent_id: this.id,
        }
      );

      this.can = new adventurejs.Character_Can(
        "can",
        this.game_name,
        this.id
      ).set({
        parent_id: this.id,
      });

      this.setIOVs(["throw", "take", "give", "hold", "tie"]);

      this.posture = "stand";

      this.aspects.in = new adventurejs.Aspect("in", this.game_name).set({
        parent_id: this.id,
      });

      this.dont_use_articles = true;
      this.name_is_proper = true;
    }

    /**
     * Get / set nest.
     * @var {Object} adventurejs.Character#nest
     */
    get nest() {
      return this._nest;
    }
    set nest(nest) {
      if (Object(nest) !== nest) {
        var msg = this.id + ".nest received a malformed value ";
        this.game.log("error", "critical", msg, "Tangible");

        nest = {};
      } else {
        var keys = Object.keys(nest);

        if (keys.length > 1) {
          var msg =
            this.id +
            ".nest received more than one location. Using the first. ";
          for (var i = 0; i < keys.length; i++) {
            msg += keys[i] + ", ";
          }
          nest = { [keys[0]]: nest[keys[0]] };
          this.game.log("error", "critical", msg, "Character");
        }

        if (keys.length === 1) {
          // serialize asset name
          nest[keys[0]] = A.serialize(nest[keys[0]]);

          // verify asset
          if ("string" !== typeof nest[keys[0]]) {
            var msg = this.id + ".nest set to an invalid asset ";
            this.game.log("error", "critical", msg, "Tangible");
          }
        }

        if (keys.length === 0) {
        }
      }

      this._nest = nest;
    }
  }
  adventurejs.Character = Character;
})();