import { ChapterNewPlayTypeEnum } from '../../../game/ChapterPlayTypes'; import type { IBattleMechanism, MechanismContext } from '../IBattleMechanism'; import { pickRandomDigGrid, pickRandomSpiderWebTarget, playSpiderSpiritVisual, } from '../MechanismServices'; /** 蜘蛛精 / 蛛网机制(Playmode 12) */ export class SpiderSpiritMechanism implements IBattleMechanism { readonly typeId = ChapterNewPlayTypeEnum.VinesGrass; private _ctx: MechanismContext | null = null; private _timeArr: number[] = []; private _eventIndex = 0; private _checkTimer = 0; private _visualPlaying = false; init(ctx: MechanismContext, config: unknown): void { this._ctx = ctx; const src = config as number[] | undefined; this._timeArr = src?.slice() ?? []; this._eventIndex = 0; this._checkTimer = 0; this._visualPlaying = false; if (!this._timeArr.length) { console.error('[SpiderSpiritMechanism] 配表 timeArr 为空,机制不会触发'); return; } } tick(dt: number): void { if (!this._ctx) return; this._checkTimer += dt; if (this._checkTimer < 1) return; this._checkTimer = 0; this._tryTriggerDueEvents(); } destroy(): void { this._visualPlaying = false; this._ctx = null; this._timeArr = []; this._eventIndex = 0; } private _tryTriggerDueEvents(): void { const ctx = this._ctx; if (!ctx || this._visualPlaying) return; while (this._eventIndex < this._timeArr.length && ctx.battleTime >= this._timeArr[this._eventIndex]) { if (!this._tryTriggerOnce()) break; this._eventIndex++; break; } } private _tryTriggerOnce(): boolean { const ctx = this._ctx; if (!ctx) return false; const appearGrid = pickRandomDigGrid(ctx); if (!appearGrid) { console.log( '[SpiderSpiritMechanism] 无可开挖出现落点,event=', this._eventIndex, 'battleTime=', ctx.battleTime, ); return false; } const targetGrid = pickRandomSpiderWebTarget(ctx); if (!targetGrid) { console.log( '[SpiderSpiritMechanism] 无可用吐丝目标,event=', this._eventIndex, 'battleTime=', ctx.battleTime, ); return false; } this._visualPlaying = true; const started = playSpiderSpiritVisual(ctx, appearGrid, targetGrid, () => { this._visualPlaying = false; }); if (!started) { this._visualPlaying = false; console.log('[SpiderSpiritMechanism] 蜘蛛精表现创建失败,event=', this._eventIndex); return false; } return true; } }