partey_workadventure/front/src/Phaser/Companion/Companion.ts

224 lines
7.3 KiB
TypeScript
Raw Normal View History

2021-04-01 00:33:05 +02:00
import Sprite = Phaser.GameObjects.Sprite;
import Container = Phaser.GameObjects.Container;
import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Animation";
2021-04-02 17:14:34 +02:00
export interface CompanionStatus {
x: number;
y: number;
name: string;
moving: boolean;
direction: PlayerAnimationDirections;
}
2021-04-01 00:33:05 +02:00
export class Companion extends Container {
public sprites: Map<string, Sprite>;
private delta: number;
private invisible: boolean;
2021-04-03 00:18:08 +02:00
private updateListener: Function;
2021-09-06 14:27:54 +02:00
private target: { x: number; y: number; direction: PlayerAnimationDirections };
2021-04-01 00:33:05 +02:00
2021-04-02 17:14:34 +02:00
private companionName: string;
private direction: PlayerAnimationDirections;
private animationType: PlayerAnimationTypes;
constructor(scene: Phaser.Scene, x: number, y: number, name: string, texturePromise: Promise<string>) {
2021-04-02 23:36:06 +02:00
super(scene, x + 14, y + 4);
2021-04-02 17:14:34 +02:00
this.sprites = new Map<string, Sprite>();
2021-04-01 00:33:05 +02:00
this.delta = 0;
this.invisible = true;
2021-04-01 18:51:51 +02:00
this.target = { x, y, direction: PlayerAnimationDirections.Down };
2021-04-01 00:33:05 +02:00
2021-04-02 17:14:34 +02:00
this.direction = PlayerAnimationDirections.Down;
this.animationType = PlayerAnimationTypes.Idle;
2021-04-02 21:21:11 +02:00
this.companionName = name;
2021-04-02 17:14:34 +02:00
2021-09-06 14:27:54 +02:00
texturePromise.then((resource) => {
this.addResource(resource);
this.invisible = false;
2021-09-06 14:27:54 +02:00
});
2021-04-01 00:33:05 +02:00
this.scene.physics.world.enableBody(this);
this.getBody().setImmovable(true);
2021-04-02 17:14:34 +02:00
this.getBody().setCollideWorldBounds(false);
2021-04-01 00:33:05 +02:00
this.setSize(16, 16);
this.getBody().setSize(16, 16);
this.getBody().setOffset(0, 8);
this.setDepth(-1);
2021-04-03 00:18:08 +02:00
this.updateListener = this.step.bind(this);
2021-09-06 14:27:54 +02:00
this.scene.events.addListener("update", this.updateListener);
2021-04-02 21:21:11 +02:00
this.scene.add.existing(this);
2021-04-01 00:33:05 +02:00
}
2021-04-01 18:51:51 +02:00
public setTarget(x: number, y: number, direction: PlayerAnimationDirections) {
2021-04-02 23:36:06 +02:00
this.target = { x, y: y + 4, direction };
2021-04-01 00:33:05 +02:00
}
2021-04-02 21:21:11 +02:00
public step(time: number, delta: number) {
2021-09-06 14:27:54 +02:00
if (typeof this.target === "undefined") return;
2021-04-01 00:33:05 +02:00
this.delta += delta;
2021-04-01 18:51:51 +02:00
if (this.delta < 128) {
2021-04-01 00:33:05 +02:00
return;
}
this.delta = 0;
const xDist = this.target.x - this.x;
const yDist = this.target.y - this.y;
2021-04-02 17:14:34 +02:00
const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2);
2021-04-01 00:33:05 +02:00
2021-04-02 23:36:06 +02:00
if (distance < 650) {
2021-04-02 17:14:34 +02:00
this.animationType = PlayerAnimationTypes.Idle;
this.direction = this.target.direction;
2021-04-01 18:51:51 +02:00
2021-04-01 00:33:05 +02:00
this.getBody().stop();
} else {
2021-04-02 17:14:34 +02:00
this.animationType = PlayerAnimationTypes.Walk;
2021-04-01 00:33:05 +02:00
const xDir = xDist / Math.max(Math.abs(xDist), 1);
const yDir = yDist / Math.max(Math.abs(yDist), 1);
const speed = 256;
2021-09-06 14:27:54 +02:00
this.getBody().setVelocity(
Math.min(Math.abs(xDist * 2.5), speed) * xDir,
Math.min(Math.abs(yDist * 2.5), speed) * yDir
);
2021-04-01 00:33:05 +02:00
2021-04-01 18:51:51 +02:00
if (Math.abs(xDist) > Math.abs(yDist)) {
if (xDist < 0) {
2021-04-02 17:14:34 +02:00
this.direction = PlayerAnimationDirections.Left;
2021-04-01 18:51:51 +02:00
} else {
2021-04-02 17:14:34 +02:00
this.direction = PlayerAnimationDirections.Right;
2021-04-01 18:51:51 +02:00
}
2021-04-01 00:33:05 +02:00
} else {
2021-04-01 18:51:51 +02:00
if (yDist < 0) {
2021-04-02 17:14:34 +02:00
this.direction = PlayerAnimationDirections.Up;
2021-04-01 18:51:51 +02:00
} else {
2021-04-02 17:14:34 +02:00
this.direction = PlayerAnimationDirections.Down;
2021-04-01 18:51:51 +02:00
}
2021-04-01 00:33:05 +02:00
}
}
2021-04-01 00:33:05 +02:00
this.setDepth(this.y);
2021-04-02 17:14:34 +02:00
this.playAnimation(this.direction, this.animationType);
}
public getStatus(): CompanionStatus {
const { x, y, direction, animationType, companionName } = this;
return {
x,
y,
direction,
moving: animationType === PlayerAnimationTypes.Walk,
2021-09-06 14:27:54 +02:00
name: companionName,
};
2021-04-01 00:33:05 +02:00
}
private playAnimation(direction: PlayerAnimationDirections, type: PlayerAnimationTypes): void {
if (this.invisible) return;
for (const [resource, sprite] of this.sprites.entries()) {
sprite.play(`${resource}-${direction}-${type}`, true);
}
}
private addResource(resource: string, frame?: string | number): void {
const sprite = new Sprite(this.scene, 0, 0, resource, frame);
this.add(sprite);
2021-09-06 14:27:54 +02:00
this.getAnimations(resource).forEach((animation) => {
2021-04-01 00:33:05 +02:00
this.scene.anims.create(animation);
});
2021-04-01 00:33:05 +02:00
this.scene.sys.updateList.add(sprite);
this.sprites.set(resource, sprite);
}
private getAnimations(resource: string): Phaser.Types.Animations.Animation[] {
return [
{
key: `${resource}-${PlayerAnimationDirections.Down}-${PlayerAnimationTypes.Idle}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [1] }),
2021-04-01 00:33:05 +02:00
frameRate: 10,
2021-09-06 14:27:54 +02:00
repeat: 1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Left}-${PlayerAnimationTypes.Idle}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [4] }),
2021-04-01 00:33:05 +02:00
frameRate: 10,
2021-09-06 14:27:54 +02:00
repeat: 1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Right}-${PlayerAnimationTypes.Idle}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [7] }),
2021-04-01 00:33:05 +02:00
frameRate: 10,
2021-09-06 14:27:54 +02:00
repeat: 1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Up}-${PlayerAnimationTypes.Idle}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [10] }),
2021-04-01 00:33:05 +02:00
frameRate: 10,
2021-09-06 14:27:54 +02:00
repeat: 1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Down}-${PlayerAnimationTypes.Walk}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [0, 1, 2] }),
2021-04-01 00:33:05 +02:00
frameRate: 15,
2021-09-06 14:27:54 +02:00
repeat: -1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Left}-${PlayerAnimationTypes.Walk}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [3, 4, 5] }),
2021-04-01 00:33:05 +02:00
frameRate: 15,
2021-09-06 14:27:54 +02:00
repeat: -1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Right}-${PlayerAnimationTypes.Walk}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [6, 7, 8] }),
2021-04-01 00:33:05 +02:00
frameRate: 15,
2021-09-06 14:27:54 +02:00
repeat: -1,
2021-04-01 00:33:05 +02:00
},
{
key: `${resource}-${PlayerAnimationDirections.Up}-${PlayerAnimationTypes.Walk}`,
2021-09-06 14:27:54 +02:00
frames: this.scene.anims.generateFrameNumbers(resource, { frames: [9, 10, 11] }),
2021-04-01 00:33:05 +02:00
frameRate: 15,
2021-09-06 14:27:54 +02:00
repeat: -1,
},
];
2021-04-01 00:33:05 +02:00
}
private getBody(): Phaser.Physics.Arcade.Body {
const body = this.body;
if (!(body instanceof Phaser.Physics.Arcade.Body)) {
2021-09-06 14:27:54 +02:00
throw new Error("Container does not have arcade body");
2021-04-01 00:33:05 +02:00
}
return body;
}
public destroy(): void {
for (const sprite of this.sprites.values()) {
if (this.scene) {
this.scene.sys.updateList.remove(sprite);
}
}
if (this.scene) {
2021-09-06 14:27:54 +02:00
this.scene.events.removeListener("update", this.updateListener);
}
2021-04-02 21:21:11 +02:00
2021-04-01 00:33:05 +02:00
super.destroy();
}
}