// GraduatedController.js
(function () {
/*global adventurejs A*/
"use strict";
/**
* @ajspath adventurejs.Atom.Asset.Matter.Tangible.Thing.GraduatedController
* @augments adventurejs.Thing
* @class adventurejs.GraduatedController
* @ajsconstruct MyGame.createAsset({ "class":"GraduatedController", "name":"foo", [...] })
* @ajsconstructedby adventurejs.Game#createAsset
* @ajsnavheading MiscAssetClasses
* @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 A controller with multiple settings, like a dial.
* @classdesc
* <p>
* <strong>GraduatedController</strong> is a subclass of
* {@link adventurejs.Thing|Thing} and a base class for controllers
* that have multiple positions which can be used to affect
* levels of flow of
* {@link adventurejs.SubstanceEmitter|SubstanceEmitters},
* such as setting a sink handle which can be set to low, medium,
* or high or any number of positions in between. This is possibly
* overkill but that's the kind of game framework this is.
* </p>
* <h3 class="examples">Example:</h3>
* <pre class="display"><code class="language-javascript">MyGame.createAsset({
* class: "FaucetHandle",
* name: "sink's cold water handle",
* synonyms: [ "handle", "sink handle" ],
* adjectives: [ "cold water", "sink", "porcelain" ],
* place: { attached: "sink" },
* description: "The sink's cold water handle. ",
* target_id: "sink faucet",
* set_substance_id: "water",
* set_substance_temperature: 15
* });
* </code></pre>
**/
class GraduatedController extends adventurejs.Thing {
constructor(name, game_name) {
super(name, game_name);
this.class = "GraduatedController";
this.singlePluralPairs = [["controller", "controllers"]];
this.descriptions.look = "It's a controller.";
this.is.listed_in_parent = false;
this.can.turn = true;
this.dimensions.width = 1;
this.dimensions.height = 1;
this.unsetDOV("take");
this.setDOVs([{ turn: { with_nothing: true } }]);
this.control_positions = 2; // toggle
//this.control_positions = 5; // range
this.current_position = 0; // off
this.set_substance_id = "";
this.set_substance_temperature = 25;
this.turnOn_doAfterTry = function (params) {
//var game = window[game_name];
var input = this.game.getInput();
var direct_object = input.getAsset(1);
var msg = "";
if (2 > direct_object.control_positions) {
// it doesn't turn
msg += `${direct_object.Articlename} can't be turned on. `;
if (msg) this.game.print(msg, input.output_class);
return null;
}
if (
direct_object.current_position + 1 ===
direct_object.control_positions
) {
// already as open as it can be
msg = `${direct_object.Articlename} is already turned on. `;
if (msg) this.game.print(msg, input.output_class);
return null;
}
return true;
};
this.turnOn_doBeforeSuccess = function (params) {
var game = window[game_name];
var input = game.getInput();
var direct_object = input.getAsset(1);
var msg = "";
var results;
direct_object.current_position = direct_object.current_position + 1;
// if controller has more than 2 positions
if (2 < direct_object.control_positions) {
// is it fully on?
if (
direct_object.current_position + 1 ===
direct_object.control_positions
) {
msg += `$(We) turn ${direct_object.articlename} all the way on. `;
} else {
// or not?
msg += `$(We) turn ${direct_object.articlename} further on. `;
}
} else {
msg += `$(We) turn ${direct_object.articlename} to on. `;
}
// if this has a target, inform target of new setting
var indirect_object = game.getAsset(direct_object.control_target_id);
//if( "undefined" !== typeof indirect_object
if (
Object(indirect_object) === indirect_object &&
"function" === typeof indirect_object.onChangeGraduatedController
) {
results = indirect_object.onChangeGraduatedController(direct_object);
if (A.isFalseOrNull(results)) return results;
// if successful, target might return a string for output
if ("string" === typeof results) msg += results;
}
if (msg) game.print(msg, input.output_class);
return null;
};
this.turnOff_doAfterTry = function (params) {
var game = window[game_name];
var input = game.getInput();
var direct_object = input.getAsset(1);
var msg = "";
if (2 > direct_object.control_positions) {
// it doesn't actually turn off
msg = `${direct_object.Articlename} can't be turned off. `;
if (msg) game.print(msg, input.output_class);
return null;
}
if (direct_object.current_position === 0) {
// already as off as it can be
msg = `${direct_object.Articlename} is already turned off. `;
if (msg) game.print(msg, input.output_class);
return null;
}
return true;
};
this.turnOff_doBeforeSuccess = function (params) {
var game = window[game_name];
var input = game.getInput();
var direct_object = input.getAsset(1);
var msg = "";
var results;
direct_object.current_position = direct_object.current_position - 1;
// if controller has more than 2 positions
if (2 < direct_object.control_positions) {
// is it fully off?
if (direct_object.current_position === 0) {
msg += `$(We) turn ${direct_object.articlename} all the way off. `;
} else {
// or not?
msg += `$(We) turn ${direct_object.articlename} further off. `;
}
} else {
msg += `$(We) turn ${direct_object.articlename} to off. `;
}
// if this has a target, inform target of new setting
var indirect_object = game.getAsset(direct_object.control_target_id);
//if( "undefined" !== typeof indirect_object
if (
Object(indirect_object) === indirect_object &&
"function" === typeof indirect_object.onChangeGraduatedController
) {
results = indirect_object.onChangeGraduatedController(direct_object);
if (A.isFalseOrNull(results)) return results;
// if successful, target might return a string for output
if ("string" === typeof results) msg += results;
}
if (msg) game.print(msg, input.output_class);
return null;
};
this.dov.turn.doAfterTry = function (params) {
var input = this.game.getInput();
var direct_object = input.getAsset(1);
var direct_preposition = input.getPreposition(1);
// sentence structure: verb preposition noun
if (input.hasStructure("verb preposition noun")) {
if (direct_preposition === "on") return this.turnOn_doAfterTry();
if (direct_preposition === "off") return this.turnOff_doAfterTry();
} // verb preposition noun
// sentence structure: verb noun
if (input.hasStructure("verb noun")) {
if (direct_object.control_positions < 2) {
// it doesn't actually turn
var msg = direct_object.Articlename + " doesn't turn. ";
if (msg) this.game.print(msg, input.output_class);
return null;
}
} // verb noun
return true;
};
this.dov.turn.doBeforeSuccess = function (params) {
var input = this.game.getInput();
var direct_object = input.getAsset(1);
var direct_preposition = input.getPreposition(1);
var msg = "";
var results;
// sentence structure: verb preposition noun
if (input.hasStructure("verb preposition noun")) {
if (direct_preposition === "on") return this.turnOn_doBeforeSuccess();
if (direct_preposition === "off")
return this.turnOff_doBeforeSuccess();
} // verb preposition noun
// sentence structure: verb noun
if (input.hasStructure("verb noun")) {
if (direct_object.control_positions === 2) {
// Turn doesn't specify a direction.
// If element only has two positions then it's a toggle
// and we can assume player meant to open or close.
direct_object.current_position = Math.abs(
direct_object.current_position - 1
);
msg += `$(We) turn ${direct_object.articlename} to ${
direct_object.current_position == 1 ? "open" : "closed"
}. `;
} else if (2 < direct_object.control_positions) {
// it has multiple positions, but turn doesn't specify direction
// so how to decide what direction?
if (
direct_object.current_position <
direct_object.control_positions - 1
) {
direct_object.current_position++;
} else {
direct_object.current_position--;
}
}
// if this has a target, inform target of new setting
var indirect_object = this.game.getAsset(
direct_object.control_target_id
);
//if( "undefined" !== typeof indirect_object
if (
Object(indirect_object) === indirect_object &&
"function" === typeof indirect_object.onChangeGraduatedController
) {
results =
indirect_object.onChangeGraduatedController(direct_object);
if (A.isFalseOrNull(results)) return results;
// if successful, target might return a string for output
if ("string" === typeof results) msg += results;
}
if (msg) this.game.print(msg, input.output_class);
return null;
} // verb noun
return true;
};
}
get set_substance_id() {
return this.__set_substance_id;
}
set set_substance_id(substance_id) {
if ("string" !== typeof substance_id) {
var msg = this.id + ".set_substance_id must be a string. ";
this.game.log("warn", "critical", msg);
return;
}
this.__set_substance_id = A.serialize(substance_id);
}
get set_substance_temperature() {
return this.__set_substance_temperature;
}
set set_substance_temperature(substance_temperature) {
if ("number" !== typeof substance_temperature) {
var msg = this.id + ".set_substance_temperature must be a number. ";
this.game.log("warn", "critical", msg);
return;
}
this.__set_substance_temperature = substance_temperature;
}
}
adventurejs.GraduatedController = GraduatedController;
})();