66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
|
import { before, describe, it } from 'node:test'
|
||
|
import { addComponent, addEntity, createWorld, defineComponent, Types } from 'bitecs'
|
||
|
import assert from './assert.js'
|
||
|
import { query } from '../src/query.js'
|
||
|
|
||
|
let engine = {}
|
||
|
describe('query', () => {
|
||
|
before(() => {
|
||
|
const world = { default: createWorld() }
|
||
|
const component = {
|
||
|
Player: defineComponent(null, 10),
|
||
|
NPC: defineComponent(null, 10),
|
||
|
Knows: defineComponent({
|
||
|
from: Types.eid,
|
||
|
to: Types.eid
|
||
|
}, 10)
|
||
|
}
|
||
|
|
||
|
engine = { world, component }
|
||
|
})
|
||
|
|
||
|
it('i just need a test runner', () => {
|
||
|
const w = engine.world.default
|
||
|
const { Player, NPC, Knows } = engine.component
|
||
|
|
||
|
const create = (...components) => {
|
||
|
const eid = addEntity(w)
|
||
|
components.forEach(cmp => addComponent(w, cmp, eid))
|
||
|
return eid
|
||
|
}
|
||
|
|
||
|
const knows = (a, b) => {
|
||
|
const edge = addEntity(w)
|
||
|
addComponent(w, Knows, edge)
|
||
|
Knows.from[edge] = a
|
||
|
Knows.to[edge] = b
|
||
|
return edge
|
||
|
}
|
||
|
|
||
|
const player = create(Player) // 0
|
||
|
const a = create(NPC) // 1
|
||
|
const b = create(NPC) // 2
|
||
|
const c = create(NPC) // 3
|
||
|
//const d = create(NPC)
|
||
|
//const e = create(NPC)
|
||
|
//const f = create(NPC)
|
||
|
//const g = create(NPC)
|
||
|
|
||
|
knows(player, a) // 4
|
||
|
knows(player, c) // 5
|
||
|
|
||
|
knows(a, player) // 6
|
||
|
knows(a, b) // 7
|
||
|
knows(a, c) // 8
|
||
|
|
||
|
knows(b, a) // 9
|
||
|
|
||
|
knows(c, player) // 10
|
||
|
//knows(c, b) // 11
|
||
|
|
||
|
const result = query('MATCH (player:NPC) RETURN player, a, b', engine)
|
||
|
console.log(result.unwrap())
|
||
|
})
|
||
|
})
|
||
|
|