Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/animation/animation-manager.ts is written in an unsupported language. File is not indexed.

0001 import {
0002   SphereGeometry,
0003   Vector3,
0004   Color,
0005   MeshBasicMaterial,
0006   Mesh,
0007   Scene,
0008   Camera,
0009   WebGLRenderer,
0010 } from 'three';
0011 
0012 // Animation Task Interface
0013 export interface AnimationTask {
0014   name: string;
0015   duration: number;
0016   start(): Promise<void>;
0017   stop?(): void;
0018   pause?(): void;
0019   resume?(): void;
0020 }
0021 
0022 
0023 // Animation Sequence Class
0024 export class AnimationSequence {
0025   private tasks: AnimationTask[] = [];
0026   private currentTaskIndex: number = 0;
0027   private isPlaying: boolean = false;
0028   private isPaused: boolean = false;
0029 
0030   constructor(public name: string) {}
0031 
0032   addTask(task: AnimationTask): AnimationSequence {
0033     this.tasks.push(task);
0034     return this;
0035   }
0036 
0037   async run(): Promise<void> {
0038     if (this.isPlaying) {
0039       return; // Already running
0040     }
0041 
0042     this.isPlaying = true;
0043     this.isPaused = false;
0044 
0045     if (this.currentTaskIndex >= this.tasks.length) {
0046       this.currentTaskIndex = 0; // Reset if we've reached the end
0047     }
0048 
0049     for (
0050       let i = this.currentTaskIndex;
0051       i < this.tasks.length && this.isPlaying;
0052       i++
0053     ) {
0054       const task = this.tasks[i];
0055       this.currentTaskIndex = i;
0056       console.log(`Starting animation task: ${task.name}`);
0057 
0058       if (this.isPaused) {
0059         console.log(`Animation sequence paused at task: ${task.name}`);
0060         await this.waitForResume();
0061       }
0062 
0063       if (!this.isPlaying) {
0064         console.log(
0065           `Animation sequence stopped during task: ${task.name}`
0066         );
0067         break; // Exit loop if stopped
0068       }
0069 
0070       await task.start();
0071       console.log(`Finished animation task: ${task.name}`);
0072     }
0073 
0074     this.isPlaying = false;
0075     this.isPaused = false;
0076     this.currentTaskIndex = 0;
0077   }
0078 
0079   stop(): void {
0080     if (!this.isPlaying) return;
0081 
0082     this.isPlaying = false;
0083     this.isPaused = false;
0084     // Stop the currently running task
0085     const currentTask = this.tasks[this.currentTaskIndex];
0086     if (currentTask && currentTask.stop) {
0087       currentTask.stop();
0088     }
0089   }
0090 
0091   pause(): void {
0092     if (!this.isPlaying || this.isPaused) return;
0093 
0094     this.isPaused = true;
0095     // Pause the currently running task
0096     const currentTask = this.tasks[this.currentTaskIndex];
0097     if (currentTask && currentTask.pause) {
0098       currentTask.pause();
0099     }
0100   }
0101 
0102   resume(): void {
0103     if (!this.isPaused) return;
0104 
0105     this.isPaused = false;
0106     // Resume the currently paused task
0107     const currentTask = this.tasks[this.currentTaskIndex];
0108     if (currentTask && currentTask.resume) {
0109       currentTask.resume();
0110     }
0111   }
0112 
0113   private waitForResume(): Promise<void> {
0114     return new Promise((resolve) => {
0115       const checkResume = () => {
0116         if (!this.isPaused) {
0117           resolve();
0118         } else {
0119           setTimeout(checkResume, 100);
0120         }
0121       };
0122       checkResume();
0123     });
0124   }
0125 }
0126 
0127 
0128 // EicAnimationsManager Class
0129 export class AnimationManager {
0130   private EVENT_DATA_ID: string = 'Event';
0131   private sequences: Map<string, AnimationSequence> = new Map();
0132 
0133   constructor(
0134     private scene: Scene,
0135     private activeCamera: Camera,
0136     private renderer: WebGLRenderer
0137   ) {}
0138 
0139   registerSequence(sequence: AnimationSequence) {
0140     this.sequences.set(sequence.name, sequence);
0141   }
0142 
0143   async playSequence(name: string): Promise<void> {
0144     const sequence = this.sequences.get(name);
0145     if (sequence) {
0146       console.log(`Playing animation sequence: ${sequence.name}`);
0147       try {
0148         await sequence.run();
0149       } catch (error) {
0150         console.error(`Error during sequence ${sequence.name}:`, error);
0151       }
0152       console.log(`Finished animation sequence: ${sequence.name}`);
0153     } else {
0154       console.warn(`No sequence found with name: ${name}`);
0155     }
0156   }
0157 
0158   stopSequence(name: string): void {
0159     const sequence = this.sequences.get(name);
0160     if (sequence) {
0161       sequence.stop();
0162     }
0163   }
0164 
0165   pauseSequence(name: string): void {
0166     const sequence = this.sequences.get(name);
0167     if (sequence) {
0168       sequence.pause();
0169     }
0170   }
0171 
0172   resumeSequence(name: string): void {
0173     const sequence = this.sequences.get(name);
0174     if (sequence) {
0175       sequence.resume();
0176     }
0177   }
0178 }