Experimental canvas 2D engine for tile-based sidescroller/sandbox games, created strictly for educational purposes.
entity-component-system
game-engine
canvas-2d
1import { Entity, System, type Cosmic } from "@cosmic/core";
2import { Transform, Physics } from "@cosmic/kit/components";
3
4export class PhysicsSystem extends System {
5 public requiredComponents = new Set([Transform.name, Physics.name]);
6 private worldStore: Map<string, any>;
7
8 constructor(engine: Cosmic) {
9 super();
10 this.worldStore = engine.getStore("cosmic.world");
11 }
12
13 update(entities: Entity[], deltaTime: number) {
14 const worldGravityStrength = this.worldStore.get("cosmic.world.gravity.strength") ?? 0;
15
16 entities.forEach((entity) => {
17 const transform = entity.getComponent(Transform)!;
18 const physics = entity.getComponent(Physics)!;
19
20 transform.y += worldGravityStrength * physics.weight * deltaTime;
21 });
22 }
23}