Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Add empty queries (match all entities)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnderShadow8 committed Jun 23, 2021
1 parent 4a2b0f3 commit b29d6c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ class ECS {
}

createQuery(...cmps: string[]): Query {
if(!cmps.length) {
throw new Error("empty query")
}
const or = cmps.filter(c => c.includes(" ")).map(i => i.split(" "))
cmps = cmps.filter(c => !c.includes(" "))
const query = new Query([cmps, ...or].map(i => {
Expand Down
15 changes: 9 additions & 6 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class Query {
mask: QueryMask
archetypes: Archetype[] = []

constructor(q: [number[], number[]][]) {
constructor(q: [number[], number[]][] | undefined) {
const updateMask = (cmps: number[], mask: Uint32Array) => {
cmps.forEach(i => {
mask[Math.floor(i / 32)] |= 1 << i % 32
})
}
this.mask = []
q = q ?? []
q.map((n, i) => {
const crMask = (x: number) => new Uint32Array(Math.ceil((Math.max(0, ...n[x]) + 1) / 32))
this.mask.push([crMask(0), crMask(1)])
Expand All @@ -23,13 +24,15 @@ class Query {
}

static match(target: Uint32Array, query: QueryMask) {
if(!Query.matchAll(target, query[0])) { // AND
return false
}
for(let q of query.slice(1)) { // OR
if(!Query.matchAny(target, q)) {
if(query.length) {
if(!Query.matchAll(target, query[0])) { // AND
return false
}
for(let q of query.slice(1)) { // OR
if(!Query.matchAny(target, q)) {
return false
}
}
}
return true
}
Expand Down
4 changes: 0 additions & 4 deletions test/ecs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ describe("ECS", function() {
expect(not.archetypes).to.eql([ecs._arch.get("0")])
})

it("should throw error on no arguments", function() {
expect(() => ecs.createQuery()).to.throw()
})

it("should throw error on invalid name", function() {
ecs.defineComponent("right")
expect(() => ecs.createQuery("wrong")).to.throw()
Expand Down
8 changes: 8 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Query} from "../build/query"
describe("Query", function() {
describe("constructor", function() {
it("should have appropriate mask", function() {
expect(new Query().mask).to.eql([])
expect(new Query([[[3, 1, 4], [2, 6, 5]]]).mask).to.eql([[new Uint32Array([26]), new Uint32Array([100])]])
expect(new Query([[[1, 2], [4]], [[0, 3, 5], []]]).mask).to.eql([
[new Uint32Array([6]), new Uint32Array([16])],
Expand All @@ -14,6 +15,13 @@ describe("Query", function() {
})

describe("match", function() {
it("should match empty", function() {
const query = new Query().mask
for(let i = 0; i < 20; i++) {
expect(Query.match(new Uint32Array([Math.floor(Math.random() * 1000)]), query)).to.be.true
}
})

it("should match AND", function() {
const query = new Query([[[0, 2, 3], []]]).mask
expect(Query.match(new Uint32Array([13]), query)).to.be.true
Expand Down

0 comments on commit b29d6c2

Please sign in to comment.