Files
esengine/packages/tilemap/src/TilemapAnimationSystem.ts

208 lines
6.2 KiB
TypeScript
Raw Normal View History

feat: 添加跨平台运行时、资产系统和UI适配功能 (#256) * feat(platform-common): 添加WASM加载器和环境检测API * feat(rapier2d): 新增Rapier2D WASM绑定包 * feat(physics-rapier2d): 添加跨平台WASM加载器 * feat(asset-system): 添加运行时资产目录和bundle格式 * feat(asset-system-editor): 新增编辑器资产管理包 * feat(editor-core): 添加构建系统和模块管理 * feat(editor-app): 重构浏览器预览使用import maps * feat(platform-web): 添加BrowserRuntime和资产读取 * feat(engine): 添加材质系统和着色器管理 * feat(material): 新增材质系统和着色器编辑器 * feat(tilemap): 增强tilemap编辑器和动画系统 * feat(modules): 添加module.json配置 * feat(core): 添加module.json和类型定义更新 * chore: 更新依赖和构建配置 * refactor(plugins): 更新插件模板使用ModuleManifest * chore: 添加第三方依赖库 * chore: 移除BehaviourTree-ai和ecs-astar子模块 * docs: 更新README和文档主题样式 * fix: 修复Rust文档测试和添加rapier2d WASM绑定 * fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题 * feat(ui): 添加UI屏幕适配系统(CanvasScaler/SafeArea) * fix(ecs-engine-bindgen): 添加缺失的ecs-framework-math依赖 * fix: 添加缺失的包依赖修复CI构建 * fix: 修复CodeQL检测到的代码问题 * fix: 修复构建错误和缺失依赖 * fix: 修复类型检查错误 * fix(material-system): 修复tsconfig配置支持TypeScript项目引用 * fix(editor-core): 修复Rollup构建配置添加tauri external * fix: 修复CodeQL检测到的代码问题 * fix: 修复CodeQL检测到的代码问题
2025-12-03 22:15:22 +08:00
/**
* Tilemap Animation System
*
*
* Manages tile animation playback for all animated tiles in tilesets.
*
*/
import type { ITilesetData, ITileMetadata } from './TilemapComponent';
/**
* Animation state for a single animated tile
*
*/
interface TileAnimationState {
/** Current frame index | 当前帧索引 */
currentFrame: number;
/** Elapsed time since last frame change (ms) | 自上次帧变化以来的时间(毫秒) */
elapsedTime: number;
}
/**
* Tilemap Animation System
*
*/
export class TilemapAnimationSystem {
/** Animation states keyed by "tilesetIndex:tileId" | 按"图块集索引:瓦片ID"索引的动画状态 */
private animationStates: Map<string, TileAnimationState> = new Map();
/** Cached animated tile metadata for quick lookup | 缓存的动画瓦片元数据用于快速查找 */
private animatedTiles: Map<string, ITileMetadata> = new Map();
/** Whether animations are playing | 动画是否正在播放 */
private _isPlaying: boolean = true;
/**
* Register a tileset's animated tiles
*
*/
registerTileset(tilesetIndex: number, tileset: ITilesetData): void {
if (!tileset.tiles) return;
for (const tile of tileset.tiles) {
if (tile.animation && tile.animation.frames.length > 0) {
const key = `${tilesetIndex}:${tile.id}`;
this.animatedTiles.set(key, tile);
this.animationStates.set(key, {
currentFrame: 0,
elapsedTime: 0
});
}
}
}
/**
* Unregister a tileset
*
*/
unregisterTileset(tilesetIndex: number): void {
const keysToRemove: string[] = [];
for (const key of this.animationStates.keys()) {
if (key.startsWith(`${tilesetIndex}:`)) {
keysToRemove.push(key);
}
}
for (const key of keysToRemove) {
this.animationStates.delete(key);
this.animatedTiles.delete(key);
}
}
/**
* Clear all animation states
*
*/
clear(): void {
this.animationStates.clear();
this.animatedTiles.clear();
}
/**
* Update all animations
*
* @param deltaTime Time since last update in milliseconds |
*/
update(deltaTime: number): void {
if (!this._isPlaying) return;
for (const [key, state] of this.animationStates) {
const tile = this.animatedTiles.get(key);
if (!tile?.animation) continue;
const frames = tile.animation.frames;
const currentFrame = frames[state.currentFrame];
if (!currentFrame) continue;
state.elapsedTime += deltaTime;
// Advance frames while elapsed time exceeds frame duration
while (state.elapsedTime >= currentFrame.duration) {
state.elapsedTime -= currentFrame.duration;
state.currentFrame = (state.currentFrame + 1) % frames.length;
}
}
}
/**
* Get the current display tile ID for an animated tile
* ID
* @param tilesetIndex Tileset index |
* @param tileId Original tile ID | ID
* @returns Current frame's tile ID, or original if not animated | IDID
*/
getCurrentTileId(tilesetIndex: number, tileId: number): number {
const key = `${tilesetIndex}:${tileId}`;
const tile = this.animatedTiles.get(key);
const state = this.animationStates.get(key);
if (!tile?.animation || !state) {
return tileId;
}
const frame = tile.animation.frames[state.currentFrame];
return frame?.tileId ?? tileId;
}
/**
* Check if a tile has animation
*
*/
hasAnimation(tilesetIndex: number, tileId: number): boolean {
const key = `${tilesetIndex}:${tileId}`;
return this.animatedTiles.has(key);
}
/**
* Get animation metadata for a tile
*
*/
getAnimation(tilesetIndex: number, tileId: number): ITileMetadata | undefined {
const key = `${tilesetIndex}:${tileId}`;
return this.animatedTiles.get(key);
}
/**
* Reset animation to first frame
*
*/
resetAnimation(tilesetIndex: number, tileId: number): void {
const key = `${tilesetIndex}:${tileId}`;
const state = this.animationStates.get(key);
if (state) {
state.currentFrame = 0;
state.elapsedTime = 0;
}
}
/**
* Reset all animations to first frame
*
*/
resetAll(): void {
for (const state of this.animationStates.values()) {
state.currentFrame = 0;
state.elapsedTime = 0;
}
}
/**
* Play/pause animations
* /
*/
get isPlaying(): boolean {
return this._isPlaying;
}
set isPlaying(value: boolean) {
this._isPlaying = value;
}
/**
* Toggle play/pause
* /
*/
togglePlayback(): boolean {
this._isPlaying = !this._isPlaying;
return this._isPlaying;
}
/**
* Get all animated tile IDs for a tileset
* ID
*/
getAnimatedTileIds(tilesetIndex: number): number[] {
const ids: number[] = [];
for (const key of this.animatedTiles.keys()) {
if (key.startsWith(`${tilesetIndex}:`)) {
const tileId = parseInt(key.split(':')[1], 10);
ids.push(tileId);
}
}
return ids;
}
}
/** Global animation system instance | 全局动画系统实例 */
export const tilemapAnimationSystem = new TilemapAnimationSystem();