MMapEntity

Entity is a base entity that cannot contain its own subtree, but can be added to another entity.

The mappable module has the abstract MMapEntity class to create entity components of the Entity type using inheritance from that class:

type MMapSomeEntityProps = {
  name: string;
};

class MMapSomeEntity extends mappable.MMapEntity<MMapSomeEntityProps> {
  public isAttached: boolean;
  constructor(props: MMapSomeEntityProps) {
    super(props);
    this.isAttached = false;
    // Additional actions that can be performed in the class constructor.
  }
  protected _onAttach(): void {
    this.isAttached = true;
    // Additional actions that can be performed when attaching an object.
  }
  // ...
}

The MMapEntity class contains various protected methods for overriding to create event handlers for different events in the entity lifecycle.

The _onAttach and _onDetach methods are handlers for adding and removing an entity from a parent subtree:

class MMapSomeEntity extends mappable.MMapEntity<MMapSomeEntityProps> {
  // When adding an entity to a parent subtree.
  protected _onAttach(): void {
    console.log('attach entity');
  }

  // When removing an entity from a parent subtree.
  protected _onDetach(): void {
    console.log('detach entity');
  }
}

Each entity can contain custom parameters in the form of an object with values. The class constructor takes a parameter object as the first argument. After a component is created, you can update properties using the update method, specifying only the updated values (other values will not change). To get actual values of entity parameters, use the _props property.

The _onUpdate method is a handler for updating entity parameters using the update method. Parameter values that updated the old parameter values come as arguments:

type MMapSomeEntityProps = {
  visible: boolean;
};
class MMapSomeEntity extends mappable.MMapEntity<MMapSomeEntityProps> {
  private _visible = false;
  // Triggers when entity parameters are updated.
  protected _onUpdate(propsDiff: Partial<MMapSomeEntityProps>, oldProps: MMapSomeEntityProps): void {
    // Because it's the parameter difference being passed, we should check that the new value isn't undefined before saving it.
    if (propsDiff.visible !== undefined) {
      this._visible = propsDiff.visible;
    }
    // this._props will contain updated values.
  }
}

The MMapEntity entity instance also has references to other related entities in the tree. To get an instance of the parent entity, use the read-only parent property that returns a MMapComplexEntity entity. The similar root property returns the root entity of the MMap tree.