//createAsset.js
(function () {
/*global adventurejs A*/
"use strict";
var p = adventurejs.Game.prototype;
/**
* <pre class="display"><code class="language-javascript">MyGame.createAsset({ "class":"foo", "name":"bar", [...] })</code></pre>
* <p>
* <strong>createAsset()</strong> is a constructor function
* available to authors, for creating all instances of
* {@link adventurejs.Asset|Asset}
* and its subclasses, which includes most
* of the objects you'd put in a game world.
* </p>
* <p>
* The function takes one parameter: a generic
* object containing properties to be passed into a class instance.
* For most asset classes, the only required
* properties are 'class' and 'name'. You'd likely also want
* to specify 'location', though that's not a required property.
* Otherwise, an author only needs to define the properties
* they wish to revise away from default settings.
* </p>
* <p>
* For example, to create a simple brass key with
* default settings and put it in a particular location:
* </p>
* <pre class="display"><code class="language-javascript">MyGame.createAsset({
* "class":"Key",
* "name":"brass key",
* place: { in: "desk drawer" },
* });
* </code></pre>
* <p>
* At runtime, <strong>createAsset()</strong> returns a new instance
* of the given class, uses
* the given name to create an id, validates the object properties,
* initializes the new object, and then adds it to the game world.
* Failure to pass any of these steps will throw an error to
* the console.
* </p>
* <p>
* For more examples of how to use createAsset, see
* <a href="/doc/AssetReference__Overview.html">About Assets</a>
* and other How To docs under Get Started.
* </p>
* @memberOf adventurejs.Game
* @method adventurejs.Game#createAsset
* @kind function
* @param {Object} asset A generic object with properties to copy into a new classed object.
* @returns {Object} A new Asset of the class specified in asset.class.
*/
p.createAsset = function Game_createAsset(asset) {
/*
* Test whether it's a generic object
* because there was an early version
* where we were passing in classed objects,
* some of which might still be valid.
*/
if (asset.constructor.name === "Object") {
asset = this.validateAssetPrecursor(asset);
if (asset) {
asset = this.constructAsset(asset);
} else {
return false;
}
}
// has it already been defined?
if (this.world[asset.id]) {
var msg = `Warning: createAsset id ${asset.id} is used more than once. `;
if (asset.class === "Exit") {
msg += `This could happen if you define an exit in a room's exits property and also as a separate exit asset. `;
}
this.game.log("error", 0, msg, "Game");
return false;
}
// add classed object to game world
this.world[asset.id] = asset;
return asset;
};
})();