add companion only on local player

This commit is contained in:
Johannes Berthel 2021-04-02 17:14:34 +02:00
parent 5a91e15580
commit 2ad712807b
3 changed files with 53 additions and 28 deletions

View File

@ -3,14 +3,25 @@ import Container = Phaser.GameObjects.Container;
import { lazyLoadResource } from "./CompanionTexturesLoadingManager"; import { lazyLoadResource } from "./CompanionTexturesLoadingManager";
import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Animation"; import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Animation";
export interface CompanionStatus {
x: number;
y: number;
name: string;
moving: boolean;
direction: PlayerAnimationDirections;
}
export class Companion extends Container { export class Companion extends Container {
public sprites: Map<string, Sprite>; public sprites: Map<string, Sprite>;
private delta: number; private delta: number;
private invisible: boolean; private invisible: boolean;
private stepListener: Function;
private target: { x: number, y: number, direction: PlayerAnimationDirections }; private target: { x: number, y: number, direction: PlayerAnimationDirections };
private companionName: string;
private direction: PlayerAnimationDirections;
private animationType: PlayerAnimationTypes;
constructor( constructor(
scene: Phaser.Scene, scene: Phaser.Scene,
x: number, x: number,
@ -18,15 +29,22 @@ export class Companion extends Container {
) { ) {
super(scene, x + 8, y + 8); super(scene, x + 8, y + 8);
this.sprites = new Map<string, Sprite>();
this.delta = 0; this.delta = 0;
this.invisible = true; this.invisible = true;
this.target = { x, y, direction: PlayerAnimationDirections.Down }; this.target = { x, y, direction: PlayerAnimationDirections.Down };
this.sprites = new Map<string, Sprite>();
this.direction = PlayerAnimationDirections.Down;
this.animationType = PlayerAnimationTypes.Idle;
// select random animal
const animal = ["dog1", "dog2", "dog3", "cat1", "cat2", "cat3"]; const animal = ["dog1", "dog2", "dog3", "cat1", "cat2", "cat3"];
const random = Math.floor(Math.random() * animal.length); 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.addResource(resource);
this.invisible = false; this.invisible = false;
}) })
@ -34,16 +52,13 @@ export class Companion extends Container {
this.scene.physics.world.enableBody(this); this.scene.physics.world.enableBody(this);
this.getBody().setImmovable(true); this.getBody().setImmovable(true);
this.getBody().setCollideWorldBounds(true); this.getBody().setCollideWorldBounds(false);
this.setSize(16, 16); this.setSize(16, 16);
this.getBody().setSize(16, 16); this.getBody().setSize(16, 16);
this.getBody().setOffset(0, 8); this.getBody().setOffset(0, 8);
this.setDepth(-1); this.setDepth(-1);
this.stepListener = this.step.bind(this);
scene.game.events.addListener('step', this.stepListener);
scene.add.existing(this); scene.add.existing(this);
} }
@ -51,7 +66,7 @@ export class Companion extends Container {
this.target = { x, y, direction }; this.target = { x, y, direction };
} }
private step(time: any, delta: any) { public step(delta: number) {
if (typeof this.target === 'undefined') return; if (typeof this.target === 'undefined') return;
this.delta += delta; this.delta += delta;
@ -63,18 +78,15 @@ export class Companion extends Container {
const xDist = this.target.x - this.x; const xDist = this.target.x - this.x;
const yDist = this.target.y - this.y; const yDist = this.target.y - this.y;
let direction: PlayerAnimationDirections; const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2);
let type: PlayerAnimationTypes;
const distance = Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2)); if (distance < 576) { // 24^2
this.animationType = PlayerAnimationTypes.Idle;
if (distance < 16) { this.direction = this.target.direction;
type = PlayerAnimationTypes.Idle;
direction = this.target.direction;
this.getBody().stop(); this.getBody().stop();
} else { } else {
type = PlayerAnimationTypes.Walk; this.animationType = PlayerAnimationTypes.Walk;
const xDir = xDist / Math.max(Math.abs(xDist), 1); const xDir = xDist / Math.max(Math.abs(xDist), 1);
const yDir = yDist / Math.max(Math.abs(yDist), 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 (Math.abs(xDist) > Math.abs(yDist)) {
if (xDist < 0) { if (xDist < 0) {
direction = PlayerAnimationDirections.Left; this.direction = PlayerAnimationDirections.Left;
} else { } else {
direction = PlayerAnimationDirections.Right; this.direction = PlayerAnimationDirections.Right;
} }
} else { } else {
if (yDist < 0) { if (yDist < 0) {
direction = PlayerAnimationDirections.Up; this.direction = PlayerAnimationDirections.Up;
} else { } else {
direction = PlayerAnimationDirections.Down; this.direction = PlayerAnimationDirections.Down;
} }
} }
} }
this.setDepth(this.y); 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 { 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(); super.destroy();
} }
} }

View File

@ -69,11 +69,9 @@ export abstract class Character extends Container {
this.setDepth(-1); this.setDepth(-1);
this.playAnimation(direction, moving); this.playAnimation(direction, moving);
this.addCompanion();
} }
private addCompanion(): void { public addCompanion(): void {
this.companion = new Companion(this.scene, this.x, this.y); this.companion = new Companion(this.scene, this.x, this.y);
} }

View File

@ -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 //the current player model should be push away by other players to prevent conflict
this.getBody().setImmovable(false); this.getBody().setImmovable(false);
this.addCompanion();
} }
moveUser(delta: number): void { moveUser(delta: number): void {
@ -58,6 +60,11 @@ export class Player extends Character implements CurrentGamerInterface {
direction = PlayerAnimationDirections.Right; direction = PlayerAnimationDirections.Right;
moving = true; moving = true;
} }
if (this.companion) {
this.companion.step(delta);
}
if (x !== 0 || y !== 0) { if (x !== 0 || y !== 0) {
this.move(x, y); this.move(x, y);
this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y}); this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});