Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
12 kB · 579 lines
TypeScript
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580import { describe, expect, it } from "vitest";
import { ECS, Entity } from "../ecs";import { evaluateAction, evaluateEntityQuery, evaluateTriggerCondition, selectValueQuery,} from "./evaluateEntityQuery";
const server: any = {};
describe("entity query", () => { it("should find an entity by identity", () => { const ecs = new ECS(server);
const entity = new Entity(); entity.addComponent("identity", { name: "Test", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity2);
expect( evaluateEntityQuery(ecs, [ { component: "identity", property: "name", comparison: "=", value: "Test", }, ]), ).toEqual([entity]); expect( evaluateEntityQuery(ecs, [ { component: "identity", property: "name", comparison: "!=", value: "Test", }, ]), ).toEqual([entity2]); }); it("should transform an entity to pull out a single value", () => { const ecs = new ECS(server); const entity = new Entity(); entity.addComponent("position", { x: 0, y: 0, }); entity.addComponent("identity", { name: "Test", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("position", { x: 0, y: 1, }); entity2.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity2);
expect( selectValueQuery(ecs, { query: [ { component: "identity", property: "name", comparison: "!=", value: "Blah", }, ], select: { component: "position", property: "y", matchType: "all" }, }), ).toEqual([0, 1]); expect( selectValueQuery(ecs, { query: [ { component: "identity", property: "name", comparison: "!=", value: "Blah", }, ], select: { component: "position", property: "y", matchType: "first" }, }), ).toEqual([0]);
expect( selectValueQuery(ecs, { query: [ { component: "identity", property: "name", comparison: "!=", value: "Blah", }, ], select: { component: "identity", property: "name", matchType: "all" }, }), ).toEqual(["Test", "Test2"]); }); it("should compare one entity's value to another's", () => { const ecs = new ECS(server); const entity = new Entity(); entity.addComponent("position", { x: 1, y: 0, }); entity.addComponent("identity", { name: "Test", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("position", { x: 0, y: 1, }); entity2.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity2);
expect( evaluateEntityQuery(ecs, [ { component: "position", property: "y", comparison: "=", value: { query: [ { component: "identity", property: "name", comparison: "=", value: "Test", }, ], select: { component: "position", property: "x", matchType: "first", }, }, }, ]), ).toEqual([entity2]); }); it("should match systems from a ship entity", () => { const ecs = new ECS(server);
const ship = new Entity(); ship.addComponent("isShip"); ship.addComponent("isPlayerShip");
ecs.addEntity(ship);
const fakeSystem = new Entity(); fakeSystem.addComponent("isShipSystem", { type: "warpEngines", shipId: ship.id, }); fakeSystem.addComponent("isWarpEngines"); ecs.addEntity(fakeSystem);
const system = new Entity(); system.addComponent("isShipSystem", { type: "impulseEngines", shipId: ship.id, }); system.addComponent("isImpulseEngines"); ecs.addEntity(system);
const ship2 = new Entity(); ship2.addComponent("isShip"); ecs.addEntity(ship2);
const system2 = new Entity(); system2.addComponent("isShipSystem", { type: "impulseEngines", shipId: ship2.id, }); system2.addComponent("isImpulseEngines"); ecs.addEntity(system2);
expect( evaluateEntityQuery(ecs, [ { component: "isShipSystem", property: "shipId", comparison: "=", value: { query: [ // @ts-expect-error { component: "isPlayerShip", property: "isPresent", }, ],
// @ts-expect-error select: { component: "id", matchType: "first" }, }, }, // @ts-expect-error { component: "isImpulseEngines", property: "isPresent", }, ]), ).toEqual([system]); });});
describe("evaluate trigger condition", () => { it("should evaluate a match condition", () => { const ecs = new ECS(server);
const entity = new Entity(); entity.addComponent("identity", { name: "Testing", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity2);
const condition = [ { type: "entityMatch" as const, matchCount: "any" as const, query: [ { component: "identity", property: "name", comparison: "=", value: "Test", }, ], }, ]; expect(evaluateTriggerCondition(ecs, condition)).toEqual(false);
entity.updateComponent("identity", { name: "Test" });
expect(evaluateTriggerCondition(ecs, condition)).toBeTruthy(); }); it("should evaluate multiple match conditions", () => { const ecs = new ECS(server);
const entity = new Entity(); entity.addComponent("identity", { name: "Test", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("identity", { name: "Test", }); ecs.addEntity(entity2);
const condition = [ { type: "entityMatch" as const, matchCount: "any", query: [ { component: "identity", property: "name", comparison: "=", value: "Test", }, ], }, { type: "entityMatch" as const, matchCount: "any", query: [ { component: "isPlayerShip", property: "isPresent", }, ], }, ]; expect(evaluateTriggerCondition(ecs, condition as any)).toEqual(false);
entity.addComponent("isPlayerShip");
expect(evaluateTriggerCondition(ecs, condition as any)).toBeTruthy(); }); it("should evaluate distance conditions", () => { const ecs = new ECS(server);
const ship = new Entity();
const entity = new Entity(); entity.addComponent("position", { x: 0, y: 0, type: "ship", parentId: ship.id, }); entity.addComponent("identity", { name: "Test", }); ecs.addEntity(entity);
const entity2 = new Entity(); entity2.addComponent("position", { x: 0, y: 100, type: "ship", parentId: ship.id, }); entity2.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity2); const entity3 = new Entity(); entity3.addComponent("position", { x: 0, y: 50, type: "ship", parentId: ship.id, }); entity3.addComponent("identity", { name: "Test2", }); ecs.addEntity(entity3);
const condition = [ { type: "distance" as const, entityA: entity2.id, entityB: entity3.id, distance: 200, condition: "less than", }, ]; expect(evaluateTriggerCondition(ecs, condition as any)).toBeTruthy(); condition[0].distance = 75; expect(evaluateTriggerCondition(ecs, condition as any)).toBeTruthy(); condition[0].distance = 25; expect(evaluateTriggerCondition(ecs, condition as any)).toEqual(false); condition[0].condition = "more than"; expect(evaluateTriggerCondition(ecs, condition as any)).toBeTruthy(); condition[0].distance = 75; expect(evaluateTriggerCondition(ecs, condition as any)).toEqual(false); condition[0].distance = 150; expect(evaluateTriggerCondition(ecs, condition as any)).toEqual(false); }); it("should evaluate event listener conditions", () => { const ecs = new ECS(server);
expect( evaluateTriggerCondition(ecs, [ { type: "eventListener", event: "test", }, ]), ).toBe(false); expect( evaluateTriggerCondition( ecs, [ { type: "eventListener", event: "test", }, ], { event: "blah", values: {}, }, ), ).toBe(false); expect( evaluateTriggerCondition( ecs, [ { type: "eventListener", event: "test", }, ], { event: "test", values: {}, }, ), ).toBeTruthy(); expect( evaluateTriggerCondition( ecs, [ { type: "eventListener", event: "test", values: { test: "test", }, }, ], { event: "test", values: { test: "Whatever", other: true, }, }, ), ).toEqual(false); expect( evaluateTriggerCondition( ecs, [ { type: "eventListener", event: "test", values: { test: "test", }, }, ], { event: "test", values: { test: "test", other: true, }, }, ), ).toEqual({ test: "test", other: true }); });});describe("evaluate action", () => { it("should evaluate an action with literal values", () => { const ecs = new ECS(server);
expect( evaluateAction(ecs, { action: "test", values: { value1: "A", value2: "2" }, name: "test", id: "test", }), ).toEqual([{ value1: "A", value2: "2" }]); }); it("should evaluate an action with a single value query", () => { const ecs = new ECS(server);
const entity = new Entity(); entity.addComponent("identity", { name: "Identity", }); entity.addComponent("tags", { tags: ["test"], }); ecs.addEntity(entity);
expect( evaluateAction(ecs, { action: "test", values: { value1: { query: [ { component: "tags", property: "tags", comparison: "contains", value: "test", }, ], select: { component: "identity", property: "name", matchType: "first", }, }, value2: "2", }, name: "test", id: "test", }), ).toEqual([{ value1: "Identity", value2: "2" }]); }); it("should evaluate an action with a multiple value query", () => { const ecs = new ECS(server);
const entity = new Entity(); entity.addComponent("identity", { name: "Identity 1", }); entity.addComponent("tags", { tags: ["test"], }); ecs.addEntity(entity); const entity2 = new Entity(); entity2.addComponent("identity", { name: "Identity 2", }); entity2.addComponent("tags", { tags: ["test"], }); ecs.addEntity(entity2);
expect( evaluateAction(ecs, { action: "test", values: { value1: { query: [ { component: "tags", property: "tags", comparison: "contains", value: "test", }, ], select: { component: "identity", property: "name", matchType: "first", }, }, value2: "2", }, name: "test", id: "test", }), ).toEqual([{ value1: "Identity 1", value2: "2" }]);
expect( evaluateAction(ecs, { action: "test", values: { value1: { query: [ { component: "tags", property: "tags", comparison: "contains", value: "test", }, ], select: { component: "identity", property: "name", matchType: "all", }, }, value2: "2", }, name: "test", id: "test", }), ).toEqual([ { value1: "Identity 1", value2: "2" }, { value1: "Identity 2", value2: "2" }, ]); });});