// renderIfAssetIs.js
(function () {
/* global AdventureJS A */
var p = AdventureJS.Game.prototype;
/**
*
* @method AdventureJS.Game#renderIfAssetIs
* @memberOf AdventureJS.Game
* @param {String} token
*/
p.renderIfAssetIs = function Game_renderIfAssetIs(token) {
let rendered = "";
let [, condition, substitution] = token.match(/^\[(.*?)\]\s*(.*)$/s);
let preposition, asset_name, asset, wearing, match, target;
// condition
// find asset name between if and is, and property after is
match = condition.match(/if\s+(.*?)\s+is\s+(.*)/s);
if (match) {
asset_name = match[1].trim();
condition = match[2].trim(); // might still be two words
}
if (asset_name) asset = this.game.getAsset(asset_name);
preposition = ["in ", "on ", "under ", "behind ", "attached "].find(
(preposition) => condition.startsWith(preposition)
);
// if we find a preposition,
// treat it as "is x in y" ?
if (preposition) {
// strip out preposition
preposition = preposition.trim();
target = condition.replace(preposition, "").trim();
// what remains should be an asset name or id
target = this.game.getAsset(target);
// if we have both...
if (target && preposition) {
if (asset.$is(preposition, target)) {
rendered = substitution;
}
}
}
// otherwise treat condition as a property
else if (asset?.$is(condition)) {
rendered = substitution;
}
return rendered;
}; //
})();