新增引导1至引导4四个引导预制体及对应元数据文件 优化GuideComp组件:添加触摸事件监听与销毁解绑逻辑,实现事件穿透以保证底层功能按钮正常响应 调整已完成引导列表的初始值从空数组改为包含0号引导项 更新角色控制器预制体的引导相关配置参数
126 lines
5.0 KiB
TypeScript
126 lines
5.0 KiB
TypeScript
|
|
import { mLogger } from "../common/Logger";
|
|
import { _decorator, Animation, AnimationClip, EventTouch, Label, Node, NodeEventType, Sprite, SpriteAtlas, Tween, tween, UIOpacity, Vec3, resources, Light, UITransform, Widget, CCInteger, CCString } from "cc";
|
|
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
|
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
|
|
import { smc } from "../common/SingletonModuleComp";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('GuideComp')
|
|
@ecs.register('GuideComp', false)
|
|
export class GuideComp extends CCComp {
|
|
/** 是否开启调试日志 */
|
|
private debugMode: boolean = true;
|
|
|
|
// ======================== 编辑器绑定节点 ========================
|
|
|
|
/** 手势图标节点 */
|
|
@property(Node)
|
|
hand: Node = null!
|
|
/** 手势图标节点 */
|
|
@property(Node)
|
|
clickNode: Node = null!
|
|
/** 引导编号 ID */
|
|
@property({ type: CCInteger })
|
|
guide_id: number = 0
|
|
/** 动画名称 ID */
|
|
@property({ type: CCString })
|
|
animation_name: string = 'dianji';
|
|
|
|
onLoad() {
|
|
// 如果该引导已经完成,则隐藏并销毁本节点
|
|
if (smc.finish_guides.includes(this.guide_id)) {
|
|
this.node.active = false;
|
|
this.node.destroy();
|
|
return;
|
|
}
|
|
|
|
// 播放手势动画
|
|
if (this.hand && this.animation_name) {
|
|
// 延迟一帧播放,防止组件还未完全激活
|
|
this.scheduleOnce(() => {
|
|
if (!this.isValid) return; // 防止节点已被销毁
|
|
|
|
// 1. 先尝试在绑定的 this.hand 本身上查找 Animation 组件
|
|
let anim = this.hand.getComponent(Animation);
|
|
|
|
// 2. 如果没找到,再尝试去它的子节点上找
|
|
if (!anim) {
|
|
anim = this.hand.getComponentInChildren(Animation);
|
|
}
|
|
|
|
if (anim) {
|
|
// 确保目标动画状态存在
|
|
const animState = anim.getState(this.animation_name);
|
|
if (animState) {
|
|
anim.play(this.animation_name);
|
|
mLogger.log(this.debugMode, 'Guide', `成功播放引导动画: ${this.animation_name}`);
|
|
} else {
|
|
mLogger.warn(this.debugMode, 'Guide', `找到了 Animation 组件,但组件内未挂载名为 '${this.animation_name}' 的动画剪辑(Clip)。请检查编辑器 Animation 组件的 Clips 列表!`);
|
|
}
|
|
} else {
|
|
mLogger.warn(this.debugMode, 'Guide', `在 hand 节点及其子节点下均未找到 Animation 组件,无法播放动画: ${this.animation_name}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 监听手势节点点击事件
|
|
if (this.hand) {
|
|
this.hand.on(NodeEventType.TOUCH_START, this.onTouchStart, this);
|
|
this.hand.on(NodeEventType.TOUCH_END, this.onGuideClick, this);
|
|
} else {
|
|
// 兜底:如果没有绑定手势节点,则监听自身节点
|
|
this.node.on(NodeEventType.TOUCH_START, this.onTouchStart, this);
|
|
this.node.on(NodeEventType.TOUCH_END, this.onGuideClick, this);
|
|
}
|
|
}
|
|
|
|
private onTouchStart(event: EventTouch) {
|
|
// 允许事件穿透,确保底层的实际功能按钮能正常接收到 TOUCH_START 事件
|
|
event.preventSwallow = true;
|
|
}
|
|
|
|
private onGuideClick(event: EventTouch) {
|
|
// 记录该引导 ID 已完成
|
|
if (!smc.finish_guides.includes(this.guide_id)) {
|
|
smc.finish_guides.push(this.guide_id);
|
|
mLogger.log(this.debugMode, 'Guide', `完成引导 ID: ${this.guide_id}`);
|
|
|
|
// TODO: 若需要持久化保存,可在此处调用本地存储或同步到服务器的方法
|
|
// oops.storage.set("finish_guides", smc.finish_guides);
|
|
}
|
|
|
|
// 允许事件穿透,确保底层的实际功能按钮能正常接收到点击事件
|
|
event.preventSwallow = true;
|
|
|
|
// 完成后隐藏并销毁引导节点
|
|
this.node.active = false;
|
|
this.node.destroy();
|
|
}
|
|
|
|
/** 组件销毁时解绑所有事件,防止残留回调 */
|
|
onDestroy() {
|
|
if (this.hand && this.hand.isValid) {
|
|
this.hand.off(NodeEventType.TOUCH_START, this.onTouchStart, this);
|
|
this.hand.off(NodeEventType.TOUCH_END, this.onGuideClick, this);
|
|
}
|
|
if (this.node && this.node.isValid) {
|
|
this.node.off(NodeEventType.TOUCH_START, this.onTouchStart, this);
|
|
this.node.off(NodeEventType.TOUCH_END, this.onGuideClick, this);
|
|
}
|
|
}
|
|
|
|
/** 外部初始化入口(由 MissionGuideComp 调用) */
|
|
init() {
|
|
|
|
}
|
|
|
|
/** ECS 组件移除时的释放钩子:销毁节点 */
|
|
reset() {
|
|
if (this.node && this.node.isValid) {
|
|
this.node.destroy();
|
|
}
|
|
}
|
|
}
|