EntityRenderer

import { EntityRenderer } from "encompass-ecs";

An EntityRenderer provides a structure for the common pattern of drawing an Entity which has a particular collection of Components and a specific type of DrawComponent. They also have the ability to draw DrawComponents at their specific layer.

EntityRenderers are defined by the Component types and DrawComponent type they track, and the render function they implement.

Decorators

@Renders(draw_component_type, ...component_type_args)

Writes MessageTypes to the emit_message_types property.

Arguments:
  • draw_component_type (Type<DrawComponent>) – The DrawComponent type which will be tracked by the EntityRenderer.
  • component_type_args (Type<Component>[]) – The other Component types which are required to be present for the EntityRenderer to track the Entity.

Abstracts

render(entity)

This callback is triggered by the World draw function. Place your drawing code inside this function.

Arguments:
  • entity (Entity) – An Entity which has all component_types and the draw_component_type.

Example

import { Entity, EntityRenderer } from "encompass-ecs";
import { CanvasComponent } from "game/components/canvas";
import { PositionComponent } from "game/components/position";

@Renders(CanvasComponent, PositionComponent)
export class CanvasRenderer extends EntityRenderer {
    public render(entity: Entity) {
        const position_component = entity.get_component(PositionComponent);
        const canvas_component = entity.get_component(CanvasComponent);

        const canvas = canvas_component.canvas;

        love.graphics.draw(
            canvas,
            position_component.x,
            position_component.y,
            0,
            canvas_component.x_scale,
            canvas_component.y_scale,
            canvas.getWidth() * 0.5,
            canvas.getHeight() * 0.5,
        );
    }
}