NSSTC / sim-ecs

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

Add convencience methods to Query, like `filter`, `sort`,... #54

Closed minecrawler closed 1 year ago

minecrawler commented 2 years ago

These convenience methods should be part of the query interface and run whenever the query result cache is updated.

Some of them are purely for edge-cases where it makes sense to manipulate the results in a usually bad way.

filter

filter((data: PDESC) => boolean): IXXXQuery

Go over each result and decide if it should stay in the cache or not.

push

push(data: PDESC): IXXXQuery

Add a new result to the end of the results cache. This is discouraged (because the result obviously is not part of a real component)

reverse

reverse(): IXXXQuery

Reverse the results' order in cache

shift

shift(data: PDESC): IXXXQuery

Add a new result to the start of the results cache. This is discouraged (because the result obviously is not part of a real component)

sort

sort((data: PDESC) => number): IXXXQuery

Works just like Array.sort(), but on the results.

minecrawler commented 1 year ago

After playing around with this for a while, I didn't find any good way to handle entities during parameterization (since components and data may change) and an implementation for the query lead to a complex solution which just wraps Array.from().

Hence, I decided it's better to offer a way to get the results as array and simply have users consume the standard array API directly. Quiet a few hours went into these two lines of code 😂