Base Entity Controller
The BaseEntityController class is what all other types of entity controllers inherit from.
Below is the SDK's internal implementation of the BaseEntityController for reference, giving direct insight into how it works.
/**
* 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.
*
* @public
*/
export default abstract class BaseEntityController {
/**
* A function that is called every tick. Useful for implementing
* tick logic without writing a new entity controller class.
*/
public onTick?: (entity: Entity, deltaTimeMs: number) => void;
/**
* A function that is called every tick with player input by a
* PlayerEntity with this controller attached. Useful for implementing
* tick logic without writing a new entity controller class.
*/
public onTickWithPlayerInput?: (entity: PlayerEntity, input: PlayerInput, cameraOrientation: PlayerCameraOrientation, deltaTimeMs: number) => void;
/**
* A function that is called when the controller is attached to an entity.
* Useful for implementing attach logic without writing a
* new entity controller class.
*/
public onAttach?: (entity: Entity) => void;
/**
* A function that is called when the controlled entity is despawned.
* Useful for implementing despawn logic without writing a
* new entity controller class.
*/
public onDespawn?: (entity: Entity) => void;
/**
* A function that is called when the controller is detached from an entity.
* Useful for implementing detach logic without writing a
* new entity controller class.
*/
public onDetach?: (entity: Entity) => void;
/**
* A function that is called when the controlled entity is spawned.
* Useful for implementing spawn logic without writing a
* new entity controller class.
*/
public onSpawn?: (entity: Entity) => void;
/**
* 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 {
if (this.onAttach) {
this.onAttach(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 {
if (this.onDespawn) {
this.onDespawn(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 {
if (this.onDetach) {
this.onDetach(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 {
if (this.onSpawn) {
this.onSpawn(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 {
if (this.onTickWithPlayerInput) {
this.onTickWithPlayerInput(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 {
if (this.onTick) {
this.onTick(entity, deltaTimeMs);
}
}
}
Diving Deeper
The BaseEntityController class is constantly evolving. You can find the latest BaseEntityController API Reference here.
If there are features that we don't currently support for the BaseEntityController that you'd like to see added to the HYTOPIA SDK, you can submit a feature request here.
Last updated