Skip to content

Commit

Permalink
improve example formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Jan 14, 2022
1 parent c68821e commit e49f352
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
42 changes: 20 additions & 22 deletions examples/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,31 @@ const CounterSystem = createSystem({
query: queryComponents({
info: Write(CounterInfo),
}),
})
}).withRunFunction(({actions, query}) => {
/// the logic goes here. Just iterate over the data-set and make your relevant changes for a single step
.withRunFunction(({actions, query}) => {
/// there are two ways to go over the query result:
/// 1. you can use regular loops:
let info;
for ({info} of query.iter()) {
info.count++;
/// there are two ways to go over the query result:
/// 1. you can use regular loops:
let info;
for ({info} of query.iter()) {
info.count++;

// after every ten steps, write out a log message
if (info.count % 10 == 0) {
console.log(`The current count is ${info.count} / ${info.limit}!`);
}
// after every ten steps, write out a log message
if (info.count % 10 == 0) {
console.log(`The current count is ${info.count} / ${info.limit}!`);
}

// if the limit is reached, set the exit field to true
if (info.count == info.limit) {
console.log('Time to exit!');
actions.commands.stopRun();
}
// if the limit is reached, set the exit field to true
if (info.count == info.limit) {
console.log('Time to exit!');
actions.commands.stopRun();
}
}

/// 2. at the cost of iteration speed, you can use a callback function, too:
// query.execute(({info}) => {
// info.count++;
// });
})
.build();
/// 2. at the cost of iteration speed, you can use a callback function, too:
// query.execute(({info}) => {
// info.count++;
// });
}).build();

/// then, we need a world which will hold our systems and entities
const world = buildWorld()
Expand Down
31 changes: 13 additions & 18 deletions examples/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,22 @@ class MyEvent {

const EventTriggerSystem = createSystem({
myEvents: WriteEvents(MyEvent),
lastEvent: Storage({ timestamp: 0 }),
})
/// the logic goes here. Just iterate over the data-set and make your relevant changes for a single step
.withRunFunction(async ({myEvents, lastEvent}) => {
if (Date.now() - lastEvent.timestamp >= 1000) {
await myEvents.publish(new MyEvent('My event just happened!'));
lastEvent.timestamp = Date.now();
}
})
.build();
lastEvent: Storage({timestamp: 0}),
}).withRunFunction(async ({myEvents, lastEvent}) => {
if (Date.now() - lastEvent.timestamp >= 1000) {
await myEvents.publish(new MyEvent('My event just happened!'));
lastEvent.timestamp = Date.now();
}
}).build();

const EventListenerSystem = createSystem({
myEvents: ReadEvents(MyEvent),
})
.withRunFunction(({myEvents}) => {
let myEvent;
for (myEvent of myEvents.iter()) {
console.log(myEvent.message);
}
})
.build();
}).withRunFunction(({myEvents}) => {
let myEvent;
for (myEvent of myEvents.iter()) {
console.log(myEvent.message);
}
}).build();


buildWorld()
Expand Down

0 comments on commit e49f352

Please sign in to comment.