Files
oops-plugin-framework/assets/module/common/part/GamePartNode.ts
dgflash 3034bf944a 优化
2026-06-29 18:29:29 +08:00

100 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { instantiate, Node, Prefab, Vec3 } from 'cc';
import type { GameComponent } from '../GameComponent';
import { ViewUtil } from '../../../core/utils/ViewUtil';
import { GamePartBase } from '../GamePartBase';
import { MoveTo } from '../../../libs/animator-move/MoveTo';
import { resLoader } from 'db://oops-framework/core/common/loader/ResLoader';
/** 预制节点与节点树管理 */
export class GamePartNode extends GamePartBase {
/** 宿主组件 */
protected declare comp: GameComponent;
/** 摊平的节点集合(所有节点不能重名)- 延迟初始化 */
private _nodes: Map<string, Node> | null = null;
/** 获取节点集合 */
get nodes(): Map<string, Node> {
if (!this._nodes) {
this._nodes = new Map();
}
return this._nodes;
}
/** 获取节点
* @param name 节点名称
* @returns 节点对象
*/
get(name: string): Node | undefined {
return this._nodes?.get(name);
}
/** 获取节点树信息 */
nodeTreeInfoLite(): void {
this.nodes.clear();
ViewUtil.nodeTreeInfoLite(this.comp.node, this.nodes);
}
/** 创建预制体节点
* @param path 预制体路径
* @param bundleName 资源包名称
* @returns 节点对象
*/
async createPrefabNode(path: string, bundleName: string = resLoader.defaultBundleName): Promise<Node | null> {
const prefab = await this.comp.res.load(bundleName, path, Prefab);
if (!prefab) {
console.warn('[OopsFramework]', `预制体加载失败: ${path}`);
return null;
}
return instantiate(prefab);
}
/**
* 移动节点到指定目标位置
* @param node 要移动的节点
* @param target 目标位置Vec3或目标节点Node
* @param speed 移动速度(每秒移动的像素距离)
* @param options 可选参数配置
* @returns MoveTo组件实例
*/
moveTo(
node: Node,
target: Vec3 | Node,
speed: number,
options?: {
hasYAxis?: boolean;
offset?: number;
offsetVector?: Vec3;
onStart?: () => void;
onComplete?: () => void;
onChange?: () => void;
}
): MoveTo | null {
let moveTo = node.getComponent(MoveTo);
if (!moveTo) {
moveTo = node.addComponent(MoveTo);
}
moveTo.target = target;
moveTo.speed = speed;
if (options) {
if (options.hasYAxis !== undefined) moveTo.hasYAxis = options.hasYAxis;
if (options.offset !== undefined) moveTo.offset = options.offset;
if (options.offsetVector !== undefined) moveTo.offsetVector = options.offsetVector;
if (options.onStart !== undefined) moveTo.onStart = options.onStart;
if (options.onComplete !== undefined) moveTo.onComplete = options.onComplete;
if (options.onChange !== undefined) moveTo.onChange = options.onChange;
}
moveTo.move();
return moveTo;
}
/** 销毁节点模块 */
override destroy(): void {
this._nodes?.clear();
this._nodes = null;
}
}