Detector¶
import { Detector } from "encompass-ecs";
A Detector is a subclass of Engine that provides a structure for a common pattern - performing a task for each Entity which has a particular combination of Components.
Detectors are defined by the Component types they track and the detect
function they implement.
Abstracts¶
-
Type<Component>[] component_types
¶ The Component types that will cause the Entity to be tracked by the Detector. Define using
@Detects
decorator.
-
detect
(entity, dt)¶ This callback is triggered every frame when an Entity has all the required component types specified by the
component_types
property.Arguments: - entity (Entity) – An entity that is being tracked by the Detector.
- dt (number) – Delta time.
Example¶
import { Detector, Entity } from "encompass-ecs";
import { PositionComponent } from "src/components/position";
import { VelocityComponent } from "src/components/velocity";
import { MotionMessage } from "src/messages/motion";
@Detects(PositionComponent, VelocityComponent)
@Emits(MotionMessage)
class MotionDetector extends Detector {
protected detect(entity: Entity) {
const position_component = entity.get_component(PositionComponent);
const velocity_component = entity.get_component(VelocityComponent);
const motion_message = this.create_component_message(MotionMessage, position_component);
motion_message.x_delta = velocity_component.x;
motion_message.y_delta = velocity_component.y;
}
}