Advertisement
ynhhoJ

[TS] camerasManager - kemperrr

Sep 3rd, 2020
3,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const cameraSerialize = (camera: CameraMp) => {
  2.   camera.setActiveCamera = (toggle) => {
  3.     CamerasManager.setActiveCamera(camera, toggle);
  4.   };
  5.  
  6.   camera.setActiveCameraWithInterp = (position, rotation, duration, easeLocation, easeRotation) => {
  7.     CamerasManager.setActiveCameraWithInterp(camera, position, rotation, duration, easeLocation, easeRotation);
  8.   };
  9. };
  10.  
  11. class CamerasManager {
  12.   private static gameplayCamera?: CameraMp;
  13.   private static activeCam?: CameraMp;
  14.   private static interpCamera?: CameraMp;
  15.   private static events = new Map();
  16.   private static cameras = new Map();
  17.  
  18.   // TODO: use node js events
  19.   static on(eventName: string, eventFunction: any) {
  20.     if (!this.events.has(eventName)) {
  21.       this.events.set(eventName, new Set([eventFunction]));
  22.  
  23.       return;
  24.     }
  25.  
  26.     const event = this.events.get(eventName);
  27.  
  28.     if (!event.has(eventFunction)) {
  29.       event.add(eventFunction);
  30.     }
  31.   }
  32.  
  33.   static fireEvent(eventName: string, ...args: any[]) {
  34.     if (!this.events.has(eventName)) {
  35.       return;
  36.     }
  37.  
  38.     const event = this.events.get(eventName);
  39.  
  40.     event.forEach((eventFunction: any) => {
  41.       eventFunction(...args);
  42.     });
  43.   }
  44.  
  45.   // TODO: Need to do smth like: CameraMp | undefined.
  46.   static getCamera(name: string): any {
  47.     const camera = this.cameras.get(name);
  48.  
  49.     if (!camera) {
  50.       mp.console.logWarning(`[cameraManager]: Camera ${name} doesn't exists.`);
  51.  
  52.       return;
  53.     }
  54.  
  55.     if (typeof camera.setActiveCamera !== 'function') {
  56.       cameraSerialize(camera);
  57.     }
  58.  
  59.     return camera;
  60.   }
  61.  
  62.   static setCamera(name: string, camera: CameraMp) {
  63.     this.cameras.set(name, camera);
  64.   }
  65.  
  66.   static hasCamera(name: string): boolean {
  67.     return this.cameras.has(name);
  68.   }
  69.  
  70.   static destroyCamera(camera: CameraMp) {
  71.     if (!camera) {
  72.       mp.console.logError('[cameraManager]: destroyCamera - can\'t be executed.');
  73.  
  74.       return;
  75.     }
  76.  
  77.     if (this.doesExist(camera)) {
  78.       this.setActiveCamera(camera, false);
  79.       camera.destroy();
  80.     }
  81.   }
  82.  
  83.   static createCamera(name: string, type: string, position: Vector3Mp, rotation: Vector3Mp, fov: number): CameraMp {
  84.     const cam = mp.cameras.new(type, position, rotation, fov);
  85.     cameraSerialize(cam);
  86.     this.cameras.set(name, cam);
  87.  
  88.     return cam;
  89.   }
  90.  
  91.   static setActiveCamera(activeCamera: CameraMp, toggle: boolean) {
  92.     if (!activeCamera) {
  93.       mp.console.logError('[cameraManager]: setActiveCamera - can\'t be executed.');
  94.  
  95.       return;
  96.     }
  97.  
  98.     if (!toggle) {
  99.       if (this.doesExist(this.activeCam!)) {
  100.         delete this.activeCam;
  101.         activeCamera.setActive(false);
  102.         mp.game.cam.renderScriptCams(false, false, 0, false, false);
  103.       }
  104.  
  105.       if (this.doesExist(this.interpCamera!)) {
  106.         this.interpCamera!.setActive(false);
  107.         this.interpCamera!.destroy();
  108.         delete this.activeCam;
  109.       }
  110.     } else {
  111.       if (this.doesExist(this.activeCamera)) {
  112.         this.activeCamera.setActive(false);
  113.       }
  114.       this.activeCam = activeCamera;
  115.       activeCamera.setActive(true);
  116.       mp.game.cam.renderScriptCams(true, false, 0, false, false);
  117.     }
  118.   }
  119.  
  120.   static setActiveCameraWithInterp(activeCamera: CameraMp, position: Vector3Mp, rotation: Vector3Mp, duration: number, easeLocation: number, easeRotation: number) {
  121.     if (this.doesExist(this.activeCamera)) {
  122.       this.activeCamera.setActive(false);
  123.     }
  124.  
  125.     if (this.doesExist(this.interpCamera!)) {
  126.       this.fireEvent('stopInterp', this.interpCamera);
  127.  
  128.       this.interpCamera!.setActive(false);
  129.       this.interpCamera!.destroy();
  130.       delete this.interpCamera;
  131.     }
  132.     const interpCamera = mp.cameras.new('default', activeCamera.getCoord(), activeCamera.getRot(2), activeCamera.getFov());
  133.     activeCamera.setCoord(position.x, position.y, position.z);
  134.     activeCamera.setRot(rotation.x, rotation.y, rotation.z, 2);
  135.     activeCamera.stopPointing();
  136.  
  137.     this.activeCam = activeCamera;
  138.     this.interpCamera = interpCamera;
  139.     activeCamera.setActiveWithInterp(interpCamera.handle, duration, easeLocation, easeRotation);
  140.     mp.game.cam.renderScriptCams(true, false, 0, false, false);
  141.  
  142.     this.fireEvent('startInterp', this.interpCamera);
  143.  
  144.     const renderEvent = new mp.Event('render', () => {
  145.       if (this.interpCamera && CamerasManager.doesExist(this.interpCamera) && !this.activeCamera.isInterpolating()) {
  146.         CamerasManager.fireEvent('stopInterp', this.activeCamera);
  147.  
  148.         this.interpCamera!.setActive(false);
  149.         this.interpCamera!.destroy();
  150.  
  151.         delete this.interpCamera;
  152.         renderEvent.destroy();
  153.       }
  154.     });
  155.   }
  156.  
  157.   static doesExist(camera: CameraMp): boolean {
  158.     return mp.cameras.exists(camera) && camera.doesExist();
  159.   }
  160.  
  161.   static get activeCamera(): CameraMp {
  162.     return this.activeCam!;
  163.   }
  164.  
  165.   static get gameplayCam(): CameraMp {
  166.     if (!this.gameplayCamera) {
  167.       this.gameplayCamera = mp.cameras.new('gameplay');
  168.     }
  169.  
  170.     return this.gameplayCamera;
  171.   }
  172. }
  173.  
  174. CamerasManager.on('startInterp', (camera: CameraMp) => {
  175.   mp.console.logInfo('startInterp' + JSON.stringify(camera.getCoord()));
  176. });
  177. CamerasManager.on('stopInterp', (camera: CameraMp) => {
  178.   mp.console.logInfo('stopInterp' + JSON.stringify(camera.getCoord()));
  179. });
  180.  
  181. export default CamerasManager;
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement