HumanBreak/src/core/render/container.ts

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-05-09 23:49:53 +08:00
import { MotaOffscreenCanvas2D } from '../fx/canvas2d';
2024-08-24 00:43:22 +08:00
import {
ERenderItemEvent,
IRenderChildable,
RenderItem,
RenderItemPosition
} from './item';
import { Transform } from './transform';
2024-05-09 23:49:53 +08:00
2024-08-24 00:43:22 +08:00
export interface EContainerEvent extends ERenderItemEvent {}
export class Container<E extends EContainerEvent = EContainerEvent>
extends RenderItem<E | EContainerEvent>
implements IRenderChildable
{
2024-05-09 23:49:53 +08:00
children: RenderItem[] = [];
sortedChildren: RenderItem[] = [];
2024-08-23 20:58:23 +08:00
private needSort: boolean = false;
2024-08-17 23:11:20 +08:00
/**
*
* @param type absolute表示绝对位置static表示跟随摄像机移动
2024-08-17 23:11:20 +08:00
* @param cache
*/
constructor(type: RenderItemPosition = 'static', cache: boolean = true) {
2024-08-23 20:58:23 +08:00
super(type, cache);
2024-05-09 23:49:53 +08:00
this.type = type;
2024-08-17 23:11:20 +08:00
}
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {
2024-08-17 23:11:20 +08:00
const { ctx } = canvas;
2024-08-23 20:58:23 +08:00
2024-08-17 23:11:20 +08:00
this.sortedChildren.forEach(v => {
if (v.hidden) return;
ctx.save();
v.renderContent(canvas, transform);
2024-08-17 23:11:20 +08:00
ctx.restore();
});
2024-05-09 23:49:53 +08:00
}
/**
* tick执行更新
* @param children
*/
appendChild(...children: RenderItem<any>[]) {
2024-05-09 23:49:53 +08:00
children.forEach(v => (v.parent = this));
this.children.push(...children);
2024-08-23 20:58:23 +08:00
if (!this.needSort) {
this.needSort = true;
this.requestBeforeFrame(() => {
this.needSort = false;
this.sortChildren();
});
}
this.update(this);
}
removeChild(...child: RenderItem<any>[]): void {
child.forEach(v => {
const index = this.children.indexOf(v);
if (index === -1) return;
this.children.splice(index, 1);
});
this.sortChildren();
this.update(this);
2024-05-16 22:43:24 +08:00
}
sortChildren() {
2024-05-09 23:49:53 +08:00
this.sortedChildren = this.children
.slice()
.sort((a, b) => a.zIndex - b.zIndex);
}
2024-05-16 22:43:24 +08:00
destroy(): void {
super.destroy();
this.children.forEach(v => {
v.destroy();
});
}
2024-05-09 23:49:53 +08:00
}