import { Cosmic, CosmicMode, Entity } from "@cosmic/core"; import { Transform, Collider, Renderable, Physics, PlayerControlled, Movable, Name, } from "@cosmic/kit/components"; import { RenderingSystem, PhysicsSystem, CollisionSystem, PlayerControlSystem, } from "@cosmic/kit/systems"; import { LoggingSystem } from "./LoggingSystem"; //====================================== // Initialize engine and systems //====================================== const engine = Cosmic.getInstance(CosmicMode.DEVELOPMENT); engine.addSystem(new PlayerControlSystem(engine)); engine.addSystem(new PhysicsSystem(engine)); engine.addSystem(new CollisionSystem()); engine.addSystem(new RenderingSystem(engine)); engine.addSystem(new LoggingSystem(engine)); engine.start(); //====================================== // Initialize crucial stores //====================================== const worldStore = engine.getStore("cosmic.world"); worldStore.set("cosmic.world.gravity.strength", 2); worldStore.set("cosmic.world.grid.cellSize", 32); //====================================== // EXPERIMENTATION AREA //====================================== const createTestEntity = (options: { initialX: number; initialY: number; isStatic: boolean; inputs: string[]; }) => { const worldGridCellSize = worldStore.get("cosmic.world.grid.cellSize"); const entity = new Entity(); entity.addComponent( new Transform(options.initialX, options.initialY, worldGridCellSize * 2, worldGridCellSize * 2), ); entity.addComponent(new PlayerControlled(500, ...options.inputs)); entity.addComponent(new Renderable()); entity.addComponent(new Movable()); entity.addComponent(new Physics()); entity.addComponent(new Name("player", "This is a player controlled entity.")); entity.addComponent(new Collider(options.isStatic)); return entity; }; engine.addEntity( createTestEntity({ initialX: 0, initialY: 0, isStatic: false, inputs: ["w", "s", "a", "d"], }), ); engine.addEntity( createTestEntity({ initialX: 30, initialY: 200, isStatic: false, inputs: ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"], }), ); // Create a static object entity, e.g. a world tile. const createObjectEntity = (options: { initialX: number; initialY: number }) => { const worldGridCellSize = worldStore.get("cosmic.world.grid.cellSize"); const entity = new Entity(); entity.addComponent( new Transform(options.initialX, options.initialY, worldGridCellSize, worldGridCellSize), ); entity.addComponent(new Renderable()); entity.addComponent(new Movable()); entity.addComponent(new Physics(0)); entity.addComponent(new Name("object", "This is a static object entity.")); entity.addComponent(new Collider(true)); return entity; }; engine.addEntity( createObjectEntity({ initialX: 0, initialY: 900, }), ); for (let i = 0; i < 70; i++) { engine.addEntity( createObjectEntity({ initialX: i * worldStore.get("cosmic.world.grid.cellSize"), initialY: 900, }), ); }