This commit is contained in:
Ludwig Behm 2022-02-04 00:41:26 +01:00
commit 0cd0f5acc0
Signed by: l.behm
GPG key ID: D344835D63B89384
10 changed files with 90 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Anima
import { TexturesHelper } from "../Helpers/TexturesHelper";
import { Writable, writable } from "svelte/store";
import type { PictureStore } from "../../Stores/PictureStore";
import type CancelablePromise from "cancelable-promise";
export interface CompanionStatus {
x: number;
@ -25,8 +26,9 @@ export class Companion extends Container {
private direction: PlayerAnimationDirections;
private animationType: PlayerAnimationTypes;
private readonly _pictureStore: Writable<string | undefined>;
private texturePromise: CancelablePromise<string | void> | undefined;
constructor(scene: Phaser.Scene, x: number, y: number, name: string, texturePromise: Promise<string>) {
constructor(scene: Phaser.Scene, x: number, y: number, name: string, texturePromise: CancelablePromise<string>) {
super(scene, x + 14, y + 4);
this.sprites = new Map<string, Sprite>();
@ -41,7 +43,7 @@ export class Companion extends Container {
this.companionName = name;
this._pictureStore = writable(undefined);
texturePromise
this.texturePromise = texturePromise
.then((resource) => {
this.addResource(resource);
this.invisible = false;
@ -234,6 +236,7 @@ export class Companion extends Container {
}
public destroy(): void {
this.texturePromise?.cancel();
for (const sprite of this.sprites.values()) {
if (this.scene) {
this.scene.sys.updateList.remove(sprite);

View file

@ -1,5 +1,6 @@
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
import { COMPANION_RESOURCES, CompanionResourceDescriptionInterface } from "./CompanionTextures";
import CancelablePromise from "cancelable-promise";
export const getAllCompanionResources = (loader: LoaderPlugin): CompanionResourceDescriptionInterface[] => {
COMPANION_RESOURCES.forEach((resource: CompanionResourceDescriptionInterface) => {
@ -9,8 +10,12 @@ export const getAllCompanionResources = (loader: LoaderPlugin): CompanionResourc
return COMPANION_RESOURCES;
};
export const lazyLoadCompanionResource = (loader: LoaderPlugin, name: string): Promise<string> => {
return new Promise((resolve, reject) => {
export const lazyLoadCompanionResource = (loader: LoaderPlugin, name: string): CancelablePromise<string> => {
return new CancelablePromise((resolve, reject, cancel) => {
cancel(() => {
return;
});
const resource = COMPANION_RESOURCES.find((item) => item.name === name);
if (typeof resource === "undefined") {

View file

@ -59,7 +59,7 @@ export abstract class Character extends Container implements OutlineableInterfac
frame: string | number,
isClickable: boolean,
companion: string | null,
companionTexturePromise?: Promise<string>
companionTexturePromise?: CancelablePromise<string>
) {
super(scene, x, y /*, texture, frame*/);
this.scene = scene;
@ -142,7 +142,7 @@ export abstract class Character extends Container implements OutlineableInterfac
this.clickable = clickable;
if (clickable) {
this.setInteractive({
hitArea: new Phaser.Geom.Circle(0, 0, interactiveRadius),
hitArea: new Phaser.Geom.Circle(8, 8, interactiveRadius),
hitAreaCallback: Phaser.Geom.Circle.Contains, //eslint-disable-line @typescript-eslint/unbound-method
useHandCursor: true,
});
@ -179,7 +179,7 @@ export abstract class Character extends Container implements OutlineableInterfac
});
}
public addCompanion(name: string, texturePromise?: Promise<string>): void {
public addCompanion(name: string, texturePromise?: CancelablePromise<string>): void {
if (typeof texturePromise !== "undefined") {
this.companion = new Companion(this.scene, this.x, this.y, name, texturePromise);
}

View file

@ -31,18 +31,18 @@ export class RemotePlayer extends Character implements ActivatableInterface {
moving: boolean,
visitCardUrl: string | null,
companion: string | null,
companionTexturePromise?: Promise<string>,
companionTexturePromise?: CancelablePromise<string>,
activationRadius?: number
) {
super(Scene, x, y, texturesPromise, name, direction, moving, 1, true, companion, companionTexturePromise);
//set data
this.userId = userId;
this.visitCardUrl = visitCardUrl;
this.registeredActions = [];
this.registerDefaultActionsMenuActions();
this.setClickable(this.registeredActions.length > 0);
this.activationRadius = activationRadius ?? 96;
this.visitCardUrl = visitCardUrl;
this.actionsMenuStoreUnsubscriber = actionsMenuStore.subscribe((value: ActionsMenuData | undefined) => {
this.isActionsMenuInitialized = value ? true : false;
});
@ -118,7 +118,7 @@ export class RemotePlayer extends Character implements ActivatableInterface {
private bindEventHandlers(): void {
this.on(Phaser.Input.Events.POINTER_DOWN, (event: Phaser.Input.Pointer) => {
if (event.downElement.nodeName === "CANVAS") {
if (event.downElement.nodeName === "CANVAS" && event.leftButtonDown()) {
this.toggleActionsMenu();
}
});

View file

@ -25,7 +25,7 @@ export class Player extends Character {
direction: PlayerAnimationDirections,
moving: boolean,
companion: string | null,
companionTexturePromise?: Promise<string>
companionTexturePromise?: CancelablePromise<string>
) {
super(Scene, x, y, texturesPromise, name, direction, moving, 1, true, companion, companionTexturePromise);