Files
esengine/packages/tilemap/src/physics/TilemapCollider2DComponent.ts

281 lines
7.9 KiB
TypeScript
Raw Normal View History

/**
* TilemapCollider2D Component
* Tilemap
*
* TilemapComponent
* 使
*/
import { Component, Property, Serialize, ECSComponent, Serializable } from '@esengine/esengine';
/**
*
*/
export enum TilemapColliderMode {
/** 每个碰撞格子单独创建碰撞体 */
PerTile = 0,
/** 合并相邻碰撞格子为更大的矩形 */
Merged = 1,
/** 只生成边缘碰撞体(优化性能) */
EdgeOnly = 2,
}
/**
*
*/
export interface CollisionRect {
x: number;
y: number;
width: number;
height: number;
}
/**
* TilemapCollider2D
*
* TilemapComponent
*
*/
@ECSComponent('TilemapCollider2D')
@Serializable({ version: 1, typeId: 'TilemapCollider2D' })
export class TilemapCollider2DComponent extends Component {
/**
*
*/
@Serialize()
@Property({
type: 'enum',
label: 'Collider Mode',
options: [
{ label: 'Per Tile', value: TilemapColliderMode.PerTile },
{ label: 'Merged', value: TilemapColliderMode.Merged },
{ label: 'Edge Only', value: TilemapColliderMode.EdgeOnly },
],
})
public colliderMode: TilemapColliderMode = TilemapColliderMode.Merged;
/**
*
*/
@Serialize()
@Property({ type: 'collisionLayer', label: 'Collision Layer' })
public collisionLayer: number = 1; // Default layer
/**
*
*/
@Serialize()
@Property({ type: 'collisionMask', label: 'Collision Mask' })
public collisionMask: number = 0xFFFF; // All layers
/**
*
*/
@Serialize()
@Property({ type: 'number', label: 'Friction', min: 0, max: 1, step: 0.01 })
public friction: number = 0.5;
/**
*
*/
@Serialize()
@Property({ type: 'number', label: 'Restitution', min: 0, max: 1, step: 0.01 })
public restitution: number = 0;
/**
*
*/
@Serialize()
@Property({ type: 'boolean', label: 'Is Trigger' })
public isTrigger: boolean = false;
/**
*
* @internal
*/
public _collisionRects: CollisionRect[] = [];
/**
*
* @internal
*/
public _colliderHandles: number[] = [];
/**
*
* @internal
*/
public _needsRebuild: boolean = true;
/**
*
* @internal
*/
public _lastCollisionVersion: number = -1;
/**
*
* @param collisionData
* @param width
* @param height
* @param tileWidth
* @param tileHeight
*/
public generateCollisionRects(
collisionData: Uint32Array,
width: number,
height: number,
tileWidth: number,
tileHeight: number
): CollisionRect[] {
if (collisionData.length === 0) {
this._collisionRects = [];
return [];
}
switch (this.colliderMode) {
case TilemapColliderMode.PerTile:
this._collisionRects = this._generatePerTileRects(
collisionData, width, height, tileWidth, tileHeight
);
break;
case TilemapColliderMode.Merged:
this._collisionRects = this._generateMergedRects(
collisionData, width, height, tileWidth, tileHeight
);
break;
case TilemapColliderMode.EdgeOnly:
// Edge-only 模式暂时使用合并模式
this._collisionRects = this._generateMergedRects(
collisionData, width, height, tileWidth, tileHeight
);
break;
}
this._needsRebuild = true;
return this._collisionRects;
}
/**
*
*/
private _generatePerTileRects(
collisionData: Uint32Array,
width: number,
height: number,
tileWidth: number,
tileHeight: number
): CollisionRect[] {
const rects: CollisionRect[] = [];
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
if (collisionData[row * width + col] > 0) {
rects.push({
x: col * tileWidth,
y: row * tileHeight,
width: tileWidth,
height: tileHeight,
});
}
}
}
return rects;
}
/**
*
*
* 使
*
*/
private _generateMergedRects(
collisionData: Uint32Array,
width: number,
height: number,
tileWidth: number,
tileHeight: number
): CollisionRect[] {
// 创建已处理标记数组
const processed = new Array(width * height).fill(false);
const rects: CollisionRect[] = [];
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const index = row * width + col;
// 跳过已处理或无碰撞的格子
if (processed[index] || collisionData[index] === 0) {
continue;
}
// 找到水平方向最大范围
let endCol = col;
while (
endCol < width &&
collisionData[row * width + endCol] > 0 &&
!processed[row * width + endCol]
) {
endCol++;
}
const rectWidth = endCol - col;
// 找到垂直方向最大范围(保持相同宽度)
let endRow = row;
let canExtend = true;
while (canExtend && endRow < height) {
// 检查这一行是否都有碰撞且未处理
for (let c = col; c < endCol; c++) {
const idx = endRow * width + c;
if (collisionData[idx] === 0 || processed[idx]) {
canExtend = false;
break;
}
}
if (canExtend) {
endRow++;
}
}
const rectHeight = endRow - row;
// 标记所有包含的格子为已处理
for (let r = row; r < endRow; r++) {
for (let c = col; c < endCol; c++) {
processed[r * width + c] = true;
}
}
// 添加合并后的矩形
rects.push({
x: col * tileWidth,
y: row * tileHeight,
width: rectWidth * tileWidth,
height: rectHeight * tileHeight,
});
}
}
return rects;
}
/**
*
*/
public getCollisionRectCount(): number {
return this._collisionRects.length;
}
/**
*
*/
public markNeedsRebuild(): void {
this._needsRebuild = true;
}
public override onRemovedFromEntity(): void {
this._collisionRects = [];
this._colliderHandles = [];
}
}