Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// setPlayerRoom.js

(function () {
  /*global adventurejs A*/
  "use strict";

  var p = adventurejs.Game.prototype;

  /**
   * Move player to the specified room.
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#setPlayerRoom
   * @param {Object} newRoom A room object to which to move player.
   * @param {Object} params
   */
  p.setPlayerRoom = function Game_setPlayerRoom(newRoom, params) {
    this.game.log("log", "high", "setPlayerRoom.js > " + newRoom.name, "Game");

    var output_class = "";
    if (
      "undefined" !== typeof params &&
      "undefined" !== typeof params.output_class
    ) {
      output_class = params.output_class;
    }

    var currentRoomID = this.world._currentRoom;
    var currentRoomObj = this.world[currentRoomID];
    var player = this.getPlayer();
    var nest_parent_object = player.getNestAsset();
    var results, msg;
    if (player.has_tied_things_that_drag_on_travel) {
      var dragged_things = player.getTiedThingsThatDragOnTravel();
    }

    //console.warn( "dragged_things",dragged_things);

    // old room can take action on exit
    if (
      "" !== currentRoomID &&
      "undefined" !== typeof currentRoomObj &&
      "undefined" !== Object.keys(currentRoomObj) &&
      0 < Object.keys(currentRoomObj).length
    ) {
      // remove player id string from old room's inventory
      results = currentRoomObj.onRemoveThatFromThis(player); // string
      if ("undefined" !== typeof results) return results;

      if (player.isNested() && nest_parent_object.is.rideable) {
        // did away with the need for this by moving
        // simple vehicles into player inventory
        // but leaving the check for other potential uses
        // results = currentRoomObj.onRemoveThatFromThis( nest_parent_object );
        // if( false === results ) { return false; }
        // else if ( null === results ) { return null; }
      }

      // if player is dragging anything at the end of a rope,
      // its parent, whether room or thing, can act on it too
      if ("undefined" !== typeof dragged_things) {
        for (var i = 0; i < dragged_things.length; i++) {
          var dragged_thing = this.game.getAsset(dragged_things[i]);
          //console.warn( "dragged_thing",dragged_thing );
          //console.warn( "dragged_thing.getPlaceAsset()",dragged_thing.getPlaceAsset() );
          results = dragged_thing
            .getPlaceAsset()
            .onRemoveThatFromThis(dragged_thing);
          if ("undefined" !== typeof results) return results;
        }
      }
    }

    // set player's location to new room id string
    // add player ID string to new room's inventory
    results = newRoom.onMoveThatToThis(player, "in"); // string
    if ("undefined" !== typeof results) return results;

    // reset player's coordinates, in case player had to do something
    // like climb a ladder to reach this room
    // TODO flying? zero-G? underwater?
    player.position = Object.assign(
      player.position,
      newRoom.aspects.in.player.initial_position
    );

    if (player.isNested() && nest_parent_object.is.rideable) {
      // did away with the need for this by moving
      // simple vehicles into player inventory
      // but leaving the check for other potential uses
      // results = newRoom.onMoveThatToThis( nest_parent_object, "in" ); // string
      // if( false === results ) { return false; }
      // else if ( null === results ) { return null; }
    }

    // if player is dragging anything at the end of a rope,
    // new room can act on that too
    if ("undefined" !== typeof dragged_things) {
      for (var i = 0; i < dragged_things.length; i++) {
        var dragged_thing = this.game.getAsset(dragged_things[i]);
        results = newRoom.onMoveThatToThis(dragged_thing, "in");
        if ("undefined" !== typeof results) return results;
      }
    }

    // set new room
    this.world._currentRoom = newRoom.id; // string

    if (
      "undefined" !== typeof params &&
      "undefined" !== typeof params.nestobject &&
      "undefined" !== typeof params.nestprep
    ) {
      results = player.onNestThisToThat(params.nestobject, params.nestprep);
      if (A.isFalseOrNull(results)) {
        this.game.log(
          "error",
          "high",
          "Game.play > setPlayerRoom > onNestThisToThat failed to nest player in " +
            params.nestobject.id +
            ". ",
          "Game"
        );
      }
    }
    this.updateDisplayRoom();
    this.updateDisplayCompasses();

    var directions = Object.keys(newRoom.exits);
    for (var i = 0; i < directions.length; i++) {
      var exit = this.getAsset(newRoom.id + "_" + directions[i]);
      exit.setKnown();
      exit.setSeen();
    }

    // parsed input can be set excludeRoomDescriptions
    // at the moment "go to" is the only invoker
    if (
      "undefined" !== typeof this.parser.input_queue[0] &&
      "undefined" !==
        typeof this.parser.input_queue[0].excludeRoomDescriptions &&
      this.parser.input_queue[0].excludeRoomDescriptions
    ) {
      return;
    }

    if ("undefined" !== typeof dragged_things) {
      for (var i = 0; i < dragged_things.length; i++) {
        var dragged_thing = this.game.getAsset(dragged_things[i]);
        var rope_asset = dragged_thing.getThingThisIsTiedToPlayerBy();
        msg = "$(We) drag " + dragged_thing.articlename + " along";
        if ("undefined" !== typeof rope_asset) {
          msg += " at the end of " + rope_asset.articlename;
        }
        msg += ". ";
      }
    }

    // print input, room name, room description
    if (msg) this.game.print(msg, output_class);
    this.printCurrentRoom();
  };
})();