diff --git a/cocoscreator_projects/YouleNexus/assets/framework/core/seat.ts b/cocoscreator_projects/YouleNexus/assets/framework/core/seat.ts new file mode 100644 index 0000000..5540648 --- /dev/null +++ b/cocoscreator_projects/YouleNexus/assets/framework/core/seat.ts @@ -0,0 +1,9 @@ +/** 绝对座位 → 以 mySeat 为视角的视图位(自己=0,其余环形顺延)。 */ +export function toView(mySeat: number, targetSeat: number, seatCount: number): number { + return ((targetSeat - mySeat) % seatCount + seatCount) % seatCount; +} + +/** 视图位 → 绝对座位(toView 的逆运算)。 */ +export function fromView(mySeat: number, viewSeat: number, seatCount: number): number { + return ((viewSeat + mySeat) % seatCount + seatCount) % seatCount; +} diff --git a/cocoscreator_projects/framework-tests/core/seat.test.ts b/cocoscreator_projects/framework-tests/core/seat.test.ts new file mode 100644 index 0000000..87a1951 --- /dev/null +++ b/cocoscreator_projects/framework-tests/core/seat.test.ts @@ -0,0 +1,21 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { toView, fromView } from '../../YouleNexus/assets/framework/core/seat.ts'; + +test('toView:自己永远是视图 0 号位', () => { + assert.equal(toView(2, 2, 4), 0); // mySeat=2, target=2 → 0 +}); + +test('toView:他人按环形相对自己排布', () => { + // 4 人桌,我在 2 号位:座位 3→视图1,座位 0→视图2,座位 1→视图3 + assert.equal(toView(2, 3, 4), 1); + assert.equal(toView(2, 0, 4), 2); + assert.equal(toView(2, 1, 4), 3); +}); + +test('fromView 是 toView 的逆运算', () => { + const seatCount = 4, mySeat = 2; + for (let s = 0; s < seatCount; s++) { + assert.equal(fromView(mySeat, toView(mySeat, s, seatCount), seatCount), s); + } +});