From 2ad712807ba13e9f32d974a284ba2961fb3b15ae Mon Sep 17 00:00:00 2001 From: Johannes Berthel Date: Fri, 2 Apr 2021 17:14:34 +0200 Subject: [PATCH] add companion only on local player --- front/src/Phaser/Companion/Companion.ts | 70 ++++++++++++++++--------- front/src/Phaser/Entity/Character.ts | 4 +- front/src/Phaser/Player/Player.ts | 7 +++ 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/front/src/Phaser/Companion/Companion.ts b/front/src/Phaser/Companion/Companion.ts index 065b3a8d..60e58995 100644 --- a/front/src/Phaser/Companion/Companion.ts +++ b/front/src/Phaser/Companion/Companion.ts @@ -3,14 +3,25 @@ import Container = Phaser.GameObjects.Container; import { lazyLoadResource } from "./CompanionTexturesLoadingManager"; import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Animation"; +export interface CompanionStatus { + x: number; + y: number; + name: string; + moving: boolean; + direction: PlayerAnimationDirections; +} + export class Companion extends Container { public sprites: Map; private delta: number; private invisible: boolean; - private stepListener: Function; private target: { x: number, y: number, direction: PlayerAnimationDirections }; + private companionName: string; + private direction: PlayerAnimationDirections; + private animationType: PlayerAnimationTypes; + constructor( scene: Phaser.Scene, x: number, @@ -18,15 +29,22 @@ export class Companion extends Container { ) { super(scene, x + 8, y + 8); + this.sprites = new Map(); + this.delta = 0; this.invisible = true; this.target = { x, y, direction: PlayerAnimationDirections.Down }; - this.sprites = new Map(); + this.direction = PlayerAnimationDirections.Down; + this.animationType = PlayerAnimationTypes.Idle; + + // select random animal const animal = ["dog1", "dog2", "dog3", "cat1", "cat2", "cat3"]; const random = Math.floor(Math.random() * animal.length); - lazyLoadResource(this.scene.load, animal[random]).then(resource => { + this.companionName = animal[random]; + + lazyLoadResource(this.scene.load, this.companionName).then(resource => { this.addResource(resource); this.invisible = false; }) @@ -34,16 +52,13 @@ export class Companion extends Container { this.scene.physics.world.enableBody(this); this.getBody().setImmovable(true); - this.getBody().setCollideWorldBounds(true); + this.getBody().setCollideWorldBounds(false); this.setSize(16, 16); this.getBody().setSize(16, 16); this.getBody().setOffset(0, 8); this.setDepth(-1); - this.stepListener = this.step.bind(this); - - scene.game.events.addListener('step', this.stepListener); scene.add.existing(this); } @@ -51,7 +66,7 @@ export class Companion extends Container { this.target = { x, y, direction }; } - private step(time: any, delta: any) { + public step(delta: number) { if (typeof this.target === 'undefined') return; this.delta += delta; @@ -63,18 +78,15 @@ export class Companion extends Container { const xDist = this.target.x - this.x; const yDist = this.target.y - this.y; - let direction: PlayerAnimationDirections; - let type: PlayerAnimationTypes; + const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2); - const distance = Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2)); - - if (distance < 16) { - type = PlayerAnimationTypes.Idle; - direction = this.target.direction; + if (distance < 576) { // 24^2 + this.animationType = PlayerAnimationTypes.Idle; + this.direction = this.target.direction; this.getBody().stop(); } else { - type = PlayerAnimationTypes.Walk; + this.animationType = PlayerAnimationTypes.Walk; const xDir = xDist / Math.max(Math.abs(xDist), 1); const yDir = yDist / Math.max(Math.abs(yDist), 1); @@ -84,21 +96,33 @@ export class Companion extends Container { if (Math.abs(xDist) > Math.abs(yDist)) { if (xDist < 0) { - direction = PlayerAnimationDirections.Left; + this.direction = PlayerAnimationDirections.Left; } else { - direction = PlayerAnimationDirections.Right; + this.direction = PlayerAnimationDirections.Right; } } else { if (yDist < 0) { - direction = PlayerAnimationDirections.Up; + this.direction = PlayerAnimationDirections.Up; } else { - direction = PlayerAnimationDirections.Down; + this.direction = PlayerAnimationDirections.Down; } } } this.setDepth(this.y); - this.playAnimation(direction, type); + this.playAnimation(this.direction, this.animationType); + } + + public getStatus(): CompanionStatus { + const { x, y, direction, animationType, companionName } = this; + + return { + x, + y, + direction, + moving: animationType === PlayerAnimationTypes.Walk, + name: companionName + } } private playAnimation(direction: PlayerAnimationDirections, type: PlayerAnimationTypes): void { @@ -192,10 +216,6 @@ export class Companion extends Container { } } - if (this.scene) { - this.scene.game.events.removeListener('step', this.stepListener); - } - super.destroy(); } } diff --git a/front/src/Phaser/Entity/Character.ts b/front/src/Phaser/Entity/Character.ts index 352494f8..f329192c 100644 --- a/front/src/Phaser/Entity/Character.ts +++ b/front/src/Phaser/Entity/Character.ts @@ -69,11 +69,9 @@ export abstract class Character extends Container { this.setDepth(-1); this.playAnimation(direction, moving); - - this.addCompanion(); } - private addCompanion(): void { + public addCompanion(): void { this.companion = new Companion(this.scene, this.x, this.y); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ac8942a5..af63ddb3 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -27,6 +27,8 @@ export class Player extends Character implements CurrentGamerInterface { //the current player model should be push away by other players to prevent conflict this.getBody().setImmovable(false); + + this.addCompanion(); } moveUser(delta: number): void { @@ -58,6 +60,11 @@ export class Player extends Character implements CurrentGamerInterface { direction = PlayerAnimationDirections.Right; moving = true; } + + if (this.companion) { + this.companion.step(delta); + } + if (x !== 0 || y !== 0) { this.move(x, y); this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});