2022-01-26 10:40:13 +01:00
import { requestVisitCardsStore } from "../../Stores/GameStore" ;
2022-02-02 13:30:49 +01:00
import { ActionsMenuData , actionsMenuStore } from "../../Stores/ActionsMenuStore" ;
2022-01-25 17:13:52 +01:00
import { Character } from "../Entity/Character" ;
2021-09-01 10:11:12 +02:00
import type { GameScene } from "../Game/GameScene" ;
import type { PointInterface } from "../../Connexion/ConnexionModels" ;
import type { PlayerAnimationDirections } from "../Player/Animation" ;
2022-02-02 13:30:49 +01:00
import type { Unsubscriber } from "svelte/store" ;
import type { ActivatableInterface } from "../Game/ActivatableInterface" ;
2022-02-01 11:44:39 +01:00
import type CancelablePromise from "cancelable-promise" ;
2021-06-01 15:35:25 +02:00
2020-06-04 18:54:34 +02:00
/ * *
* Class representing the sprite of a remote player ( a player that plays on another computer )
* /
2022-01-26 12:57:10 +01:00
export class RemotePlayer extends Character implements ActivatableInterface {
public userId : number ;
public readonly activationRadius : number ;
2022-02-02 13:30:49 +01:00
private registeredActions : { actionName : string ; callback : Function } [ ] ;
2021-09-01 10:11:12 +02:00
private visitCardUrl : string | null ;
2022-01-26 10:40:13 +01:00
private isActionsMenuInitialized : boolean = false ;
private actionsMenuStoreUnsubscriber : Unsubscriber ;
2022-01-25 15:47:21 +01:00
2020-06-04 18:54:34 +02:00
constructor (
2020-09-18 13:57:38 +02:00
userId : number ,
2020-06-04 18:54:34 +02:00
Scene : GameScene ,
x : number ,
y : number ,
name : string ,
2022-02-01 11:44:39 +01:00
texturesPromise : CancelablePromise < string [ ] > ,
2021-03-11 16:13:05 +01:00
direction : PlayerAnimationDirections ,
2021-04-02 21:21:11 +02:00
moving : boolean ,
2021-09-01 10:11:12 +02:00
visitCardUrl : string | null ,
companion : string | null ,
2022-02-02 11:10:52 +01:00
companionTexturePromise? : CancelablePromise < string > ,
2022-02-02 13:30:49 +01:00
activationRadius? : number
2020-06-04 18:54:34 +02:00
) {
2022-02-02 13:30:49 +01:00
super ( Scene , x , y , texturesPromise , name , direction , moving , 1 , true , companion , companionTexturePromise ) ;
2021-09-01 10:11:12 +02:00
2020-06-04 18:54:34 +02:00
//set data
this . userId = userId ;
2022-02-03 09:50:52 +01:00
this . visitCardUrl = visitCardUrl ;
2022-01-31 12:26:37 +01:00
this . registeredActions = [ ] ;
this . registerDefaultActionsMenuActions ( ) ;
this . setClickable ( this . registeredActions . length > 0 ) ;
2022-01-26 12:57:10 +01:00
this . activationRadius = activationRadius ? ? 96 ;
2022-01-26 10:40:13 +01:00
this . actionsMenuStoreUnsubscriber = actionsMenuStore . subscribe ( ( value : ActionsMenuData | undefined ) = > {
this . isActionsMenuInitialized = value ? true : false ;
2022-01-25 15:47:21 +01:00
} ) ;
2021-09-01 10:11:12 +02:00
2022-01-26 12:57:10 +01:00
this . bindEventHandlers ( ) ;
2020-06-04 18:54:34 +02:00
}
2022-01-25 17:13:52 +01:00
public updatePosition ( position : PointInterface ) : void {
2021-03-11 16:13:05 +01:00
this . playAnimation ( position . direction as PlayerAnimationDirections , position . moving ) ;
2020-06-04 18:54:34 +02:00
this . setX ( position . x ) ;
this . setY ( position . y ) ;
2021-05-12 09:13:25 +02:00
2021-01-06 15:00:54 +01:00
this . setDepth ( position . y ) ; //this is to make sure the perspective (player models closer the bottom of the screen will appear in front of models nearer the top of the screen).
2021-05-12 09:13:25 +02:00
2021-04-01 18:51:51 +02:00
if ( this . companion ) {
this . companion . setTarget ( position . x , position . y , position . direction as PlayerAnimationDirections ) ;
}
2020-06-04 18:54:34 +02:00
}
2022-01-25 17:13:52 +01:00
2022-02-02 13:30:49 +01:00
public registerActionsMenuAction ( action : { actionName : string ; callback : Function } ) : void {
2022-01-31 12:26:37 +01:00
this . registeredActions . push ( action ) ;
this . updateIsClickable ( ) ;
}
public unregisterActionsMenuAction ( actionName : string ) {
2022-02-02 13:30:49 +01:00
const index = this . registeredActions . findIndex ( ( action ) = > action . actionName === actionName ) ;
2022-01-31 12:26:37 +01:00
if ( index !== - 1 ) {
this . registeredActions . splice ( index , 1 ) ;
}
this . updateIsClickable ( ) ;
}
2022-01-26 12:57:10 +01:00
public activate ( ) : void {
this . toggleActionsMenu ( ) ;
}
2022-01-25 17:13:52 +01:00
public destroy ( ) : void {
2022-01-26 10:40:13 +01:00
this . actionsMenuStoreUnsubscriber ( ) ;
actionsMenuStore . clear ( ) ;
2022-01-25 17:13:52 +01:00
super . destroy ( ) ;
}
2022-01-26 10:54:51 +01:00
2022-01-31 13:00:14 +01:00
public isActivatable ( ) : boolean {
return this . isClickable ( ) ;
}
2022-01-31 12:26:37 +01:00
private updateIsClickable ( ) : void {
this . setClickable ( this . registeredActions . length > 0 ) ;
}
2022-01-26 12:57:10 +01:00
private toggleActionsMenu ( ) : void {
if ( this . isActionsMenuInitialized ) {
actionsMenuStore . clear ( ) ;
return ;
}
actionsMenuStore . initialize ( this . playerName ) ;
2022-01-31 12:26:37 +01:00
for ( const action of this . registeredActions ) {
2022-01-26 12:57:10 +01:00
actionsMenuStore . addAction ( action . actionName , action . callback ) ;
}
}
2022-01-31 12:26:37 +01:00
private registerDefaultActionsMenuActions ( ) : void {
if ( this . visitCardUrl ) {
this . registeredActions . push ( {
2022-01-26 10:54:51 +01:00
actionName : "Visiting Card" ,
callback : ( ) = > {
requestVisitCardsStore . set ( this . visitCardUrl ) ;
actionsMenuStore . clear ( ) ;
2022-02-02 13:30:49 +01:00
} ,
2022-01-31 12:26:37 +01:00
} ) ;
}
2022-01-26 10:54:51 +01:00
}
2022-01-26 12:57:10 +01:00
private bindEventHandlers ( ) : void {
this . on ( Phaser . Input . Events . POINTER_DOWN , ( event : Phaser.Input.Pointer ) = > {
2022-02-03 09:50:52 +01:00
if ( event . downElement . nodeName === "CANVAS" && event . leftButtonDown ( ) ) {
2022-01-26 12:57:10 +01:00
this . toggleActionsMenu ( ) ;
}
} ) ;
}
2020-06-04 18:54:34 +02:00
}