Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created December 4, 2025 19:44
Show Gist options
  • Select an option

  • Save Lightnet/bcf10a60a438dccc99ce55215f83b7a6 to your computer and use it in GitHub Desktop.

Select an option

Save Lightnet/bcf10a60a438dccc99ce55215f83b7a6 to your computer and use it in GitHub Desktop.
becsy Singleton System test.
import {System, Type, World} from 'https://esm.run/@lastolivegames/becsy';
// import * as THREE from 'https://esm.run/three';
import van from "vanjs-core";
class ConfigComponent {
static schema = {
gameSpeed: {type:Type.float32, default: 60},
isPaused: {type:Type.boolean, default: false},
};
}
class SingletonSystem extends System {
// Declare the required singleton component
static singletons = [ConfigComponent];
constructor() {
super();
console.log(this);
this.singleton.write(ConfigComponent);
// this.config = this.singleton.write(ConfigComponent);
}
execute() {
const config = this.singleton.write(ConfigComponent);
// config.gameSpeed += 1;
// console.log(config);
// if (!config.isPaused) {
// console.log(`Game running at speed: ${config.gameSpeed}`);
// // ... game logic ...
// }
}
}
class DummySystem extends System {
static singletons = [ConfigComponent];
constructor() {
super();
console.log(this);
// this.singleton.write(ConfigComponent);
this.singleton.read(ConfigComponent);// this to be read else error.
// this.config = this.singleton.write(ConfigComponent);
}
execute() {
const config = this.singleton.read(ConfigComponent);
console.log(config.gameSpeed)
// console.log('dummy system');
}
}
class RenderSystem extends System {
execute() {
console.log('render system');
}
}
console.log(World);
const dummyGroup = System.group(DummySystem);
const renderGroup = System.group(RenderSystem);
const instanceGroup = System.group(SingletonSystem);
const world = await World.create({
// defs: [Acceleration, Position, PositionLogSystem, MovableSystem, DummySystem]
defs: [
ConfigComponent,
RenderSystem,
DummySystem,
SingletonSystem,
dummyGroup,
renderGroup,
instanceGroup
]
});
console.log(world);
console.log(renderGroup);
const frame = world.createCustomExecutor( dummyGroup, renderGroup, instanceGroup);
// van.add(document.body, renderer.domElement);
async function run() {
// await world.execute();
await frame.begin();
await frame.execute(dummyGroup);
// await frame.execute(renderGroup);
await frame.execute(instanceGroup);
await frame.end();
requestAnimationFrame(run);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment