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

  /**
   * @ajspath adventurejs.Atom.StateManager.Asset_Is
   * @augments adventurejs.StateManager
   * @class adventurejs.Asset_Is
   * @ajsnavheading StateClasses
   * @param {String} game_name Name of top level game instance that is scoped to window.
   * @param {String} name Instance name.
   * @summary A container for state variables.
   * @classdesc
   * <p>
   * <strong>Asset_Is</strong> is a state management class for all {@link adventurejs.Asset|Assets}.
   * used to store general state variables.
   **/

  class Asset_Is extends adventurejs.StateManager {
    constructor(name = "is", game_name, context_id) {
      // Call the constructor of the super class
      super(name, game_name, context_id);

      this.class = "Asset_Is";

      /**
       * Used to track whether player knows about an asset.
       * @var {Boolean} adventurejs.Asset#is!known
       * @default false
       */
      this._known = false;

      /**
       * Set to true when an object is removed from the game
       * by destruction.
       * @var {Boolean} adventurejs.Asset#is!destroyed
       * @default false
       */
      this.destroyed = false;

      /**
       * Set to true when an object is in the game.
       * @var {Boolean} adventurejs.Asset#is!extant
       * @default true
       */
      this.extant = true;

      /**
       * Set whether object is a collection of other object IDs.
       * @var {Boolean} adventurejs.Asset#is!collection
       * @default true
       */
      this.collection = false;

      /**
       * Used at runtime to keep track of asset's validation state.
       * @var {Boolean} adventurejs.Asset#is!validated
       * @default false
       */
      this.validated = false;

      /**
       * Used at runtime to keep track of asset's initialization state.
       * @var {Boolean} adventurejs.Asset#is!initialized
       * @default false
       */
      this.initialized = false;

      /**
       * Almost all assets are given a name which is then used to create
       * their id, with some exceptions.
       * For example, exit ids are generated by taking the name of the
       * exit's room + the exit's direction.
       * @var {Boolean} adventurejs.Asset#is!nameless
       * @default false
       */
      this.nameless = false;

      /**
       * Almost all tangible assets have a singular location.
       * Global assets are available in all locations
       * in order to catch player input.
       * @var {Boolean} adventurejs.Asset#is!global
       * @default false
       */
      this.global = false;

      /**
       * Abstractions are universally available and bypass reachability tests.
       * @var {Boolean} adventurejs.Asset#is!abstract
       * @default false
       */
      this.abstract = false;

      /**
       * Placeholders are universally available to catch references to non-existent
       * elements, such as when player tries to use an exit that doesn't exist.
       * @var {Boolean} adventurejs.Asset#is!placeholder
       * @default false
       */
      this.placeholder = false;

      /**
       * A data asset represents data such as a name or password.
       * Ex: "enter password", "tell name to guard"
       * @var {Boolean} adventurejs.Asset#is!data
       * @default false
       */
      this.data = false;

      /**
       * Meant for use to distinguish some assets as being singular
       * in the game world.
       * @var {Boolean} adventurejs.Asset#is!singleton
       * @default false
       * @todo Have only applied this to several special global assets,
       * and have not implemented any code around it. Is it still useful?
       */
      this.singleton = false;

      /**
       * Define whether an asset is plural, as opposed to singular,
       * in the sense of eyeglasses vs a drinking glass. Used to
       * help select pronouns.
       * @var {Boolean} adventurejs.Asset#is!plural
       * @default false
       */
      this.plural = false;

      return this;
    }

    /**
     * We store an asset's known state per character so that
     * each character can have distinct knowledge. However,
     * the known property needs to be accessed during game
     * construction, before the player character is available.
     * So, during construction, we save state to an object
     * property, and during play, we save state to the active
     * character.
     */
    get known() {
      return this._known;
    }

    set known(value) {
      let self = this.context;
      let linked_asset;
      this._known = value;
      if (self && self.linked_asset) {
        linked_asset = this.game.getAsset(self.linked_asset);
      }
      if (linked_asset) {
        linked_asset.is._known = value;
      }
      if (self && self.aspects) {
        for (let item in self.aspects) {
          let aspect = self.aspects[item];
          if (
            aspect.name === "in" &&
            self.isDOV("open") &&
            self.is.closed &&
            self.appearance.opacity >= 1
          )
            continue;
          if (aspect.know_contents_with_parent) {
            let contents = aspect.contents;
            for (let i = 0; i < contents.length; i++) {
              let nested_asset = self.game.getAsset(contents[i]);
              nested_asset.is._known = value;
            }
          }
          if (aspect.vessel?.know_contents_with_parent) {
            aspect.vessel.is._known = true;
          }
        }
      }
    }
  }
  adventurejs.Asset_Is = Asset_Is;
})();