Advanced Scripting:Custom Classes
AdventureJS comes with many pre-defined asset classes. Authors can easily
extend existing classes to create new classes. Asset classes inherit
properties from their parent classes, from the basic
Asset class all the way down the
inheritence chain. For instance, lets say we want to extend the built-in
Potion class to create a PotionMagic class, and then subclass that to create a
variety of PotionMagic subclasses: PotionHealth, PotionSpeed, PotionStrength,
etc. We use the
game.createClass()
function to do this. createClass() is structured just like
game.createAsset(), with an additional extends property.
Examples
AdventureJS already has a basic
Potion class, which subscribes to
the verb drink but doesn't do much else. Here,
we will define a subclass of Potion called
PotionMagic and set its
asset.dov.drink.on_success to call
asset.applyPotion().
MyGame.createClass(
extend: "Potion",
class: "PotionMagic",
dov: {
drink: {
then_destroy: true,
on_success: function () {
this.applyPotion();
},
},
},
applyPotion: function () {
// the base PotionMagic class has no default behavior
// when we create subclasses, all we need to do is set their applyPotion method
},
);
Now we will create a set of subclasses with unique behaviors in their
applyPotion() methods.
MyGame.createClass(
extend: "PotionMagic",
class: "PotionSpeed",
applyPotion: function () {
let msg = `{We} vibrate with a boost of speed. `;
MyGame.print(msg);
if ("undefined" === MyGame.getPlayer().speed)
MyGame.getPlayer().speed = 1;
MyGame.getPlayer().speed = MyGame.getPlayer().speed * 2;
},
);
MyGame.createClass(
extend: "PotionMagic",
class: "PotionStrength",
applyPotion: function () {
let msg = `{We} feel strong. Really strong! `;
MyGame.print(msg);
if ("undefined" === MyGame.getPlayer().strength)
MyGame.getPlayer().strength = 1;
MyGame.getPlayer().strength = MyGame.getPlayer().strength * 2;
},
);
MyGame.createClass(
extend: "PotionMagic",
class: "PotionHealth",
applyPotion: function () {
if ("undefined" === MyGame.getPlayer().health)
MyGame.getPlayer().health = 100;
let msg =
MyGame.getPlayer().health < 100
? `{Our} health is improved. `
: `{We} feel a faint warm glow, but nothing else happens. `;
MyGame.print(msg);
MyGame.getPlayer().health = Math.max(MyGame.getPlayer().health + 10, 100);
},
);
Now that we've created these new custom classes, we can add assets of that type to our game.
MyGame.createAsset({
class: "PotionStrength",
name: "strength potion",
article: "a",
place: { in: "medicine cabinet" },
description: "It's a strength potion. ",
});
MyGame.createAsset({
class: "PotionSpeed",
name: "speed potion",
article: "a",
place: { in: "medicine cabinet" },
description: "It's a speed potion. ",
});
MyGame.createAsset({
class: "PotionHealth",
name: "health potion",
article: "a",
place: { in: "medicine cabinet" },
description: "It's a health potion. ",
});
Though we've set an applyPotion() function for each of our potion
subclasses at the class level, we can still customize subclasses the same as
any other AdventureJS asset class. The health potion defined above only adds
10 points of health. In the following example we'll make a one-off potion that
restores full health.
MyGame.createAsset({
class: "PotionHealth",
name: "resurrection potion",
article: "a",
place: { in: "medicine cabinet" },
description: "It's a resurrection potion. ",
applyPotion: function () {
if ("undefined" === MyGame.getPlayer().health)
MyGame.getPlayer().health = 100;
let msg =
MyGame.getPlayer().health < 100
? `{We're} miraculously healed! `
: `{We} feel a burst of warmth. `;
MyGame.print(msg);
MyGame.getPlayer().health = 100;
},
});