fallback in case of CanvasRenderer

This commit is contained in:
Hanusiak Piotr 2021-12-04 16:29:28 +01:00
parent 3e904447d3
commit 8016cf3c1a
4 changed files with 18 additions and 9 deletions

View File

@ -10,8 +10,8 @@ import { onDestroy } from 'svelte';
const playerWokaPictureStore = gameScene.getUserWokaPictureStore(userId); const playerWokaPictureStore = gameScene.getUserWokaPictureStore(userId);
let src = placeholderSrc; let src = placeholderSrc;
const unsubscribe = playerWokaPictureStore.picture.subscribe((htmlElement) => { const unsubscribe = playerWokaPictureStore.picture.subscribe((source) => {
src = htmlElement?.src ?? placeholderSrc; src = source ?? placeholderSrc;
}); });
onDestroy(unsubscribe); onDestroy(unsubscribe);

View File

@ -10,7 +10,7 @@ import type { GameScene } from "../Game/GameScene";
import { DEPTH_INGAME_TEXT_INDEX } from "../Game/DepthIndexes"; import { DEPTH_INGAME_TEXT_INDEX } from "../Game/DepthIndexes";
import type OutlinePipelinePlugin from "phaser3-rex-plugins/plugins/outlinepipeline-plugin.js"; import type OutlinePipelinePlugin from "phaser3-rex-plugins/plugins/outlinepipeline-plugin.js";
import { isSilentStore } from "../../Stores/MediaStore"; import { isSilentStore } from "../../Stores/MediaStore";
import { lazyLoadPlayerCharacterTextures } from "./PlayerTexturesLoadingManager"; import { lazyLoadPlayerCharacterTextures, loadAllDefaultModels } from "./PlayerTexturesLoadingManager";
const playerNameY = -25; const playerNameY = -25;
@ -117,17 +117,26 @@ export abstract class Character extends Container {
} }
} }
public async getSnapshot(): Promise<HTMLImageElement> { public async getSnapshot(): Promise<string> {
const rt = this.scene.make.renderTexture({}, false); const rt = this.scene.make.renderTexture({}, false);
if (rt.renderer instanceof Phaser.Renderer.Canvas.CanvasRenderer) {
rt.destroy();
for (const sprite of this.sprites.values()) {
// we can be sure that either predefined woka or body texture is at this point loaded
if (sprite.texture.key.includes("color") || sprite.texture.key.includes("male")) {
return this.scene.textures.getBase64(sprite.texture.key);
}
}
}
for (const sprite of this.sprites.values()) { for (const sprite of this.sprites.values()) {
sprite.setFrame(1); sprite.setFrame(1);
rt.draw(sprite, sprite.displayWidth * 0.5, sprite.displayHeight * 0.5); rt.draw(sprite, sprite.displayWidth * 0.5, sprite.displayHeight * 0.5);
} }
return new Promise<HTMLImageElement>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
try { try {
rt.snapshot( rt.snapshot(
(url) => { (url) => {
resolve(url as HTMLImageElement); resolve((url as HTMLImageElement).src);
rt.destroy(); rt.destroy();
}, },
"image/png", "image/png",

View File

@ -1535,8 +1535,8 @@ ${escapedMessage}
} }
private async savePlayerWokaPicture(character: Character, userId: number): Promise<void> { private async savePlayerWokaPicture(character: Character, userId: number): Promise<void> {
const htmlImageElement = await character.getSnapshot(); const htmlImageElementSrc = await character.getSnapshot();
this.getUserWokaPictureStore(userId).picture.set(htmlImageElement); this.getUserWokaPictureStore(userId).picture.set(htmlImageElementSrc);
} }
pushPlayerPosition(event: HasPlayerMovedEvent) { pushPlayerPosition(event: HasPlayerMovedEvent) {

View File

@ -4,5 +4,5 @@ import { writable, Writable } from "svelte/store";
* A store that contains the player avatar picture * A store that contains the player avatar picture
*/ */
export class UserWokaPictureStore { export class UserWokaPictureStore {
constructor(public picture: Writable<HTMLImageElement | undefined> = writable(undefined)) {} constructor(public picture: Writable<string | undefined> = writable(undefined)) {}
} }