NSSTC / sim-ecs

Batteries included TypeScript ECS
https://nsstc.github.io/sim-ecs/
Mozilla Public License 2.0
81 stars 12 forks source link

System Builder #42

Closed minecrawler closed 2 years ago

minecrawler commented 2 years ago

So this is the PR turning the System Class into build-able functions. Any feedback?

CC @vmwxiong

/// systems process data. We declare what kind of input we need and then define the processing code
const CounterSystem = createSystem(
        /// query for all entities with the correct component
        new Query({
            info: Write(CounterInfo),
        }),
        /// also prepare actions
        Actions,
    )
        /// the logic goes here
        .withRunFunction((query, actions) => {
            /// there are two ways to go over the query result:
            /// 1. you can use regular loops:
            for (const {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}!`);
                }

                // 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:
            // await this.query.execute(({info}) => {
            //     ...
            // });
        })
        .build();