diff options
Diffstat (limited to 'static/src/engine/render.ts')
| -rw-r--r-- | static/src/engine/render.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/static/src/engine/render.ts b/static/src/engine/render.ts new file mode 100644 index 0000000..8f0343a --- /dev/null +++ b/static/src/engine/render.ts @@ -0,0 +1,46 @@ +import { + ComponentType, + PositionComponent, + TrailingPositionComponent, +} from "./component"; +import { Game } from "./game"; +import { System, SystemType } from "./system"; +import { drawLaserPen } from "laser-pen"; + +export class RenderSystem extends System { + constructor(private readonly canvas: HTMLCanvasElement) { + super(SystemType.RENDER); + } + + public set_world_dimensions(width: number, height: number) { + this.canvas.width = width; + this.canvas.height = height; + } + + public update(_dt: number, game: Game) { + const ctx = this.canvas.getContext("2d"); + if (ctx === null) return; + ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); + + game.for_each_entity_with_component(ComponentType.RENDERABLE, (entity) => { + if (ComponentType.TRAILING_POSITION in entity.components) { + const trailing_position = entity.components[ + ComponentType.TRAILING_POSITION + ] as TrailingPositionComponent; + if (trailing_position.trails.length < 3) return; + drawLaserPen(ctx, trailing_position.trails); + return; + } + + if (ComponentType.POSITION in entity.components) { + const position = entity.components[ + ComponentType.POSITION + ] as PositionComponent; + ctx.beginPath(); + ctx.arc(position.x, position.y, 50, 0, 2 * Math.PI); + ctx.stroke(); + return; + } + }); + } +} |
