| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { dirname, join } from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js"; |
| 6 | import { mobIsAggressive } from "../../shared/definitions/mobCombat.js"; |
| 7 | import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; |
| 8 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 9 | import { shadeSummonComponents, stepSummons } from "../src/spells/summons.js"; |
| 10 | import { stepAggression } from "../src/combat/service.js"; |
| 11 | import { Component } from "../src/ecs/components.js"; |
| 12 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 13 | |
| 14 | const ASSET_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../../client/assets"); |
| 15 | |
| 16 | function workerOf (simulation, entityId) |
| 17 | { |
| 18 | const record = simulation.entityRecord(entityId); |
| 19 | assert.ok(record, "entity must be remembered"); |
| 20 | return simulation.workers[record.workerId]; |
| 21 | } |
| 22 | |
| 23 | test("shade uses own static body and animated bloom sprites", () => |
| 24 | { |
| 25 | const shade = mobDefinition(MobId.shade); |
| 26 | assert.equal(shade.spriteKey, undefined); |
| 27 | assert.equal(mobSpriteName(MobId.shade), "mob:shade:shade"); |
| 28 | assert.equal(shade.bloomBody, true); |
| 29 | assert.equal(shade.frames, 1); |
| 30 | assert.equal(shade.bloomFrames, 2); |
| 31 | assert.equal(shade.bloomAnimationSpeed, 0.5); |
| 32 | assert.equal(shade.aggressive, true); |
| 33 | assert.equal(mobIsAggressive(shade), true); |
| 34 | |
| 35 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 36 | const body = catalog.images.find((entry) => entry.name === "mob:shade:shade"); |
| 37 | const bloom = catalog.images.find((entry) => entry.name === "mob:shade:shade:bloom"); |
| 38 | assert.ok(body, "shade body catalog entry"); |
| 39 | assert.ok(bloom, "shade bloom catalog entry"); |
| 40 | assert.equal(body.subImgTotal, 1); |
| 41 | assert.equal(body.animationSpeed, 0); |
| 42 | assert.equal(bloom.subImgTotal, 2); |
| 43 | assert.equal(bloom.perRow, 2); |
| 44 | assert.equal(bloom.animationSpeed, 0.5); |
| 45 | |
| 46 | const components = mobComponents({ |
| 47 | tileX: 8, |
| 48 | tileY: 8, |
| 49 | mobTypeId: MobId.shade, |
| 50 | spawnerId: "0:0:1", |
| 51 | originTileX: 8, |
| 52 | originTileY: 8 |
| 53 | }); |
| 54 | assert.equal(components.appearance.bloomBody, true); |
| 55 | assert.equal(components.appearance.tint, undefined); |
| 56 | assert.equal(components.appearance.dualTint, undefined); |
| 57 | assert.equal(components.spawnOrigin.aggressive, true); |
| 58 | assert.equal(components.summon, undefined); |
| 59 | }); |
| 60 | |
| 61 | test("summoned shade uses companion targeting, not player aggro", () => |
| 62 | { |
| 63 | const harness = makeSimulation({ workerCount: 1 }); |
| 64 | try |
| 65 | { |
| 66 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 67 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 68 | const components = shadeSummonComponents({ |
| 69 | tileX: 10, |
| 70 | tileY: 8, |
| 71 | level: 10, |
| 72 | ownerEntityId: playerId, |
| 73 | expiresAt: Date.now() + 60_000 |
| 74 | }); |
| 75 | assert.ok(components.summon); |
| 76 | assert.equal(components.spawnOrigin, undefined); |
| 77 | assert.equal(components.appearance.bloomBody, true); |
| 78 | |
| 79 | const shadeId = harness.simulation.spawnEntity(components); |
| 80 | const worker = workerOf(harness.simulation, shadeId); |
| 81 | const world = worker.world; |
| 82 | const shadeHandle = world.ecs.handleOf(shadeId); |
| 83 | |
| 84 | stepAggression(world, Date.now()); |
| 85 | assert.equal( |
| 86 | world.ecs.get(shadeHandle, Component.combat)?.targetEntityId ?? null, |
| 87 | null, |
| 88 | "summoned shade must not auto-aggro the owner/player" |
| 89 | ); |
| 90 | |
| 91 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 92 | tileX: 12, |
| 93 | tileY: 8, |
| 94 | mobTypeId: MobId.rat, |
| 95 | spawnerId: "0:0:2", |
| 96 | originTileX: 12, |
| 97 | originTileY: 8, |
| 98 | aggressive: true |
| 99 | })); |
| 100 | stepSummons(world, Date.now()); |
| 101 | assert.equal( |
| 102 | world.ecs.get(shadeHandle, Component.combat)?.targetEntityId, |
| 103 | ratId, |
| 104 | "summoned shade should pick nearby aggressive hostiles" |
| 105 | ); |
| 106 | } |
| 107 | finally |
| 108 | { harness.close(); } |
| 109 | }); |