{
const world = bitECS.createWorld();
world.name = "TempestWorld";
addToConsole(world);
const entity001 = bitECS.addEntity(world)
addToConsole(entity001);
const Vector3 = {
x: bitECS.Types.f32,
y: bitECS.Types.f32,
z: bitECS.Types.f32
}
addToConsole(Vector3)
const Position = bitECS.defineComponent(Vector3)
const Velocity = bitECS.defineComponent(Vector3)
addToConsole(Position);
addToConsole(Velocity);
const List = bitECS.defineComponent({
values: [bitECS.Types.f32, 3]
})
// a generic container
const Tag = bitECS.defineComponent()
addToConsole(Tag)
// add these components to our entity - entity001
bitECS.addComponent(world, Position, entity001)
bitECS.addComponent(world, Velocity, entity001)
bitECS.addComponent(world, List, entity001)
bitECS.addComponent(world, Tag, entity001)
// set the velocity of our entity via the components. Each value in a component is an array of the entities that posess it.
Velocity.x[entity001] = 1;
Velocity.y[entity001] = 1;
// note entity001 has not changed. It's just that the value in the Velicity.x array with that entity ID now has those values.
addToConsole(Velocity.x) //look at first item in array - it has a value of 1 now.
// next let's create a query into the world. Return to us all the entities that have a Position and a Velocity
const movementQuery = bitECS.defineQuery([Position, Velocity])
// it has no meaning until associated with a world
const ents = movementQuery(world)
addToConsole(ents); // returns the ID of the only entity in the world
// add concept of time. Just a plain attribute of the world.
world.time = { delta: 0, elapsed: 0, then: performance.now() }
addToConsole(world)
// add a system - a way to update some component values
const movementSystem = (world) => {
const ents = movementQuery(world)
for (let i = 0; i < ents.length; i++) {
const eid = ents[i]
Position.x[eid] += Velocity.x[eid]
Position.y[eid] += Velocity.y[eid]
Position.z[eid] += Velocity.z[eid]
}
return world
}
// add a "time" system to keep track of time
const timeSystem = world => {
const { time } = world
const now = performance.now()
const delta = now - time.then
time.delta = delta
time.elapsed += delta
time.then = now
return world
}
// a pipeline to run the systems
const pipeline = bitECS.pipe(movementSystem, timeSystem)
//now run the pipleline ever 1000 msec
setInterval(() => {
pipeline(world);
console.log("Position", Position.x[entity001])
scanner.querySelector("#xpos").innerHTML = Position.x[entity001]
console.log("tobj", JSON.stringify(world.time))
timeview.querySelector("#tobj").innerHTML = JSON.stringify(world.time);
}, 1000)
}