Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// play.js

(function () {
  /*global adventurejs A*/

  var p = adventurejs.Game.prototype;

  /**
   * Validate and initialize all classed game objects in world.
   * Validation is internal to each object.
   * Initialization draws connections to other objects.
   * @method adventurejs.Game#play
   * @memberOf adventurejs.Game
   * @returns adventurejs.Game
   */
  p.play = function Game_play() {
    var startPlayTime = new Date().getTime();

    this.game.log("L1054", "log", 1, "play.js > Validating objects.", "Game");
    this.game_state = this.game_states.validateAssets;
    if (!this.validateAssets()) {
      return false;
    }

    this.game.log(
      "L1055",
      "log",
      1,
      "play.js > Creating deferred objects.",
      "Game"
    );
    this.game_state = this.game_states.createDeferredAssets;
    if (!this.createDeferredAssets()) {
      return false;
    }

    this.game.log("L1056", "log", 1, "play.js > Initializing objects.", "Game");
    this.game_state = this.game_states.initializeAssets;
    if (!this.initializeAssets()) {
      return false;
    }

    this.game.log(
      "L1057",
      "log",
      1,
      "play.js > Sorting lookup values.",
      "Game"
    );
    this.game_state = this.game_states.sortLookupValues;
    this.sortLookupValues();

    this.game_state = this.game_states.worldSetDefaultAssets;
    // if no rooms were created, create one
    if (0 === this.room_lookup.length) {
      this.createAsset({
        class: "Room",
        name: "Empty Room",
        descriptions: {
          look: "The emptiest room $(we've) ever seen.",
          brief: "The emptiest room ever.",
          verbose: "This is, by far, the emptiest room $(we've) ever seen.",
        },
      });
      this.game.world.empty_room.validate();
      this.game.world.empty_room.initialize();
    }

    // if no players were created, create one
    if (!this.world._player) {
      this.createAsset({
        class: "Player",
        name: "Unnamed Hero",
        is: { active: true },
        place: { in: this.room_lookup[0] },
      });
      this.game.world.unnamed_hero.validate();
      this.game.world.unnamed_hero.initialize();
    }

    this.game_state = this.game_states.setPlayerRoom;
    var player = this.getPlayer();
    var startRoom = player.getPlaceAsset();
    var params = {};
    player.is.active = true;
    if (player.isNested()) {
      var nestprep = player.getNestPreposition();
      var nestobject = player.getNestAsset();
      params.nestobject = nestobject;
      params.nestprep = nestprep;
    }
    if (!startRoom) startRoom = this.getAsset(this.room_lookup[0]);
    this.setPlayerRoom(startRoom, params);
    this.updateDisplayInventory();

    this.scorecard.updateScore();

    this.game_state = this.game_states.worldSaveBaseline;

    // save baseline world for later comparisons

    // this.baseline.world = A.cloneWorld.call(this, this.world);
    this.baseline.world = A.clone.call(this, this.world);
    // @TODO cloneWorld is a specialized function but I don't remember
    // specifically why it was useful vs my generic clone function,
    // which seems to work ok and much faster

    this.game.log(
      "L1058",
      "log",
      "high",
      `play.js > READY! Startup took ${
        (new Date().getTime() - startPlayTime) / 1000
      } seconds.`,
      "Game"
    );

    //this.game.display.inputEl.focus();

    this.game_state = this.game_states.playing;

    return this;
  };
})();