| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import { find, Node } from 'cc';
- import { GuideStep } from '../../NewbieGuidePlugin-v1.0.0/NewbieGuidePlugin-v1.0.0/scripts/GuideStep';
- import { BlockManager } from '../CombatSystem/BlockManager';
- import EventBus, { GameEvents } from '../Core/EventBus';
- /**
- * 新手引导步骤:提示并等待玩家将任意方块拖到网格
- * 触发条件:场景中存在方块选择区域,且尚未放置任何方块
- * 完成条件:BlockManager 检测到至少一个方块已移动到 PlacedBlocks
- */
- export class DragBlockToGridStep extends GuideStep {
- private _blockManager: BlockManager | null = null;
- private _pollTimer: any = null;
- private _dragEventsReady = false;
- private _unsubscribers: Array<() => void> = [];
- constructor() {
- super('drag_block_to_grid');
- }
- public canTrigger(): boolean {
- // 查找 BlockManager(位于方块选择系统下)
- const root = find('Canvas');
- if (!root) return false;
- // 在整棵树中查找 BlockManager 组件
- const nodes: Node[] = [];
- const stack: Node[] = [root];
- while (stack.length) {
- const n = stack.pop()!;
- nodes.push(n);
- for (const child of n.children) stack.push(child);
- }
- for (const n of nodes) {
- const bm = n.getComponent(BlockManager as any) as BlockManager | null;
- if (bm) {
- this._blockManager = bm;
- break;
- }
- }
- if (!this._blockManager) return false;
- // 如果已经有方块被放置则不触发
- const hasPlaced = typeof (this._blockManager as any).hasPlacedBlocks === 'function'
- ? (this._blockManager as any).hasPlacedBlocks()
- : false;
- if (hasPlaced) return false;
- // 确认方块选择 UI 存在(避免在战斗中或加载前触发)
- const selectionUI = find('Canvas/GameLevelUI/BlockSelectionUI');
- return !!selectionUI;
- }
- public doExecute(): void {
- // 监听拖拽相关事件,便于在拖拽区域就绪时给出提示或开始轮询
- const bus = EventBus.getInstance();
- const onSetup = () => {
- this._dragEventsReady = true;
- };
- bus.on(GameEvents.SETUP_BLOCK_DRAG_EVENTS, onSetup, this);
- this._unsubscribers.push(() => bus.off(GameEvents.SETUP_BLOCK_DRAG_EVENTS, onSetup, this));
- const onDragEnd = () => {
- // 尝试立即检测一次是否已经放置成功
- this.checkPlacedAndComplete();
- };
- bus.on(GameEvents.BLOCK_DRAG_END, onDragEnd, this);
- this._unsubscribers.push(() => bus.off(GameEvents.BLOCK_DRAG_END, onDragEnd, this));
- // 启动轮询,容错检测放置完成(避免事件遗漏)
- this._pollTimer = setInterval(() => this.checkPlacedAndComplete(), 500);
- }
- private checkPlacedAndComplete() {
- if (!this._blockManager) return;
- try {
- const hasPlaced = typeof (this._blockManager as any).hasPlacedBlocks === 'function'
- ? (this._blockManager as any).hasPlacedBlocks()
- : false;
- if (hasPlaced) {
- this.complete();
- }
- } catch (e) {
- console.error('[DragBlockToGridStep] 检测放置状态失败:', e);
- }
- }
- protected onComplete(): void {
- // 清理事件与轮询
- for (const unsub of this._unsubscribers) {
- try { unsub(); } catch {}
- }
- this._unsubscribers = [];
- if (this._pollTimer) {
- clearInterval(this._pollTimer);
- this._pollTimer = null;
- }
- // 记录由基类完成 complete() 统一处理
- }
- }
- export default DragBlockToGridStep;
|