diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
commit | a8d07a790395e14fe7aedd3ba638db466f9c0842 (patch) | |
tree | 644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/systems/Render.ts | |
parent | aa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff) | |
download | the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip |
boundingbox + draw player
Diffstat (limited to 'src/engine/systems/Render.ts')
-rw-r--r-- | src/engine/systems/Render.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/engine/systems/Render.ts b/src/engine/systems/Render.ts new file mode 100644 index 0000000..6f539c0 --- /dev/null +++ b/src/engine/systems/Render.ts @@ -0,0 +1,50 @@ +import { System, SystemNames } from "."; +import { BoundingBox, ComponentNames, Sprite } from "../components"; +import { Game } from ".."; +import { clamp } from "../utils"; + +export class Render extends System { + private ctx: CanvasRenderingContext2D; + + constructor(ctx: CanvasRenderingContext2D) { + super(SystemNames.Render); + this.ctx = ctx; + } + + public update(dt: number, game: Game) { + this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); + + game.forEachEntityWithComponent(ComponentNames.Sprite, (entity) => { + const sprite = entity.getComponent<Sprite>(ComponentNames.Sprite); + sprite.update(dt); + + const boundingBox = entity.getComponent<BoundingBox>( + ComponentNames.BoundingBox, + ); + + // don't render if we're outside the screen + if ( + clamp( + boundingBox.center.y, + -boundingBox.dimension.height / 2, + this.ctx.canvas.height + boundingBox.dimension.height / 2, + ) != boundingBox.center.y || + clamp( + boundingBox.center.x, + -boundingBox.dimension.width / 2, + this.ctx.canvas.width + boundingBox.dimension.width / 2, + ) != boundingBox.center.x + ) { + return; + } + + const drawArgs = { + center: boundingBox.center, + dimension: boundingBox.dimension, + rotation: boundingBox.rotation, + }; + + sprite.draw(this.ctx, drawArgs); + }); + } +} |