Base Entity Controller
/**
* A base class for entity controller implementations.
*
* @remarks
* The BaseEntityController should be extended
* by a more specific entity controller that you or a
* plugin implements. Entity controllers are intended to
* be used as one controller instance per entity, but
* are flexible enough for edge cases such as if you want to create
* niche behavior of one controller for many entities that
* behave in unison.
*
* <h2>Events</h2>
*
* This class is an EventRouter, and instances of it emit
* events with payloads listed under {@link BaseEntityControllerEventPayloads}
*
* @public
*/
export default abstract class BaseEntityController extends EventRouter {
/**
* Override this method to handle the attachment of an entity
* to your entity controller.
* @param entity - The entity to attach the controller to.
*/
public attach(entity: Entity): void {
this.emit(BaseEntityControllerEvent.ATTACH, { entity });
};
/**
* Override this method to handle the despawn of an entity
* from your entity controller.
* @param entity - The entity to despawn.
*/
public despawn(entity: Entity): void {
this.emit(BaseEntityControllerEvent.DESPAWN, { entity });
};
/**
* Override this method to handle the detachment of an entity
* from your entity controller.
* @param entity - The entity to detach.
*/
public detach(entity: Entity): void {
this.emit(BaseEntityControllerEvent.DETACH, { entity });
};
/**
* Override this method to handle the spawning of an entity
* to your entity controller.
* @param entity - The entity to spawn.
*/
public spawn(entity: Entity): void {
this.emit(BaseEntityControllerEvent.SPAWN, { entity });
};
/**
* Override this method to handle entity movements
* based on player input for your entity controller.
* This is called every tick by a PlayerEntity with a
* entity controller.
* @param entity - The entity to tick.
* @param input - The current input state of the player.
* @param cameraOrientation - The current camera orientation state of the player.
* @param deltaTimeMs - The delta time in milliseconds since the last tick.
*/
public tickWithPlayerInput(entity: PlayerEntity, input: PlayerInput, cameraOrientation: PlayerCameraOrientation, deltaTimeMs: number): void {
this.emit(BaseEntityControllerEvent.TICK_WITH_PLAYER_INPUT, { entity, input, cameraOrientation, deltaTimeMs });
}
/**
* Override this method to handle entity movements
* based on your entity controller.
* @param deltaTimeMs - The delta time in milliseconds since the last tick.
*/
public tick(entity: Entity, deltaTimeMs: number): void {
this.emit(BaseEntityControllerEvent.TICK, { entity, deltaTimeMs });
}
}Diving Deeper
Last updated
