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

  /**
   * @ajspath adventurejs.Atom.Asset.Matter.Tangible.Character.Player
   * @augments adventurejs.Character
   * @class adventurejs.Player
   * @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 The player character.
   * @todo Character swapping.
   * @classdesc
   * <p>
   * <strong>Player</strong> is a subclass of
   * {@link adventurejs.Character|Character}, used for
   * the player character(s). It will be possible to have
   * multiple player characters, though that hasn't been
   * implemented yet.
   * </p>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">
   * </code></pre>
   **/
  class Player extends adventurejs.Character {
    constructor(name, game_name) {
      super(name, game_name);
      this.class = "Player";

      this.is.active = false;
      this.is._known = true;
      this.is._seen = true;

      this.setIOVs([{ hold: { with_params: { max_connections: 2 } } }]);
    }

    validate(game) {
      super.validate(game);

      // do we already have an active player?
      if (
        "" !== this.game.world._player &&
        "undefined" !== typeof this.game.world[this.game.world._player] &&
        Object.keys(this.game.world[this.game.world._player]).length > 0
      ) {
        var msg =
          "Player.js > " +
          this.game.world[this.game.world._player].name +
          " and " +
          this.name +
          " are both set is.active. " +
          "\nOnly one Player can be active. " +
          "Using " +
          this.game.world[this.game.world._player].name +
          ". ";
        this.game.log("warn", "critical", msg, "Character");

        this.is.active = false;
      } else {
        this.game.world._player = this.id;
        this.synonyms.push("me", "myself");
      }

      return true;
    }
  }
  adventurejs.Player = Player;
})();