feat(engine-core): 添加统一输入系统 (#282)

* perf(core): 优化 EntitySystem 迭代性能,添加 CommandBuffer 延迟命令

ReactiveQuery 快照优化:
- 添加快照机制,避免每帧拷贝数组
- 只在实体列表变化时创建新快照
- 静态场景下多个系统共享同一快照

CommandBuffer 延迟命令系统:
- 支持延迟添加/移除组件、销毁实体、设置实体激活状态
- 每个系统拥有独立的 commands 属性
- 命令在帧末统一执行,避免迭代过程中修改实体列表

Scene 更新:
- 在 lateUpdate 后自动刷新所有系统的命令缓冲区

文档:
- 更新系统文档,添加 CommandBuffer 使用说明

* fix(ci): upgrade first-interaction action to v1.3.0

Fix Docker build failure in welcome workflow.

* fix(ci): upgrade pnpm/action-setup to v4 and fix unused import

- Upgrade pnpm/action-setup@v2 to v4 in all workflow files
- Remove unused CommandType import in CommandBuffer.test.ts

* fix(ci): remove duplicate pnpm version specification

* feat(engine-core): 添加统一输入系统

添加完整的输入系统,支持平台抽象:

- IPlatformInputSubsystem: 扩展接口支持键盘/鼠标/滚轮事件
- WebInputSubsystem: 浏览器实现,支持事件绑定/解绑
- InputManager: 全局输入状态管理器(键盘、鼠标、触摸)
- InputSystem: ECS 系统,连接平台事件到 InputManager
- GameRuntime 集成: 自动创建 InputSystem 并绑定平台子系统

使用方式:
```typescript
import { Input, MouseButton } from '@esengine/engine-core';

if (Input.isKeyDown('KeyW')) { /* 移动 */ }
if (Input.isKeyJustPressed('Space')) { /* 跳跃 */ }
if (Input.isMouseButtonDown(MouseButton.Left)) { /* 射击 */ }
```

* fix(runtime-core): 添加缺失的 platform-common 依赖

* fix(runtime-core): 移除 platform-web 依赖避免循环依赖

* fix(runtime-core): 使用工厂函数注入 InputSubsystem 避免循环依赖

- BrowserPlatformAdapter 通过 inputSubsystemFactory 配置接收输入子系统
- 在 IPlatformInputSubsystem 接口添加可选的 dispose 方法
- 移除对 @esengine/platform-web 的直接依赖
This commit is contained in:
YHH
2025-12-05 18:15:50 +08:00
committed by GitHub
parent 13a149c3a2
commit 823e0c1d94
14 changed files with 1412 additions and 13 deletions

View File

@@ -8,7 +8,7 @@
import { Core, Scene, SceneSerializer, HierarchySystem } from '@esengine/ecs-framework';
import { EngineBridge, EngineRenderSystem, CameraSystem } from '@esengine/ecs-engine-bindgen';
import { TransformComponent, TransformSystem } from '@esengine/engine-core';
import { TransformComponent, TransformSystem, InputSystem, Input } from '@esengine/engine-core';
import { AssetManager, EngineIntegration } from '@esengine/asset-system';
import {
runtimePluginManager,
@@ -77,6 +77,7 @@ export class GameRuntime {
private _scene: Scene | null = null;
private _renderSystem: EngineRenderSystem | null = null;
private _cameraSystem: CameraSystem | null = null;
private _inputSystem: InputSystem | null = null;
private _assetManager: AssetManager | null = null;
private _engineIntegration: EngineIntegration | null = null;
private _projectConfig: ProjectConfig;
@@ -228,6 +229,19 @@ export class GameRuntime {
this._scene.addSystem(new HierarchySystem());
this._scene.addSystem(new TransformSystem());
// 7. 添加输入系统(最先更新,以便其他系统可以读取输入状态)
// Add input system (updates first so other systems can read input state)
this._inputSystem = new InputSystem({
disableInEditor: true // 编辑器模式下禁用,避免与编辑器输入冲突
});
this._scene.addSystem(this._inputSystem);
// 设置平台输入子系统 | Set platform input subsystem
const inputSubsystem = this._platform.getInputSubsystem?.();
if (inputSubsystem) {
this._inputSystem.setInputSubsystem(inputSubsystem);
}
this._cameraSystem = new CameraSystem(this._bridge);
this._scene.addSystem(this._cameraSystem);
@@ -257,7 +271,9 @@ export class GameRuntime {
isEditor: this._platform.isEditorMode(),
engineBridge: this._bridge,
renderSystem: this._renderSystem,
assetManager: this._assetManager
assetManager: this._assetManager,
inputSystem: this._inputSystem,
inputManager: Input
};
// 11. 让插件创建系统(编辑器模式下跳过,由 EngineService.initializeModuleSystems 处理)
@@ -845,6 +861,7 @@ export class GameRuntime {
this._renderSystem = null;
this._cameraSystem = null;
this._inputSystem = null;
this._systemContext = null;
this._platform.dispose();

View File

@@ -6,6 +6,8 @@
* Defines the adapter interface that different platforms need to implement
*/
import type { IPlatformInputSubsystem } from '@esengine/platform-common';
/**
* 资源路径解析器
* Asset path resolver
@@ -127,6 +129,15 @@ export interface IPlatformAdapter {
*/
setShowGizmos?(show: boolean): void;
/**
* 获取输入子系统
* Get input subsystem
*
* 返回平台特定的输入子系统实现
* Returns platform-specific input subsystem implementation
*/
getInputSubsystem?(): IPlatformInputSubsystem | null;
/**
* 释放资源
* Dispose resources

View File

@@ -12,6 +12,7 @@ import type {
PlatformCapabilities,
PlatformAdapterConfig
} from '../IPlatformAdapter';
import type { IPlatformInputSubsystem } from '@esengine/platform-common';
/**
* 浏览器路径解析器
@@ -56,6 +57,11 @@ export interface BrowserPlatformConfig {
wasmModuleLoader?: () => Promise<any>;
/** 资产基础 URL */
assetBaseUrl?: string;
/**
* 输入子系统工厂函数
* Input subsystem factory function
*/
inputSubsystemFactory?: () => IPlatformInputSubsystem;
}
/**
@@ -77,6 +83,7 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
private _canvas: HTMLCanvasElement | null = null;
private _config: BrowserPlatformConfig;
private _viewportSize = { width: 0, height: 0 };
private _inputSubsystem: IPlatformInputSubsystem | null = null;
constructor(config: BrowserPlatformConfig = {}) {
this._config = config;
@@ -100,6 +107,10 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
this._canvas.width = width;
this._canvas.height = height;
this._viewportSize = { width, height };
if (this._config.inputSubsystemFactory) {
this._inputSubsystem = this._config.inputSubsystemFactory();
}
}
async getWasmModule(): Promise<any> {
@@ -137,7 +148,15 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
return false;
}
getInputSubsystem(): IPlatformInputSubsystem | null {
return this._inputSubsystem;
}
dispose(): void {
if (this._inputSubsystem) {
this._inputSubsystem.dispose?.();
this._inputSubsystem = null;
}
this._canvas = null;
}
}

View File

@@ -70,3 +70,20 @@ export {
type AssetCatalogEntry,
type BrowserFileSystemOptions
} from './services/BrowserFileSystemService';
// Re-export Input System from engine-core for convenience
export {
Input,
InputManager,
InputSystem,
MouseButton,
type InputSystemConfig,
type KeyState,
type MouseButtonState,
type Vector2,
type KeyboardEventInfo,
type MouseEventInfo,
type WheelEventInfo,
type TouchInfo,
type TouchEvent
} from '@esengine/engine-core';