some improvements with registering menu actions

This commit is contained in:
Hanusiak Piotr 2022-01-31 12:26:37 +01:00
parent 5aba99403e
commit 2322f5f76d
6 changed files with 289 additions and 14 deletions

View file

@ -134,6 +134,18 @@ export abstract class Character extends Container implements OutlineableInterfac
}
}
public setClickable(clickable: boolean = true): void {
if (clickable) {
this.setInteractive({
hitArea: new Phaser.Geom.Circle(0, 0, interactiveRadius),
hitAreaCallback: Phaser.Geom.Circle.Contains, //eslint-disable-line @typescript-eslint/unbound-method
useHandCursor: true,
});
return;
}
this.disableInteractive();
}
public getPosition(): { x: number, y: number } {
return { x: this.x, y: this.y };
}

View file

@ -16,6 +16,7 @@ export class RemotePlayer extends Character implements ActivatableInterface {
public userId: number;
public readonly activationRadius: number;
private registeredActions: { actionName: string, callback: Function }[];
private visitCardUrl: string | null;
private isActionsMenuInitialized: boolean = false;
private actionsMenuStoreUnsubscriber: Unsubscriber;
@ -43,13 +44,16 @@ export class RemotePlayer extends Character implements ActivatableInterface {
direction,
moving,
1,
!!visitCardUrl,
true,
companion,
companionTexturePromise
);
//set data
this.userId = userId;
this.registeredActions = [];
this.registerDefaultActionsMenuActions();
this.setClickable(this.registeredActions.length > 0);
this.activationRadius = activationRadius ?? 96;
this.visitCardUrl = visitCardUrl;
this.actionsMenuStoreUnsubscriber = actionsMenuStore.subscribe((value: ActionsMenuData | undefined) => {
@ -71,6 +75,19 @@ export class RemotePlayer extends Character implements ActivatableInterface {
}
}
public registerActionsMenuAction(action: { actionName: string, callback: Function }): void {
this.registeredActions.push(action);
this.updateIsClickable();
}
public unregisterActionsMenuAction(actionName: string) {
const index = this.registeredActions.findIndex(action => action.actionName === actionName);
if (index !== -1) {
this.registeredActions.splice(index, 1);
}
this.updateIsClickable();
}
public activate(): void {
this.toggleActionsMenu();
}
@ -81,33 +98,31 @@ export class RemotePlayer extends Character implements ActivatableInterface {
super.destroy();
}
private updateIsClickable(): void {
this.setClickable(this.registeredActions.length > 0);
}
private toggleActionsMenu(): void {
if (this.isActionsMenuInitialized) {
actionsMenuStore.clear();
return;
}
actionsMenuStore.initialize(this.playerName);
for (const action of this.getActionsMenuActions()) {
for (const action of this.registeredActions) {
actionsMenuStore.addAction(action.actionName, action.callback);
}
}
private getActionsMenuActions(): { actionName: string, callback: Function }[] {
return [
{
private registerDefaultActionsMenuActions(): void {
if (this.visitCardUrl) {
this.registeredActions.push({
actionName: "Visiting Card",
callback: () => {
requestVisitCardsStore.set(this.visitCardUrl);
actionsMenuStore.clear();
}
},
{
actionName: "Log Hello",
callback: () => {
console.log('HELLO');
}
},
];
});
}
}
private bindEventHandlers(): void {

View file

@ -26,7 +26,6 @@ function createActionsMenuStore() {
removeAction: (actionName: string) => {
update((data) => {
const actionIndex = data?.actions.findIndex(action => action.actionName === actionName);
console.log(actionIndex);
if (actionIndex !== undefined && actionIndex != -1) {
data?.actions.splice(actionIndex, 1);
}