hmans / miniplex

A 👩‍💻 developer-friendly entity management system for 🕹 games and similarly demanding applications, based on 🛠 ECS architecture.
MIT License
854 stars 39 forks source link

`world.update` #255

Closed hmans closed 2 years ago

hmans commented 2 years ago

Added an update function to the World class that allows the user to update an entity. This is intended to complement the other two mutating functions (addComponent, removeComponent), simply to allow for the use of syntactic sugar (component constructor functions, entity factories, etc.) in a streamlined fashion:

/* With an object with changes */
world.update(entity, { age: entity.age + 1 })

/* With a function returning an object with changes */
const increaseAge = ({age}) => ({ age: age + 1 }
world.update(entity, increaseAge)

/* With a function that mutates the entity directly: */
const mutatingIncreaseAge = (entity) => entity.age++
world.update(entity, mutatingIncreaseAge)

The main job of the function is to re-index the updated entity against known derived buckets, and since in Miniplex you'll typically mutate entities directly, it can even be called with only an entity. All of the above are essentially equivalent to:

entity.age++
world.update(entity)
codesandbox[bot] commented 2 years ago

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

changeset-bot[bot] commented 2 years ago

🦋 Changeset detected

Latest commit: d775492e5df996b2a33821b916df13a30fd4065e

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR