Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
5.9 kB · 268 lines
TypeScript
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269import { thoriumContext } from "@thorium/utils/.server/context";import { createMockDataContext } from "@thorium/utils/.server/createMockDataContext";import { testDataStoreProps } from "@thorium/utils/.server/db-fs/testDataStoreProps";import { calculateShipMapPath, createShipMapGraph,} from "@thorium/utils/.server/ship/shipMapPathfinder";import { ECS, Entity } from "@thorium/utils/ecs";import { aroundEach, beforeEach, describe, expect, it } from "vitest";
import { PassengerMovementSystem } from "../PassengerMovementSystem";
aroundEach(async (runTest) => { await thoriumContext.run(testDataStoreProps, async () => { await runTest(); });});
describe("PassengerMovementSystem", () => { let ecs: ECS; let ship: Entity; beforeEach(() => { const mockDataContext = createMockDataContext();
ecs = new ECS(mockDataContext.server); ecs.addSystem(new PassengerMovementSystem()); ship = new Entity(); ship.addComponent("shipMap", { decks: [ { name: "Deck 1", backgroundUrl: "", }, { name: "Deck 2", backgroundUrl: "", }, ], deckNodes: [ { id: 1, deckIndex: 0, x: 0, y: 0, contents: {}, volume: 12000, flags: [], systems: [], }, { id: 2, deckIndex: 0, x: 1, y: 1, contents: {}, volume: 12000, flags: [], systems: [], }, { id: 3, deckIndex: 0, x: 2, y: 1, contents: {}, volume: 12000, flags: [], systems: [], }, { id: 4, deckIndex: 1, x: 2, y: 1, contents: {}, volume: 12000, flags: [], systems: [], }, { id: 5, deckIndex: 1, x: 1, y: 0, contents: {}, volume: 12000, flags: [], systems: [], }, ], deckEdges: [ { id: 1, from: 1, to: 2, }, { id: 2, from: 2, to: 3, }, { id: 2, from: 3, to: 4, }, { id: 2, from: 4, to: 5, }, ], }); ecs.addEntity(ship); }); it("should move a passenger within the same deck", () => { let updateTime = 0; function update(updateCount = 1) { for (let i = 20 * updateCount; i > 0; i--) { ecs.update(16); updateTime += 16; } } const passenger = new Entity(); passenger.addComponent("position", { x: 0, y: 0, z: 0, parentId: ship.id, type: "ship", }); passenger.addComponent("passengerMovement"); ecs.addEntity(passenger); ecs.update(16); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 1, "type": "ship", "x": 0, "y": 0, "z": 0, } `); const graph = createShipMapGraph(ship.components.shipMap?.deckEdges || []); const nodePath = calculateShipMapPath(graph, 1, 3); if (!nodePath) throw new Error("unable to calculate node path"); passenger.updateComponent("passengerMovement", { nodePath, nextNodeIndex: 0, }); update(2); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 1, "type": "ship", "x": 0.44215339876352006, "y": 0.44215339876352006, "z": 0, } `); expect(passenger.components.passengerMovement?.nextNodeIndex).toBe(1); update(7); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 1, "type": "ship", "x": 1.0995775896953266, "y": 0.9459775896953266, "z": 0, } `); expect(passenger.components.passengerMovement?.nextNodeIndex).toBe(2); update(8); const oldX = passenger.components.position?.x; expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 1, "type": "ship", "x": 2, "y": 1, "z": 0, } `); expect(passenger.components.passengerMovement?.nextNodeIndex).toBe(0); expect(updateTime).toMatchInlineSnapshot(`5440`); update(); expect(passenger.components.position?.x).toBe(oldX); }); it("should move a passenger between decks", () => { let updateTime = 0; function update(updateCount = 1) { for (let i = 20 * updateCount; i > 0; i--) { ecs.update(16); updateTime += 16; } }
const passenger = new Entity(); passenger.addComponent("position", { x: 2, y: 1, z: 0, parentId: ship.id, type: "ship", }); passenger.addComponent("passengerMovement"); ecs.addEntity(passenger); ecs.update(16); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 3, "type": "ship", "x": 2, "y": 1, "z": 0, } `); const graph = createShipMapGraph(ship.components.shipMap?.deckEdges || []); const nodePath = calculateShipMapPath(graph, 3, 5); if (!nodePath) throw new Error("unable to calculate node path"); passenger.updateComponent("passengerMovement", { nodePath, nextNodeIndex: 0, }); update(2); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 3, "type": "ship", "x": 2, "y": 1, "z": 0.16799999999999998, } `); expect(passenger.components.passengerMovement?.nextNodeIndex).toBe(1); update(22); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 3, "type": "ship", "x": 1, "y": 0, "z": 1, } `); update(4); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 3, "type": "ship", "x": 1, "y": 0, "z": 1, } `); update(4); expect(passenger.components.position).toMatchInlineSnapshot(` { "parentId": 3, "type": "ship", "x": 1, "y": 0, "z": 1, } `); expect(updateTime).toMatchInlineSnapshot(`10240`); });});