// setRoom.js
(function () {
/* global adventurejs A */
var p = adventurejs.Game.prototype;
/**
* Move subject to the specified room.
* @memberOf adventurejs.Game
* @method adventurejs.Game#setRoom
* @param {Object} newRoom A room object to which to move subject.
* @param {Object} params
*/
p.setRoom = function Game_setRoom(newRoom, params = {}) {
if ("string" === typeof newRoom) newRoom = this.game.getAsset(newRoom);
if (!newRoom) {
this.game.log(
"L1522",
"warn",
"high",
`[setRoom.js] received invalid room ${newRoom}`,
"Game"
);
return false;
}
this.game.log(
"L1059",
"log",
"high",
`[setRoom.js] ${newRoom.name}`,
"Game"
);
var output_class = "";
if (params.output_class) {
output_class = params.output_class;
}
var currentRoomID = this.world._room;
var currentRoomObj = this.world[currentRoomID];
var input = this.getInput();
var player = this.getPlayer();
var subject = input.getSubject() || this.getPlayer();
var subject_is_player = subject.id === player.id;
var nest_parent_object = subject.getNestAsset();
var results, msg;
if (subject.has_tied_things_that_drag_on_travel) {
var dragged_things = subject.getTiedThingsThatDragOnTravel();
}
//console.warn( "dragged_things",dragged_things);
// old room can take action on exit
if (
"" !== currentRoomID &&
"undefined" !== typeof currentRoomObj &&
"undefined" !== typeof Object.keys(currentRoomObj) &&
0 < Object.keys(currentRoomObj).length
) {
// remove subject id string from old room's inventory
results = currentRoomObj.onRemoveThatFromThis(subject); // string
if ("undefined" !== typeof results) return results;
if (subject.isNested() && nest_parent_object.isDOV("ride")) {
// did away with the need for this by moving
// simple vehicles into subject 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 subject 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 subject's location to new room id string
// add subject ID string to new room's inventory
results = newRoom.onMoveThatToThis(subject, "in"); // string
if ("undefined" !== typeof results) return results;
subject.knowAsset(newRoom, true);
subject.seeAsset(newRoom, true);
// reset subject's coordinates, in case subject had to do something
// like climb a ladder to reach this room
// @TODO flying? zero-G? underwater?
subject.position = Object.assign(
subject.position,
newRoom.aspects.in.nest.initial_position
);
if (subject.isNested() && nest_parent_object.isDOV("ride")) {
// did away with the need for this by moving
// simple vehicles into subject 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 subject 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
if (subject_is_player) this.world._room = newRoom.id; // string
if (params.nestobject && params.nestprep) {
results = subject.onNestThisToThat(params.nestobject, params.nestprep);
if (A.isFalseOrNull(results)) {
this.game.log(
"L1060",
"error",
"high",
`[setRoom.js] onNestThisToThat failed to nest ${subject.id} in params.nestobject.id. `,
"Game"
);
}
}
if (!subject_is_player) {
if (msg) this.game.print(msg, output_class);
return;
}
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]);
subject.knowAsset(exit);
subject.seeAsset(exit);
}
// 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.printRoom();
};
})();