// findNestedAssetsWithProperty.js
(function () {
/*global adventurejs A*/
var p = adventurejs.Tangible.prototype;
/**
* Find assets that have a particular property, for example
* find all assets emitting light. Returns an array of assets.
* @memberOf adventurejs.Tangible
* @method adventurejs.Tangible#findNestedAssetsWithProperty
* @param {String} property
* @returns {Array}
*/
p.findNestedAssetsWithProperty = function Tangible_findProperty(
property,
params = {}
) {
if ("string" !== typeof property) return [];
const properties = property.split(".");
var contents = this.getAllNestedContents();
var foundInstances = [];
for (var i = 0; i < contents.length; i++) {
var asset_id = contents[i];
if (params.exclude?.includes(asset_id)) continue;
var asset = this.game.getAsset(asset_id);
if (!asset) continue;
let current = asset;
let found = true;
for (let j = 0; j < properties.length; j++) {
// If current is not an object, it can't have properties
if (
current === null ||
current === undefined ||
(typeof current !== "object" && j < properties.length - 1)
) {
found = false;
break;
}
// Check if the current object (or final primitive) has the property
if (
typeof current === "object" &&
Object.prototype.hasOwnProperty.call(current, properties[j])
) {
current = current[properties[j]];
} else if (
j === properties.length - 1 &&
current[properties[j]] !== undefined
) {
// If it's the last property in the chain and is a primitive,
// consider it found
current = current[properties[j]];
if (!current) {
found = false;
break;
}
} else {
found = false;
break;
}
}
if (found) foundInstances.push(asset);
}
return foundInstances;
};
})();