send camera update event from CameraManager

This commit is contained in:
Hanusiak Piotr 2021-12-17 11:05:11 +01:00
parent 4871b406de
commit f8353bd7b5
3 changed files with 62 additions and 23 deletions

View File

@ -20,6 +20,18 @@ export enum CameraMode {
Focus = "Focus", Focus = "Focus",
} }
export enum CameraManagerEvent {
CameraUpdate = "CameraUpdate",
}
export interface CameraManagerEventCameraUpdateData {
x: number;
y: number;
width: number;
height: number;
zoom: number;
}
export class CameraManager extends Phaser.Events.EventEmitter { export class CameraManager extends Phaser.Events.EventEmitter {
private scene: GameScene; private scene: GameScene;
private camera: Phaser.Cameras.Scene2D.Camera; private camera: Phaser.Cameras.Scene2D.Camera;
@ -78,6 +90,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
(camera, progress, x, y) => { (camera, progress, x, y) => {
if (this.cameraMode === CameraMode.Positioned) { if (this.cameraMode === CameraMode.Positioned) {
this.waScaleManager.zoomModifier = currentZoomModifier + progress * zoomModifierChange; this.waScaleManager.zoomModifier = currentZoomModifier + progress * zoomModifierChange;
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
} }
if (progress === 1) { if (progress === 1) {
this.playerToFollow?.once(hasMovedEventName, () => { this.playerToFollow?.once(hasMovedEventName, () => {
@ -125,6 +138,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
true, true,
(camera, progress, x, y) => { (camera, progress, x, y) => {
this.waScaleManager.zoomModifier = currentZoomModifier + progress * zoomModifierChange; this.waScaleManager.zoomModifier = currentZoomModifier + progress * zoomModifierChange;
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
} }
); );
} }
@ -157,6 +171,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
const shiftY = const shiftY =
(this.playerToFollow.y - this.camera.worldView.height * 0.5 - oldPos.y) * tween.getValue(); (this.playerToFollow.y - this.camera.worldView.height * 0.5 - oldPos.y) * tween.getValue();
this.camera.setScroll(oldPos.x + shiftX, oldPos.y + shiftY); this.camera.setScroll(oldPos.x + shiftX, oldPos.y + shiftY);
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
}, },
onComplete: () => { onComplete: () => {
this.camera.startFollow(player, true); this.camera.startFollow(player, true);
@ -205,6 +220,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
ease: Easing.SineEaseOut, ease: Easing.SineEaseOut,
onUpdate: (tween: Phaser.Tweens.Tween) => { onUpdate: (tween: Phaser.Tweens.Tween) => {
this.waScaleManager.zoomModifier = tween.getValue(); this.waScaleManager.zoomModifier = tween.getValue();
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
}, },
}); });
} }
@ -222,7 +238,26 @@ export class CameraManager extends Phaser.Events.EventEmitter {
return; return;
} }
this.camera.centerOn(focusOn.x + focusOn.width * 0.5, focusOn.y + focusOn.height * 0.5); this.camera.centerOn(focusOn.x + focusOn.width * 0.5, focusOn.y + focusOn.height * 0.5);
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
} }
); );
this.camera.on("followupdate", () => {
this.sendCameraUpdateEvent();
});
}
private sendCameraUpdateEvent(): void {
this.emit(CameraManagerEvent.CameraUpdate, this.getCameraUpdateEventData());
}
private getCameraUpdateEventData(): CameraManagerEventCameraUpdateData {
return {
x: this.camera.worldView.x,
y: this.camera.worldView.y,
width: this.camera.worldView.width,
height: this.camera.worldView.height,
zoom: this.camera.scaleManager.zoom,
};
} }
} }

View File

@ -65,7 +65,7 @@ import type { ActionableItem } from "../Items/ActionableItem";
import type { ItemFactoryInterface } from "../Items/ItemFactoryInterface"; import type { ItemFactoryInterface } from "../Items/ItemFactoryInterface";
import type { ITiledMap, ITiledMapLayer, ITiledMapProperty, ITiledMapObject, ITiledTileSet } from "../Map/ITiledMap"; import type { ITiledMap, ITiledMapLayer, ITiledMapProperty, ITiledMapObject, ITiledTileSet } from "../Map/ITiledMap";
import type { AddPlayerInterface } from "./AddPlayerInterface"; import type { AddPlayerInterface } from "./AddPlayerInterface";
import { CameraManager } from "./CameraManager"; import { CameraManager, CameraManagerEvent, CameraManagerEventCameraUpdateData } from "./CameraManager";
import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent"; import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent";
import type { Character } from "../Entity/Character"; import type { Character } from "../Entity/Character";
@ -1102,13 +1102,15 @@ ${escapedMessage}
this.iframeSubscriptionList.push( this.iframeSubscriptionList.push(
iframeListener.trackCameraUpdateStream.subscribe(() => { iframeListener.trackCameraUpdateStream.subscribe(() => {
if (!this.firstCameraUpdateSent) { if (!this.firstCameraUpdateSent) {
this.cameras.main.on("followupdate", (camera: Camera) => { this.cameraManager.on(
CameraManagerEvent.CameraUpdate,
(data: CameraManagerEventCameraUpdateData) => {
const cameraEvent: WasCameraUpdatedEvent = { const cameraEvent: WasCameraUpdatedEvent = {
x: camera.worldView.x, x: data.x,
y: camera.worldView.y, y: data.y,
width: camera.worldView.width, width: data.width,
height: camera.worldView.height, height: data.height,
zoom: camera.scaleManager.zoom, zoom: data.zoom,
}; };
if ( if (
this.lastCameraEvent?.x == cameraEvent.x && this.lastCameraEvent?.x == cameraEvent.x &&
@ -1123,7 +1125,8 @@ ${escapedMessage}
this.lastCameraEvent = cameraEvent; this.lastCameraEvent = cameraEvent;
iframeListener.sendCameraUpdated(cameraEvent); iframeListener.sendCameraUpdated(cameraEvent);
this.firstCameraUpdateSent = true; this.firstCameraUpdateSent = true;
}); }
);
iframeListener.sendCameraUpdated(this.cameras.main); iframeListener.sendCameraUpdated(this.cameras.main);
} }

View File

@ -4,7 +4,8 @@
<script src="<?php echo $_SERVER["FRONT_URL"] ?>/iframe_api.js"></script> <script src="<?php echo $_SERVER["FRONT_URL"] ?>/iframe_api.js"></script>
<script> <script>
window.addEventListener('load', () => { window.addEventListener('load', () => {
console.log('On load'); //@ts-ignore
WA.camera.onCameraUpdate((worldView) => console.log(worldView));
WA.onInit().then(() => { WA.onInit().then(() => {
console.log('After WA init'); console.log('After WA init');
const setPositionButton = document.getElementById('setPositionButton'); const setPositionButton = document.getElementById('setPositionButton');