fix(spatial): 修复 GridAOI 可见性更新问题

- 修复 addObserver 时现有观察者无法检测到新实体的问题
- 修复实体远距离移动时观察者可见性未正确更新的问题
- 重构 demos 抽取公共测试工具
This commit is contained in:
yhh
2025-12-26 22:23:03 +08:00
parent 881ffad3bc
commit d66c18041e
9 changed files with 103 additions and 118 deletions

View File

@@ -3,22 +3,12 @@
*/
import { createStateMachine } from '@esengine/fsm';
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
function section(name: string): void {
console.log(`\n▶ ${name}`);
}
import { assert, section, demoHeader, demoFooter } from './utils.js';
type PlayerState = 'idle' | 'walk' | 'run' | 'jump';
export async function runFSMDemo(): Promise<void> {
console.log('═══════════════════════════════════════');
console.log(' FSM Module Demo');
console.log('═══════════════════════════════════════');
demoHeader('FSM Module Demo');
// 1. Basic Creation
section('1. createStateMachine()');
@@ -167,9 +157,7 @@ export async function runFSMDemo(): Promise<void> {
const history = fsmHist.getHistory();
assert(history.length >= 2, 'History recorded');
console.log('\n═══════════════════════════════════════');
console.log(' FSM Demo: ALL TESTS PASSED ✓');
console.log('═══════════════════════════════════════\n');
demoFooter('FSM Demo');
}
runFSMDemo().catch(console.error);

View File

@@ -10,20 +10,10 @@ import {
manhattanDistance,
octileDistance
} from '@esengine/pathfinding';
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
function section(name: string): void {
console.log(`\n▶ ${name}`);
}
import { assert, section, demoHeader, demoFooter } from './utils.js';
export async function runPathfindingDemo(): Promise<void> {
console.log('═══════════════════════════════════════');
console.log(' Pathfinding Module Demo');
console.log('═══════════════════════════════════════');
demoHeader('Pathfinding Module Demo');
// 1. Create Grid Map
section('1. createGridMap()');
@@ -156,9 +146,7 @@ export async function runPathfindingDemo(): Promise<void> {
grid.reset();
assert(grid.isWalkable(5, 5), 'Grid reset - all walkable');
console.log('\n═══════════════════════════════════════');
console.log(' Pathfinding Demo: ALL TESTS PASSED ✓');
console.log('═══════════════════════════════════════\n');
demoFooter('Pathfinding Demo');
}
runPathfindingDemo().catch(console.error);

View File

@@ -17,20 +17,10 @@ import {
weightedPick,
weightedPickFromMap
} from '@esengine/procgen';
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
function section(name: string): void {
console.log(`\n▶ ${name}`);
}
import { assert, section, demoHeader, demoFooter } from './utils.js';
export async function runProcgenDemo(): Promise<void> {
console.log('═══════════════════════════════════════');
console.log(' Procgen Module Demo');
console.log('═══════════════════════════════════════');
demoHeader('Procgen Module Demo');
// 1. Perlin Noise
section('1. createPerlinNoise()');
@@ -200,9 +190,7 @@ export async function runProcgenDemo(): Promise<void> {
const afterReset = rngReset.next();
assert(first === afterReset, 'Reset restores initial state');
console.log('\n═══════════════════════════════════════');
console.log(' Procgen Demo: ALL TESTS PASSED ✓');
console.log('═══════════════════════════════════════\n');
demoFooter('Procgen Demo');
}
runProcgenDemo().catch(console.error);

View File

@@ -13,15 +13,7 @@ import {
distance,
distanceSquared
} from '@esengine/spatial';
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
function section(name: string): void {
console.log(`\n▶ ${name}`);
}
import { assert, section, demoHeader, demoFooter } from './utils.js';
interface Entity {
id: number;
@@ -29,9 +21,7 @@ interface Entity {
}
export async function runSpatialDemo(): Promise<void> {
console.log('═══════════════════════════════════════');
console.log(' Spatial Module Demo');
console.log('═══════════════════════════════════════');
demoHeader('Spatial Module Demo');
// 1. Create Spatial Index
section('1. createGridSpatialIndex()');
@@ -150,10 +140,6 @@ export async function runSpatialDemo(): Promise<void> {
aoi.addObserver(p1, { x: 100, y: 100 }, { viewRange: 200 });
aoi.addObserver(p2, { x: 150, y: 150 }, { viewRange: 200 });
// Note: After adding p2, p2 can see p1, but p1's visibility isn't auto-updated
// We need to trigger an update for p1 to detect p2
aoi.updatePosition(p1, { x: 100, y: 100 });
// 15. getEntitiesInView
section('15. getEntitiesInView()');
const visible = aoi.getEntitiesInView(p1);
@@ -166,14 +152,11 @@ export async function runSpatialDemo(): Promise<void> {
// 17. updatePosition
section('17. updatePosition()');
aoi.updatePosition(p2, { x: 1000, y: 1000 });
// Refresh p1's visibility (implementation requires explicit update for distant moves)
aoi.updatePosition(p1, { x: 100, y: 100 });
assert(!aoi.canSee(p1, p2), 'p1 cannot see p2 after move');
// 18. getObserversOf
section('18. getObserversOf()');
aoi.updatePosition(p2, { x: 120, y: 120 });
aoi.updatePosition(p1, { x: 100, y: 100 }); // Refresh p1's visibility
const observers = aoi.getObserversOf(p2);
assert(observers.includes(p1), 'p1 observes p2');
@@ -184,16 +167,13 @@ export async function runSpatialDemo(): Promise<void> {
eventCount++;
});
aoi.updatePosition(p2, { x: 2000, y: 2000 }); // Should trigger exit
aoi.updatePosition(p1, { x: 100, y: 100 }); // Refresh p1
aoi.updatePosition(p2, { x: 130, y: 130 }); // Should trigger enter
aoi.updatePosition(p1, { x: 100, y: 100 }); // Refresh p1
assert(eventCount >= 1, `Events triggered: ${eventCount}`);
// 20. updateViewRange
section('20. updateViewRange()');
aoi.updateViewRange(p1, 50);
aoi.updatePosition(p2, { x: 200, y: 200 });
aoi.updatePosition(p1, { x: 100, y: 100 }); // Refresh p1's visibility with new range
assert(!aoi.canSee(p1, p2), 'Cannot see after view range reduced');
// 21. removeObserver
@@ -238,9 +218,7 @@ export async function runSpatialDemo(): Promise<void> {
const dsq = distanceSquared({ x: 0, y: 0 }, { x: 3, y: 4 });
assert(dsq === 25, `Distance squared: ${dsq}`);
console.log('\n═══════════════════════════════════════');
console.log(' Spatial Demo: ALL TESTS PASSED ✓');
console.log('═══════════════════════════════════════\n');
demoFooter('Spatial Demo');
}
runSpatialDemo().catch(console.error);

View File

@@ -3,20 +3,10 @@
*/
import { createTimerService } from '@esengine/timer';
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
function section(name: string): void {
console.log(`\n▶ ${name}`);
}
import { assert, section, demoHeader, demoFooter } from './utils.js';
export async function runTimerDemo(): Promise<void> {
console.log('═══════════════════════════════════════');
console.log(' Timer Module Demo');
console.log('═══════════════════════════════════════');
demoHeader('Timer Module Demo');
// 1. Basic Creation
section('1. createTimerService()');
@@ -111,9 +101,7 @@ export async function runTimerDemo(): Promise<void> {
const limited = createTimerService({ maxTimers: 2, maxCooldowns: 1 });
assert(limited !== null, 'Created with config');
console.log('\n═══════════════════════════════════════');
console.log(' Timer Demo: ALL TESTS PASSED ✓');
console.log('═══════════════════════════════════════\n');
demoFooter('Timer Demo');
}
runTimerDemo().catch(console.error);

View File

@@ -0,0 +1,41 @@
/**
* @zh Demo 测试工具函数
* @en Demo test utility functions
*/
/**
* @zh 断言条件为真,否则抛出错误
* @en Assert condition is true, otherwise throw error
*/
export function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(`FAILED: ${message}`);
console.log(`${message}`);
}
/**
* @zh 打印测试章节标题
* @en Print test section header
*/
export function section(name: string): void {
console.log(`\n▶ ${name}`);
}
/**
* @zh 打印 Demo 开始标题
* @en Print demo start header
*/
export function demoHeader(name: string): void {
console.log('═══════════════════════════════════════');
console.log(` ${name}`);
console.log('═══════════════════════════════════════');
}
/**
* @zh 打印 Demo 结束标题
* @en Print demo end header
*/
export function demoFooter(name: string): void {
console.log('\n═══════════════════════════════════════');
console.log(` ${name}: ALL TESTS PASSED ✓`);
console.log('═══════════════════════════════════════\n');
}