// @ts-nocheck import type { Controllable } from './Common' import { Cruise } from './Cruise'; import { TaskState, MoveInfo } from './Common' import { SdkError } from '../Error' // 巡航执行器 export abstract class CruiseExecutor> { _restartTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser.restart(); this.restartAction(cruiser); } protected abstract restartAction(cruiser: T): void; _startTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser.start(); this.startAction(cruiser); } protected abstract startAction(cruiser: T): void; _pauseTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser.pause(); this.pauseAction(cruiser); } protected abstract pauseAction(cruiser: T): void; _resumeTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser.resume(); this.resumeAction(cruiser); } protected abstract resumeAction(cruiser: T): void; _finishTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser._finish(); this.finishAction(cruiser); } protected abstract finishAction(cruiser: T): void; _cancelTarget(cruiser: T): void { if (cruiser == undefined) { return; } cruiser.cancel(); this.cancelAction(cruiser); } protected abstract cancelAction(cruiser: T): void; protected abstract run(cruiser: T): void; protected abstract onReachNode(cruiser: T): void; } type CruisebleCllback = >(cruiser: T) => void; // 巡航目标物 export abstract class Cruiseble> implements Controllable{ readonly id: string; _pid: string | undefined = undefined; _cruise: Cruise | undefined = undefined; name: string; _index: number = 0; // 巡航总耗时 _time: number = 0; // 当前所处的路径单元index protected pathIndex: number = 0; // 路径单元移动信息 readonly moveInfoList: Array = []; // 路径单元完成回调 _reachNodeCallback: CruisebleCllback | undefined = undefined; // 巡航完成回调 _finishCallback: CruisebleCllback | undefined = undefined; state: TaskState = TaskState.Default; private repeat: boolean = false; constructor(id: string, name: string) { this.id = id; this.name = name; } dispose(): void { this.pathIndex = 0; this._pid = undefined; this._cruise = undefined; this.moveInfoList.splice(0); this._reachNodeCallback = undefined; this._finishCallback = undefined; } /** * 巡航到达节点回调 * @param callback 回调 * @returns */ onReachNode(callback: CruisebleCllback): T { this._reachNodeCallback = callback; return this as any as T; } /** * 巡航结束回调 * @param callback 回调 * @returns */ onFinish(callback: CruisebleCllback): T { this._finishCallback = callback; return this as any as T; } /** * 设置巡航总耗时 * @param time 巡航总耗时 * @returns */ resetTime(time: number): T { this._time = time; return this as any as T; } // 返回值 --- 是否已走完所有路径 _calculatePosition(): boolean { return this.findCurrentMoveInfo()._calculatePosition(); } isReachEnd() { return (this.pathIndex == this.moveInfoList.length - 1) } _ready(cruise: Cruise, index: number, transH: number): T { this._cruise = cruise; this._pid = cruise.id; this._index = index; if (this._time == 0) { throw SdkError.createDefault('DataError', '巡航目标数据中time不能为0(resetTime)'); } this.moveInfoList.splice(0); // 初始化当前巡航目标的运行路径 cruise._pathUnitList.forEach(unit => { const moveInfo = new MoveInfo(unit, cruise._length, this._time, transH); this.moveInfoList.push(moveInfo); }); this.state = TaskState.Ready; return this as any as T; } _toStartUnit(): T { // 设置运行节点为第一个节点 this.findCurrentMoveInfo().finishCounting(); this.pathIndex = 0; this.findCurrentMoveInfo().startCounting(); return this as any as T; } _toNextUnit(): T { // 设置运行节点 this.findCurrentMoveInfo().finishCounting(); this.pathIndex++; this.findCurrentMoveInfo().startCounting(); return this as any as T; } restart(): T { if (this.state == TaskState.Finished) { // throw SdkError.createDefault('StateError', '此方法仅在巡航目标处于结束(Finished)状态时可用'); this.state = TaskState.Running; } return this as any as T; } start(): T { if (this.state == TaskState.Ready) { // throw SdkError.createDefault('StateError', '此方法仅在巡航目标处于预备(Ready)状态时可用'); this.state = TaskState.Running; } return this as any as T; } pause(): T { if (this.state == TaskState.Running) { // throw SdkError.createDefault('StateError', '此方法仅在巡航目标处于运行(Finished)状态时可用'); this.state = TaskState.Paused; } this.findCurrentMoveInfo().stopCounting(); return this as any as T; } resume(): T { if (this.state == TaskState.Paused) { // throw SdkError.createDefault('StateError', '此方法仅在巡航目标处于暂停(Pause)状态时可用'); this.state = TaskState.Running; } this.findCurrentMoveInfo().startCounting(); return this as any as T; } cancel(): T { this.state = TaskState.Canceled; this.findCurrentMoveInfo().finishCounting(); return this as any as T; } _finish(): T { this.state = TaskState.Finished; this.findCurrentMoveInfo().finishCounting(); return this as any as T; } changeRepeatMode(repeat: boolean) { this.repeat = repeat; return this; } needRepeat(): boolean { return this.repeat; } protected findCurrentMoveInfo(): MoveInfo { return this.moveInfoList[this.pathIndex]; } _isLastInParent() { return (this._index == this._cruise!._cruiserList.length - 1) } }