Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const cameraSerialize = (camera: CameraMp) => {
- camera.setActiveCamera = (toggle) => {
- CamerasManager.setActiveCamera(camera, toggle);
- };
- camera.setActiveCameraWithInterp = (position, rotation, duration, easeLocation, easeRotation) => {
- CamerasManager.setActiveCameraWithInterp(camera, position, rotation, duration, easeLocation, easeRotation);
- };
- };
- class CamerasManager {
- private static gameplayCamera?: CameraMp;
- private static activeCam?: CameraMp;
- private static interpCamera?: CameraMp;
- private static events = new Map();
- private static cameras = new Map();
- // TODO: use node js events
- static on(eventName: string, eventFunction: any) {
- if (!this.events.has(eventName)) {
- this.events.set(eventName, new Set([eventFunction]));
- return;
- }
- const event = this.events.get(eventName);
- if (!event.has(eventFunction)) {
- event.add(eventFunction);
- }
- }
- static fireEvent(eventName: string, ...args: any[]) {
- if (!this.events.has(eventName)) {
- return;
- }
- const event = this.events.get(eventName);
- event.forEach((eventFunction: any) => {
- eventFunction(...args);
- });
- }
- // TODO: Need to do smth like: CameraMp | undefined.
- static getCamera(name: string): any {
- const camera = this.cameras.get(name);
- if (!camera) {
- mp.console.logWarning(`[cameraManager]: Camera ${name} doesn't exists.`);
- return;
- }
- if (typeof camera.setActiveCamera !== 'function') {
- cameraSerialize(camera);
- }
- return camera;
- }
- static setCamera(name: string, camera: CameraMp) {
- this.cameras.set(name, camera);
- }
- static hasCamera(name: string): boolean {
- return this.cameras.has(name);
- }
- static destroyCamera(camera: CameraMp) {
- if (!camera) {
- mp.console.logError('[cameraManager]: destroyCamera - can\'t be executed.');
- return;
- }
- if (this.doesExist(camera)) {
- this.setActiveCamera(camera, false);
- camera.destroy();
- }
- }
- static createCamera(name: string, type: string, position: Vector3Mp, rotation: Vector3Mp, fov: number): CameraMp {
- const cam = mp.cameras.new(type, position, rotation, fov);
- cameraSerialize(cam);
- this.cameras.set(name, cam);
- return cam;
- }
- static setActiveCamera(activeCamera: CameraMp, toggle: boolean) {
- if (!activeCamera) {
- mp.console.logError('[cameraManager]: setActiveCamera - can\'t be executed.');
- return;
- }
- if (!toggle) {
- if (this.doesExist(this.activeCam!)) {
- delete this.activeCam;
- activeCamera.setActive(false);
- mp.game.cam.renderScriptCams(false, false, 0, false, false);
- }
- if (this.doesExist(this.interpCamera!)) {
- this.interpCamera!.setActive(false);
- this.interpCamera!.destroy();
- delete this.activeCam;
- }
- } else {
- if (this.doesExist(this.activeCamera)) {
- this.activeCamera.setActive(false);
- }
- this.activeCam = activeCamera;
- activeCamera.setActive(true);
- mp.game.cam.renderScriptCams(true, false, 0, false, false);
- }
- }
- static setActiveCameraWithInterp(activeCamera: CameraMp, position: Vector3Mp, rotation: Vector3Mp, duration: number, easeLocation: number, easeRotation: number) {
- if (this.doesExist(this.activeCamera)) {
- this.activeCamera.setActive(false);
- }
- if (this.doesExist(this.interpCamera!)) {
- this.fireEvent('stopInterp', this.interpCamera);
- this.interpCamera!.setActive(false);
- this.interpCamera!.destroy();
- delete this.interpCamera;
- }
- const interpCamera = mp.cameras.new('default', activeCamera.getCoord(), activeCamera.getRot(2), activeCamera.getFov());
- activeCamera.setCoord(position.x, position.y, position.z);
- activeCamera.setRot(rotation.x, rotation.y, rotation.z, 2);
- activeCamera.stopPointing();
- this.activeCam = activeCamera;
- this.interpCamera = interpCamera;
- activeCamera.setActiveWithInterp(interpCamera.handle, duration, easeLocation, easeRotation);
- mp.game.cam.renderScriptCams(true, false, 0, false, false);
- this.fireEvent('startInterp', this.interpCamera);
- const renderEvent = new mp.Event('render', () => {
- if (this.interpCamera && CamerasManager.doesExist(this.interpCamera) && !this.activeCamera.isInterpolating()) {
- CamerasManager.fireEvent('stopInterp', this.activeCamera);
- this.interpCamera!.setActive(false);
- this.interpCamera!.destroy();
- delete this.interpCamera;
- renderEvent.destroy();
- }
- });
- }
- static doesExist(camera: CameraMp): boolean {
- return mp.cameras.exists(camera) && camera.doesExist();
- }
- static get activeCamera(): CameraMp {
- return this.activeCam!;
- }
- static get gameplayCam(): CameraMp {
- if (!this.gameplayCamera) {
- this.gameplayCamera = mp.cameras.new('gameplay');
- }
- return this.gameplayCamera;
- }
- }
- CamerasManager.on('startInterp', (camera: CameraMp) => {
- mp.console.logInfo('startInterp' + JSON.stringify(camera.getCoord()));
- });
- CamerasManager.on('stopInterp', (camera: CameraMp) => {
- mp.console.logInfo('stopInterp' + JSON.stringify(camera.getCoord()));
- });
- export default CamerasManager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement