summaryrefslogtreecommitdiff
path: root/src/engine/systems
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
commita8d07a790395e14fe7aedd3ba638db466f9c0842 (patch)
tree644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/systems
parentaa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff)
downloadthe-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz
the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip
boundingbox + draw player
Diffstat (limited to 'src/engine/systems')
-rw-r--r--src/engine/systems/Render.ts50
-rw-r--r--src/engine/systems/index.ts1
2 files changed, 51 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);
+ });
+ }
+}
diff --git a/src/engine/systems/index.ts b/src/engine/systems/index.ts
index 989dc7f..bb87060 100644
--- a/src/engine/systems/index.ts
+++ b/src/engine/systems/index.ts
@@ -1,2 +1,3 @@
export * from "./SystemNames";
export * from "./System";
+export * from "./Render";