refactor: reorganize package structure and decouple framework packages (#338)

* refactor: reorganize package structure and decouple framework packages

## Package Structure Reorganization
- Reorganized 55 packages into categorized subdirectories:
  - packages/framework/ - Generic framework (Laya/Cocos compatible)
  - packages/engine/ - ESEngine core modules
  - packages/rendering/ - Rendering modules (WASM dependent)
  - packages/physics/ - Physics modules
  - packages/streaming/ - World streaming
  - packages/network-ext/ - Network extensions
  - packages/editor/ - Editor framework and plugins
  - packages/rust/ - Rust WASM engine
  - packages/tools/ - Build tools and SDK

## Framework Package Decoupling
- Decoupled behavior-tree and blueprint packages from ESEngine dependencies
- Created abstracted interfaces (IBTAssetManager, IBehaviorTreeAssetContent)
- ESEngine-specific code moved to esengine/ subpath exports
- Framework packages now usable with Cocos/Laya without ESEngine

## CI Configuration
- Updated CI to only type-check and lint framework packages
- Added type-check:framework and lint:framework scripts

## Breaking Changes
- Package import paths changed due to directory reorganization
- ESEngine integrations now use subpath imports (e.g., '@esengine/behavior-tree/esengine')

* fix: update es-engine file path after directory reorganization

* docs: update README to focus on framework over engine

* ci: only build framework packages, remove Rust/WASM dependencies

* fix: remove esengine subpath from behavior-tree and blueprint builds

ESEngine integration code will only be available in full engine builds.
Framework packages are now purely engine-agnostic.

* fix: move network-protocols to framework, build both in CI

* fix: update workflow paths from packages/core to packages/framework/core

* fix: exclude esengine folder from type-check in behavior-tree and blueprint

* fix: update network tsconfig references to new paths

* fix: add test:ci:framework to only test framework packages in CI

* fix: only build core and math npm packages in CI

* fix: exclude test files from CodeQL and fix string escaping security issue
This commit is contained in:
YHH
2025-12-26 14:50:35 +08:00
committed by GitHub
parent a84ff902e4
commit 155411e743
1936 changed files with 4147 additions and 11578 deletions

View File

@@ -0,0 +1,134 @@
/**
* Brush Tool - Paint tiles on the tilemap
*/
import type { ITilemapTool, ToolContext } from './ITilemapTool';
export class BrushTool implements ITilemapTool {
readonly id = 'brush';
readonly name = 'Brush';
readonly icon = 'Paintbrush';
readonly cursor = 'crosshair';
private _isDrawing = false;
private _lastTileX = -1;
private _lastTileY = -1;
onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void {
if (ctx.layerLocked && !ctx.editingCollision) return;
this._isDrawing = true;
this._lastTileX = tileX;
this._lastTileY = tileY;
this.paint(tileX, tileY, ctx);
}
onMouseMove(tileX: number, tileY: number, ctx: ToolContext): void {
if (!this._isDrawing || (ctx.layerLocked && !ctx.editingCollision)) return;
if (tileX === this._lastTileX && tileY === this._lastTileY) return;
// Line drawing between last and current position
this.drawLine(this._lastTileX, this._lastTileY, tileX, tileY, ctx);
this._lastTileX = tileX;
this._lastTileY = tileY;
}
onMouseUp(_tileX: number, _tileY: number, _ctx: ToolContext): void {
this._isDrawing = false;
this._lastTileX = -1;
this._lastTileY = -1;
}
getPreviewTiles(tileX: number, tileY: number, ctx: ToolContext): { x: number; y: number }[] {
const tiles: { x: number; y: number }[] = [];
const selection = ctx.selectedTiles;
if (!selection) {
// Single tile brush
const halfSize = Math.floor(ctx.brushSize / 2);
for (let dy = -halfSize; dy <= halfSize; dy++) {
for (let dx = -halfSize; dx <= halfSize; dx++) {
tiles.push({ x: tileX + dx, y: tileY + dy });
}
}
} else {
// Multi-tile brush
for (let dy = 0; dy < selection.height; dy++) {
for (let dx = 0; dx < selection.width; dx++) {
tiles.push({ x: tileX + dx, y: tileY + dy });
}
}
}
return tiles;
}
private paint(tileX: number, tileY: number, ctx: ToolContext): void {
const { tilemap, selectedTiles, brushSize, editingCollision, currentLayer } = ctx;
if (editingCollision) {
// Paint collision
const halfSize = Math.floor(brushSize / 2);
for (let dy = -halfSize; dy <= halfSize; dy++) {
for (let dx = -halfSize; dx <= halfSize; dx++) {
const x = tileX + dx;
const y = tileY + dy;
if (x >= 0 && x < tilemap.width && y >= 0 && y < tilemap.height) {
tilemap.setCollision(x, y, 1);
}
}
}
} else if (selectedTiles) {
// Paint selected tiles
for (let dy = 0; dy < selectedTiles.height; dy++) {
for (let dx = 0; dx < selectedTiles.width; dx++) {
const x = tileX + dx;
const y = tileY + dy;
if (x >= 0 && x < tilemap.width && y >= 0 && y < tilemap.height) {
const tileIndex = selectedTiles.tiles[dy * selectedTiles.width + dx] ?? 0;
tilemap.setTile(currentLayer, x, y, tileIndex);
}
}
}
} else {
// No selection, paint tile 0 with brush size
const halfSize = Math.floor(brushSize / 2);
for (let dy = -halfSize; dy <= halfSize; dy++) {
for (let dx = -halfSize; dx <= halfSize; dx++) {
const x = tileX + dx;
const y = tileY + dy;
if (x >= 0 && x < tilemap.width && y >= 0 && y < tilemap.height) {
tilemap.setTile(currentLayer, x, y, 1);
}
}
}
}
}
private drawLine(x0: number, y0: number, x1: number, y1: number, ctx: ToolContext): void {
// Bresenham's line algorithm
const dx = Math.abs(x1 - x0);
const dy = Math.abs(y1 - y0);
const sx = x0 < x1 ? 1 : -1;
const sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
let x = x0;
let y = y0;
while (true) {
this.paint(x, y, ctx);
if (x === x1 && y === y1) break;
const e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += sx;
}
if (e2 < dx) {
err += dx;
y += sy;
}
}
}
}

View File

@@ -0,0 +1,98 @@
/**
* Eraser Tool - Remove tiles from the tilemap
*/
import type { ITilemapTool, ToolContext } from './ITilemapTool';
export class EraserTool implements ITilemapTool {
readonly id = 'eraser';
readonly name = 'Eraser';
readonly icon = 'Eraser';
readonly cursor = 'crosshair';
private _isErasing = false;
private _lastTileX = -1;
private _lastTileY = -1;
onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void {
if (ctx.layerLocked && !ctx.editingCollision) return;
this._isErasing = true;
this._lastTileX = tileX;
this._lastTileY = tileY;
this.erase(tileX, tileY, ctx);
}
onMouseMove(tileX: number, tileY: number, ctx: ToolContext): void {
if (!this._isErasing || (ctx.layerLocked && !ctx.editingCollision)) return;
if (tileX === this._lastTileX && tileY === this._lastTileY) return;
this.drawLine(this._lastTileX, this._lastTileY, tileX, tileY, ctx);
this._lastTileX = tileX;
this._lastTileY = tileY;
}
onMouseUp(_tileX: number, _tileY: number, _ctx: ToolContext): void {
this._isErasing = false;
this._lastTileX = -1;
this._lastTileY = -1;
}
getPreviewTiles(tileX: number, tileY: number, ctx: ToolContext): { x: number; y: number }[] {
const tiles: { x: number; y: number }[] = [];
const halfSize = Math.floor(ctx.brushSize / 2);
for (let dy = -halfSize; dy <= halfSize; dy++) {
for (let dx = -halfSize; dx <= halfSize; dx++) {
tiles.push({ x: tileX + dx, y: tileY + dy });
}
}
return tiles;
}
private erase(tileX: number, tileY: number, ctx: ToolContext): void {
const { tilemap, brushSize, editingCollision, currentLayer } = ctx;
const halfSize = Math.floor(brushSize / 2);
for (let dy = -halfSize; dy <= halfSize; dy++) {
for (let dx = -halfSize; dx <= halfSize; dx++) {
const x = tileX + dx;
const y = tileY + dy;
if (x >= 0 && x < tilemap.width && y >= 0 && y < tilemap.height) {
if (editingCollision) {
tilemap.setCollision(x, y, 0);
} else {
tilemap.setTile(currentLayer, x, y, 0);
}
}
}
}
}
private drawLine(x0: number, y0: number, x1: number, y1: number, ctx: ToolContext): void {
const dx = Math.abs(x1 - x0);
const dy = Math.abs(y1 - y0);
const sx = x0 < x1 ? 1 : -1;
const sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
let x = x0;
let y = y0;
while (true) {
this.erase(x, y, ctx);
if (x === x1 && y === y1) break;
const e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += sx;
}
if (e2 < dx) {
err += dx;
y += sy;
}
}
}
}

View File

@@ -0,0 +1,130 @@
/**
* Fill Tool - Flood fill tiles on the tilemap
*/
import type { ITilemapTool, ToolContext } from './ITilemapTool';
export class FillTool implements ITilemapTool {
readonly id = 'fill';
readonly name = 'Fill';
readonly icon = 'PaintBucket';
readonly cursor = 'crosshair';
onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void {
if (ctx.layerLocked && !ctx.editingCollision) return;
this.floodFill(tileX, tileY, ctx);
}
onMouseMove(_tileX: number, _tileY: number, _ctx: ToolContext): void {
// No action on move
}
onMouseUp(_tileX: number, _tileY: number, _ctx: ToolContext): void {
// No action on up
}
getPreviewTiles(tileX: number, tileY: number, ctx: ToolContext): { x: number; y: number }[] {
const { tilemap, editingCollision, currentLayer } = ctx;
if (tileX < 0 || tileX >= tilemap.width || tileY < 0 || tileY >= tilemap.height) {
return [];
}
const tiles: { x: number; y: number }[] = [];
const maxPreviewTiles = 500;
if (editingCollision) {
const targetCollision = tilemap.hasCollision(tileX, tileY);
const stack: [number, number][] = [[tileX, tileY]];
const visited = new Set<string>();
while (stack.length > 0 && tiles.length < maxPreviewTiles) {
const [x, y] = stack.pop()!;
const key = `${x},${y}`;
if (visited.has(key)) continue;
if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
if (tilemap.hasCollision(x, y) !== targetCollision) continue;
visited.add(key);
tiles.push({ x, y });
stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
}
} else {
const targetTile = tilemap.getTile(currentLayer, tileX, tileY);
const stack: [number, number][] = [[tileX, tileY]];
const visited = new Set<string>();
while (stack.length > 0 && tiles.length < maxPreviewTiles) {
const [x, y] = stack.pop()!;
const key = `${x},${y}`;
if (visited.has(key)) continue;
if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
if (tilemap.getTile(currentLayer, x, y) !== targetTile) continue;
visited.add(key);
tiles.push({ x, y });
stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
}
}
return tiles;
}
private floodFill(startX: number, startY: number, ctx: ToolContext): void {
const { tilemap, selectedTiles, editingCollision, currentLayer } = ctx;
if (startX < 0 || startX >= tilemap.width || startY < 0 || startY >= tilemap.height) {
return;
}
if (editingCollision) {
// Flood fill collision
const targetCollision = tilemap.hasCollision(startX, startY);
const newCollision = targetCollision ? 0 : 1;
const stack: [number, number][] = [[startX, startY]];
const visited = new Set<string>();
while (stack.length > 0) {
const [x, y] = stack.pop()!;
const key = `${x},${y}`;
if (visited.has(key)) continue;
if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
if (tilemap.hasCollision(x, y) !== targetCollision) continue;
visited.add(key);
tilemap.setCollision(x, y, newCollision);
stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
}
} else {
// Flood fill tiles
const targetTile = tilemap.getTile(currentLayer, startX, startY);
const newTile = selectedTiles ? (selectedTiles.tiles[0] ?? 1) : 1;
if (targetTile === newTile) return;
const stack: [number, number][] = [[startX, startY]];
const visited = new Set<string>();
while (stack.length > 0) {
const [x, y] = stack.pop()!;
const key = `${x},${y}`;
if (visited.has(key)) continue;
if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
if (tilemap.getTile(currentLayer, x, y) !== targetTile) continue;
visited.add(key);
tilemap.setTile(currentLayer, x, y, newTile);
stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
}
}
}
}

View File

@@ -0,0 +1,31 @@
/**
* Tilemap Tool Interface
*/
import type { TilemapComponent } from '@esengine/tilemap';
import type { TileSelection } from '../stores/TilemapEditorStore';
export interface ToolContext {
tilemap: TilemapComponent;
selectedTiles: TileSelection | null;
currentLayer: number;
layerLocked: boolean;
brushSize: number;
editingCollision: boolean;
tileWidth: number;
tileHeight: number;
}
export interface ITilemapTool {
readonly id: string;
readonly name: string;
readonly icon: string;
readonly cursor: string;
onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void;
onMouseMove(tileX: number, tileY: number, ctx: ToolContext): void;
onMouseUp(tileX: number, tileY: number, ctx: ToolContext): void;
// Preview tiles to highlight during drag
getPreviewTiles?(tileX: number, tileY: number, ctx: ToolContext): { x: number; y: number }[];
}

View File

@@ -0,0 +1,100 @@
/**
* Rectangle Tool - Draw rectangular areas of tiles
*/
import type { ITilemapTool, ToolContext } from './ITilemapTool';
export class RectangleTool implements ITilemapTool {
readonly id = 'rectangle';
readonly name = 'Rectangle';
readonly icon = 'Square';
readonly cursor = 'crosshair';
private _isDrawing = false;
private _startX = -1;
private _startY = -1;
private _currentX = -1;
private _currentY = -1;
onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void {
if (ctx.layerLocked && !ctx.editingCollision) return;
this._isDrawing = true;
this._startX = tileX;
this._startY = tileY;
this._currentX = tileX;
this._currentY = tileY;
}
onMouseMove(tileX: number, tileY: number, _ctx: ToolContext): void {
if (!this._isDrawing) return;
this._currentX = tileX;
this._currentY = tileY;
}
onMouseUp(tileX: number, tileY: number, ctx: ToolContext): void {
if (!this._isDrawing) return;
if (ctx.layerLocked && !ctx.editingCollision) {
this.reset();
return;
}
this._currentX = tileX;
this._currentY = tileY;
this.fillRectangle(ctx);
this.reset();
}
getPreviewTiles(tileX: number, tileY: number, _ctx: ToolContext): { x: number; y: number }[] {
if (!this._isDrawing) {
return [{ x: tileX, y: tileY }];
}
const tiles: { x: number; y: number }[] = [];
const minX = Math.min(this._startX, tileX);
const maxX = Math.max(this._startX, tileX);
const minY = Math.min(this._startY, tileY);
const maxY = Math.max(this._startY, tileY);
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
tiles.push({ x, y });
}
}
return tiles;
}
private fillRectangle(ctx: ToolContext): void {
const { tilemap, selectedTiles, editingCollision, currentLayer } = ctx;
const minX = Math.min(this._startX, this._currentX);
const maxX = Math.max(this._startX, this._currentX);
const minY = Math.min(this._startY, this._currentY);
const maxY = Math.max(this._startY, this._currentY);
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
if (editingCollision) {
tilemap.setCollision(x, y, 1);
} else if (selectedTiles) {
const localX = (x - minX) % selectedTiles.width;
const localY = (y - minY) % selectedTiles.height;
const tileIndex = selectedTiles.tiles[localY * selectedTiles.width + localX] ?? 0;
tilemap.setTile(currentLayer, x, y, tileIndex);
} else {
tilemap.setTile(currentLayer, x, y, 1);
}
}
}
}
private reset(): void {
this._isDrawing = false;
this._startX = -1;
this._startY = -1;
this._currentX = -1;
this._currentY = -1;
}
}

View File

@@ -0,0 +1,94 @@
/**
* Select Tool - Select rectangular regions of tiles for copy/move operations
*/
import type { ITilemapTool, ToolContext } from './ITilemapTool';
import { useTilemapEditorStore } from '../stores/TilemapEditorStore';
export class SelectTool implements ITilemapTool {
readonly id = 'select';
readonly name = 'Select';
readonly icon = 'BoxSelect';
readonly cursor = 'crosshair';
private _isSelecting = false;
private _startX = -1;
private _startY = -1;
private _currentX = -1;
private _currentY = -1;
onMouseDown(tileX: number, tileY: number, _ctx: ToolContext): void {
this._isSelecting = true;
this._startX = tileX;
this._startY = tileY;
this._currentX = tileX;
this._currentY = tileY;
}
onMouseMove(tileX: number, tileY: number, _ctx: ToolContext): void {
if (!this._isSelecting) return;
this._currentX = tileX;
this._currentY = tileY;
}
onMouseUp(tileX: number, tileY: number, ctx: ToolContext): void {
if (!this._isSelecting) return;
this._currentX = tileX;
this._currentY = tileY;
const minX = Math.max(0, Math.min(this._startX, this._currentX));
const maxX = Math.min(ctx.tilemap.width - 1, Math.max(this._startX, this._currentX));
const minY = Math.max(0, Math.min(this._startY, this._currentY));
const maxY = Math.min(ctx.tilemap.height - 1, Math.max(this._startY, this._currentY));
const width = maxX - minX + 1;
const height = maxY - minY + 1;
const tiles: number[] = [];
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
const tileIndex = ctx.tilemap.getTile(ctx.currentLayer, x, y);
tiles.push(tileIndex);
}
}
useTilemapEditorStore.getState().setSelectedTiles({
x: minX,
y: minY,
width,
height,
tiles
});
this.reset();
}
getPreviewTiles(tileX: number, tileY: number, _ctx: ToolContext): { x: number; y: number }[] {
if (!this._isSelecting) {
return [{ x: tileX, y: tileY }];
}
const tiles: { x: number; y: number }[] = [];
const minX = Math.min(this._startX, tileX);
const maxX = Math.max(this._startX, tileX);
const minY = Math.min(this._startY, tileY);
const maxY = Math.max(this._startY, tileY);
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
tiles.push({ x, y });
}
}
return tiles;
}
private reset(): void {
this._isSelecting = false;
this._startX = -1;
this._startY = -1;
this._currentX = -1;
this._currentY = -1;
}
}