Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dirty entity tracking & updating #221

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/miniplex-bucket/src/Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ export class Bucket<E> extends SimpleBucket<E> {

return bucket
}

/* DIRTY TRACKING */

private dirty = new Set<E>()

mark(entity: E) {
this.dirty.add(entity)
}

flushMarked() {
for (const entity of this.dirty) this.test(entity)
this.dirty.clear()
}

update<D extends E>(entity: D, fun: (entity: D) => void) {
fun(entity)
this.mark(entity)
}
}
24 changes: 24 additions & 0 deletions packages/miniplex-bucket/test/Bucket.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { World } from "../../miniplex/src"
import { Bucket } from "../src"

describe(Bucket, () => {
Expand Down Expand Up @@ -53,5 +54,28 @@ describe(Bucket, () => {
const derived2 = bucket.where(predicate)
expect(derived1).toBe(derived2)
})

it("creates a bucket that is updated automatically for entities that are explicitly marked as dirty", () => {
type Entity = { health: number }

const bucket = new Bucket<Entity>()
const player = bucket.add({ health: 30 })
const lowHealth = bucket.where((p) => p.health < 25)

function applyDamage(entity: Entity, amount = 10) {
entity.health -= amount
bucket.mark(entity)
}

expect(lowHealth.entities).toEqual([])

/* Ouch, something hit the player */
applyDamage(player)

/* This would typically be done once per tick, or similar */
bucket.flushMarked()

expect(lowHealth.entities).toEqual([player])
})
})
})
57 changes: 57 additions & 0 deletions packages/miniplex-core/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,63 @@ profile("simulate (iterator, archetypes)", () => {
}
})

profile("simulate (mutate & mark)", () => {
const world = new World<Entity>()
const withVelocity = world.where(archetype("velocity"))

for (let i = 0; i < entityCount; i++)
world.add({
position: { x: 0, y: i, z: 0 },
velocity: { x: 1, y: 2, z: 3 }
})

return () => {
let i = 0

for (const entity of withVelocity) {
i++

const { position, velocity } = entity
position.x += velocity.x
position.y += velocity.y
position.z += velocity.z
world.mark(entity)
}

world.flushMarked()

return () => i === entityCount
}
})

profile("simulate (update & function)", () => {
const world = new World<Entity>()
const withVelocity = world.where(archetype("velocity"))

for (let i = 0; i < entityCount; i++)
world.add({
position: { x: 0, y: i, z: 0 },
velocity: { x: 1, y: 2, z: 3 }
})

return () => {
let i = 0

for (const entity of withVelocity) {
i++
world.update(entity, ({ position, velocity }) => {
position.x += velocity.x
position.y += velocity.y
position.z += velocity.z
})
}

world.flushMarked()

return () => i === entityCount
}
})

profile("simulate (array)", () => {
const world = new World<Entity>()

Expand Down