// @ts-nocheck import type { Disposable } from "../Common" import { Axis } from "../Common" import Utils from "../Utils" /** * 任务内部状态值 */ export enum TaskState { Default = 0, // 默认 Ready = 1, // 已准备 Running = 2, // 执行中 Paused = 3, // 已暂停 Canceled = 4, // 已取消 Finished = 5 // 已结束 } export class Point { readonly id: string; _index?: number; readonly position: Cesium.Cartesian3; static createList(positionList: Array, pTag?: string): Array { const pointList = new Array(positionList.length); for (let i = 0, s = positionList.length; i < s; i++) { const id = (pTag == undefined ? 'point_' + i : pTag + '_point_' + i); const position = positionList[i]; pointList[i] = new Point(id, position); } return pointList; } private constructor(id: string, position: Cesium.Cartesian3) { this.id = id; this.position = position; } addToChain(index: number): Point { this._index = index; return this; } } export type ChainCallback = (unit: PathUnit) => void; // 路径单元 export class PathUnit { readonly tag: string; readonly index: number; readonly sp: Cesium.Cartesian3; readonly ep: Cesium.Cartesian3; // 路径向量 readonly vector: Cesium.Cartesian3; readonly length: number; private constructor(tag: string, index: number, sp: Cesium.Cartesian3, ep: Cesium.Cartesian3) { this.tag = tag; this.index = index; this.sp = sp; this.ep = ep; this.vector = Utils.Vector.create(sp, ep); this.length = Cesium.Cartesian3.distance(sp, ep); } static chain(pTag: string, pointList: Array, pathUnitList: Array, callback?: ChainCallback): void { for (let i = 0, s = pointList.length; i < s; i++) { const ep: Point = pointList[i].addToChain(i).position; if (i == 0) { continue; } const spIndex = i - 1; const tag = pTag + '_' + i; const sp = pointList[spIndex].position; const unit: PathUnit = new PathUnit(tag, spIndex, sp, ep); pathUnitList.push(unit); callback?.(unit); } } } // 行为控制器 export interface Controllable> extends Disposable { state: TaskState; /** * 开始当前任务 */ start(): T; /** * 暂停当前任务 */ pause(): T; /** * 继续当前任务 */ resume(): T; /** * 取消当前任务 */ cancel(): T; /** * 结束当前任务(内部调用) */ _finish(): T; } // 移动信息描述 export class MoveInfo { readonly pathUnit: PathUnit; _position: Cesium.Cartesian3; _sp: Cesium.Cartesian3; _ep: Cesium.Cartesian3; // 上次计时时间 private lastTime: number = 0; // 运行时间 private runTime: number = 0; // 对应路径运行总耗时 readonly totalTime: number; constructor(pathUnit: PathUnit, totalLength: number, time: number, transH: number) { this.pathUnit = pathUnit; this.calculatePathPositions(transH); this.totalTime = pathUnit.length / totalLength * time; } // 向垂直方向平移点位 private calculatePathPositions(diff: number) { if (diff == 0) { this._sp = this.pathUnit.sp; this._ep = this.pathUnit.ep; } else { const axis = Axis.create(this.pathUnit.sp); const lsp = axis.toLocal(this.pathUnit.sp); const lep = axis.toLocal(this.pathUnit.ep); lsp.z += diff; lep.z += diff; this._sp = axis.toWorld(lsp); this._ep = axis.toWorld(lep); } } startCounting(): MoveInfo { this.lastTime = Date.now(); this._position = this._sp; return this; } stopCounting(): MoveInfo { this.lastTime = 0; return this; } finishCounting(): MoveInfo { this.lastTime = 0; this.runTime = 0; this._position = undefined; return this; } // 返回值 --- 是否已走完当前路径 _calculatePosition(): boolean { if (this.lastTime == 0) { return false; } const currentTime = Date.now(); const diffTime = currentTime - this.lastTime; this.lastTime = currentTime; this.runTime += diffTime; const percent = this.runTime / this.totalTime; if (percent < 1) { this._position = Utils.Vector.move(this._sp, Utils.Vector.percent(this.pathUnit.vector, percent)); return false; } else { this._position = this._ep; return true; } } }