Overview

Overview

Mixins are functions that can be used to modularly extend actor and pawns. The "mix" and "with" operators are semantic sugar to make the construction of the composite class look nice.

It's Worldcore convention to prefix actor mixins with "AM_" and pawn mixins with "PM_". In some cases, actor and pawn mixins are designed to work together. For example:

class SpatialActor extends mix(Actor).with(AM_Spatial) {
   get pawn() {return SpatialPawn}
}
SpatialActor.register("SpatialActor");


class SpatialPawn extends mix(Pawn).with(PM_Spatial) {
}

The spatial mixins give the actor and the pawn the ability to occupy a position in 3d space. We use them together because the mixins also contain code to make sure the position of the pawn tracks the position of the actor.

Worldcore comes with a standard set of built-in mixins. Some are actor/pawn pairs, while others are used with actors or pawns independently. You can also define your own mixins.

Actor Mixins

Actor mixins contain methods that extend the actor class they're added to. They can have an init() method that overrides the actor's base init. For example:

const AM_Color = superclass extends superclass {
   init(...args) {
     super.init(...args);
     this.red = 1;
     this.blue = 1;
     this.green = 1;
   }

   get rgb() { return [this.red, this.blue, this.green]}
};
 RegisterMixin(AM_Color);

This defines a mixin that adds color properties to an actor. Note that init() passes all arguments straight through to super.init(). Also, the mixin is registered after it is defined. (This ensures that changing the mixin won't cause divergence in the model.)

Pawn Mixins

Pawn mixins are similar to actor mixins, but they have constructors instead of init(), and they don't need to be registered:

const PM_Shape = superclass extends superclass {
   constructor(...args) {
     super(...args);
     this.shape = "sphere"
   }

   makeCube() { this.shape = "cube"}
};

Inheritance

Mixins can inherit from other mixins, just like classes can inherit from other classes:

const AM_TransparentColor = superclass extends AM_color(superclass) {
   init(...args) {
     super.init(...args);
     this.opacity = 1;
   }

   show() {this.opacity = 1}
   hide() {this.opacity = 0}
};
 RegisterMixin(AM_TransparentColor);

This extends the AM_Color mixin to have an opacity property. If you add a child mixin to an actor or a pawn you get all the properties and methods of the parent as well. You don't need to explicitly add both the parent and the child.