MMapComplexEntity

ComplexEntity is an entity that can contain child components, but the methods for adding them to the subtree and removing them from it are internal.

The mappable module has the abstract MMapComplexEntity class inherited from MMapEntity, so it inherits all of its properties and methods. It also has additional methods for working with its own subtree. You can create a Complex Entity component using inheritance from the MMapComplexEntity class:

type MMapSomeLayerProps = {
  visible: boolean;
  source: string;
};

class MMapSomeLayer extends mappable.MMapComplexEntity<MMapSomeLayerProps> {
  private _dataSource?: MMapTileDataSource;
  private _layer?: MMapLayer;
  constructor(props: MMapSomeLayerProps) {
    super(props);
    const {source, visible} = this._props;
    // Create child entity instances.
    this._dataSource = new mappable.MMapTileDataSource({id: source});
    this._layer = new mappable.MMapLayer({source, type: 'ground'});
    // Add child entities to the component's subtree.
    this.addChild(this._dataSource);
    if (visible) {
      this.addChild(this._layer);
    }
  }
  protected _onUpdate(propsDiff: Partial<MMapSomeLayerProps>): void {
    if (propsDiff.visible !== undefined) {
      // Remove or add an entity depending on the "visible" value.
      propsDiff.visible ? this.addChild(this._layer) : this.removeChild(this._layer);
    }
  }
}

In the example above, we used methods for interacting with own subtree. The addChild method adds an external entity to the subtree. All child entities are stored in the children array, so the method takes a sequence number in place of which the entity will be added to that array as a second optional argument. The removeChild method removes a child entity from the subtree.

To get a list of child elements, use the read-only children array.

The MMapComplexEntity class constructor also has a second optional argument (options), which is an object with the following properties:

  • children: An array of child entities that will be added to the subtree immediately after the class instance is initialized.
  • container: If true, creates a child proxy container that will contain a subtree of child components. By default, doesn't create a proxy container.

For more information about proxy containers, see the section.