2021-03-11 16:13:05 +01:00
|
|
|
import {PlayerAnimationDirections, PlayerAnimationTypes} from "../Player/Animation";
|
2020-04-12 19:35:51 +02:00
|
|
|
import {SpeechBubble} from "./SpeechBubble";
|
2021-05-26 20:31:33 +02:00
|
|
|
import Text = Phaser.GameObjects.Text;
|
2020-07-28 17:43:33 +02:00
|
|
|
import Container = Phaser.GameObjects.Container;
|
|
|
|
import Sprite = Phaser.GameObjects.Sprite;
|
2020-12-18 14:30:46 +01:00
|
|
|
import {TextureError} from "../../Exception/TextureError";
|
2021-04-01 18:51:51 +02:00
|
|
|
import {Companion} from "../Companion/Companion";
|
2021-05-19 18:08:53 +02:00
|
|
|
import type {GameScene} from "../Game/GameScene";
|
2021-05-10 17:10:41 +02:00
|
|
|
import {DEPTH_INGAME_TEXT_INDEX} from "../Game/DepthIndexes";
|
2021-05-19 18:08:53 +02:00
|
|
|
import {waScaleManager} from "../Services/WaScaleManager";
|
2021-03-31 11:21:06 +02:00
|
|
|
|
|
|
|
const playerNameY = - 25;
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2020-06-04 18:54:34 +02:00
|
|
|
interface AnimationData {
|
|
|
|
key: string;
|
|
|
|
frameRate: number;
|
|
|
|
repeat: number;
|
|
|
|
frameModel: string; //todo use an enum
|
2021-03-11 16:13:05 +01:00
|
|
|
frames : number[]
|
2020-06-04 18:54:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:08:53 +02:00
|
|
|
const interactiveRadius = 35;
|
2021-05-10 17:10:41 +02:00
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
export abstract class Character extends Container {
|
2020-06-04 18:11:07 +02:00
|
|
|
private bubble: SpeechBubble|null = null;
|
2021-05-26 20:31:33 +02:00
|
|
|
private readonly playerName: Text;
|
2020-05-06 01:50:01 +02:00
|
|
|
public PlayerValue: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
public sprites: Map<string, Sprite>;
|
2021-03-11 16:13:05 +01:00
|
|
|
private lastDirection: PlayerAnimationDirections = PlayerAnimationDirections.Down;
|
2020-10-12 11:22:41 +02:00
|
|
|
//private teleportation: Sprite;
|
2021-01-07 17:11:22 +01:00
|
|
|
private invisible: boolean;
|
2021-04-01 18:51:51 +02:00
|
|
|
public companion?: Companion;
|
2021-03-31 11:21:06 +02:00
|
|
|
private emote: Phaser.GameObjects.Sprite | null = null;
|
2021-05-21 16:25:12 +02:00
|
|
|
private emoteTween: Phaser.Tweens.Tween|null = null;
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2021-05-10 17:10:41 +02:00
|
|
|
constructor(scene: GameScene,
|
2020-06-04 18:54:34 +02:00
|
|
|
x: number,
|
|
|
|
y: number,
|
2021-01-07 17:11:22 +01:00
|
|
|
texturesPromise: Promise<string[]>,
|
2020-06-04 18:54:34 +02:00
|
|
|
name: string,
|
2021-03-11 16:13:05 +01:00
|
|
|
direction: PlayerAnimationDirections,
|
2020-06-04 18:54:34 +02:00
|
|
|
moving: boolean,
|
2021-05-10 17:10:41 +02:00
|
|
|
frame: string | number,
|
|
|
|
companion: string|null,
|
|
|
|
companionTexturePromise?: Promise<string>
|
2020-06-04 18:54:34 +02:00
|
|
|
) {
|
2020-07-28 17:43:33 +02:00
|
|
|
super(scene, x, y/*, texture, frame*/);
|
2021-01-07 12:43:21 +01:00
|
|
|
this.PlayerValue = name;
|
2021-01-07 17:11:22 +01:00
|
|
|
this.invisible = true
|
2020-07-28 17:43:33 +02:00
|
|
|
|
|
|
|
this.sprites = new Map<string, Sprite>();
|
|
|
|
|
2021-01-07 17:11:22 +01:00
|
|
|
//textures are inside a Promise in case they need to be lazyloaded before use.
|
|
|
|
texturesPromise.then((textures) => {
|
|
|
|
this.addTextures(textures, frame);
|
|
|
|
this.invisible = false
|
|
|
|
})
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2021-05-26 20:31:33 +02:00
|
|
|
this.playerName = new Text(scene, 0, playerNameY, name, {fontFamily: '"Press Start 2P"', fontSize: '8px', strokeThickness: 2, stroke: "#000"});
|
|
|
|
this.playerName.setOrigin(0.5).setDepth(DEPTH_INGAME_TEXT_INDEX);
|
2021-01-07 12:43:21 +01:00
|
|
|
this.add(this.playerName);
|
2020-05-01 23:38:09 +02:00
|
|
|
|
2021-05-10 17:10:41 +02:00
|
|
|
if (this.isClickable()) {
|
|
|
|
this.setInteractive({
|
|
|
|
hitArea: new Phaser.Geom.Circle(0, 0, interactiveRadius),
|
|
|
|
hitAreaCallback: Phaser.Geom.Circle.Contains, //eslint-disable-line @typescript-eslint/unbound-method
|
|
|
|
useHandCursor: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
scene.add.existing(this);
|
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
this.scene.physics.world.enableBody(this);
|
2020-07-28 17:43:33 +02:00
|
|
|
this.getBody().setImmovable(true);
|
|
|
|
this.getBody().setCollideWorldBounds(true);
|
|
|
|
this.setSize(16, 16);
|
|
|
|
this.getBody().setSize(16, 16); //edit the hitbox to better match the character model
|
|
|
|
this.getBody().setOffset(0, 8);
|
2020-05-08 17:35:40 +02:00
|
|
|
this.setDepth(-1);
|
2020-05-12 11:49:55 +02:00
|
|
|
|
2020-06-04 18:54:34 +02:00
|
|
|
this.playAnimation(direction, moving);
|
2021-05-10 17:10:41 +02:00
|
|
|
|
|
|
|
if (typeof companion === 'string') {
|
|
|
|
this.addCompanion(companion, companionTexturePromise);
|
|
|
|
}
|
2021-04-01 18:51:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 19:10:18 +02:00
|
|
|
public addCompanion(name: string, texturePromise?: Promise<string>): void {
|
|
|
|
if (typeof texturePromise !== 'undefined') {
|
|
|
|
this.companion = new Companion(this.scene, this.x, this.y, name, texturePromise);
|
|
|
|
}
|
2020-06-04 18:54:34 +02:00
|
|
|
}
|
2021-05-10 17:10:41 +02:00
|
|
|
|
|
|
|
public abstract isClickable(): boolean;
|
2020-06-04 18:54:34 +02:00
|
|
|
|
2020-10-20 16:39:23 +02:00
|
|
|
public addTextures(textures: string[], frame?: string | number): void {
|
|
|
|
for (const texture of textures) {
|
2020-12-18 14:30:46 +01:00
|
|
|
if(!this.scene.textures.exists(texture)){
|
|
|
|
throw new TextureError('texture not found');
|
|
|
|
}
|
2020-10-20 16:39:23 +02:00
|
|
|
const sprite = new Sprite(this.scene, 0, 0, texture, frame);
|
|
|
|
this.add(sprite);
|
|
|
|
this.getPlayerAnimations(texture).forEach(d => {
|
|
|
|
this.scene.anims.create({
|
|
|
|
key: d.key,
|
2021-03-11 16:13:05 +01:00
|
|
|
frames: this.scene.anims.generateFrameNumbers(d.frameModel, {frames: d.frames}),
|
2020-10-20 16:39:23 +02:00
|
|
|
frameRate: d.frameRate,
|
|
|
|
repeat: d.repeat
|
|
|
|
});
|
|
|
|
})
|
|
|
|
// Needed, otherwise, animations are not handled correctly.
|
2020-12-18 13:56:25 +01:00
|
|
|
if(this.scene) {
|
|
|
|
this.scene.sys.updateList.add(sprite);
|
|
|
|
}
|
2020-10-20 16:39:23 +02:00
|
|
|
this.sprites.set(texture, sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:54:34 +02:00
|
|
|
private getPlayerAnimations(name: string): AnimationData[] {
|
|
|
|
return [{
|
2021-03-11 16:13:05 +01:00
|
|
|
key: `${name}-${PlayerAnimationDirections.Down}-${PlayerAnimationTypes.Walk}`,
|
2020-06-04 18:54:34 +02:00
|
|
|
frameModel: name,
|
2021-03-11 16:13:05 +01:00
|
|
|
frames: [0, 1, 2, 1],
|
2020-06-04 18:54:34 +02:00
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
2021-03-11 16:13:05 +01:00
|
|
|
key: `${name}-${PlayerAnimationDirections.Left}-${PlayerAnimationTypes.Walk}`,
|
2020-06-04 18:54:34 +02:00
|
|
|
frameModel: name,
|
2021-03-11 16:13:05 +01:00
|
|
|
frames: [3, 4, 5, 4],
|
2020-06-04 18:54:34 +02:00
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
2021-03-11 16:13:05 +01:00
|
|
|
key: `${name}-${PlayerAnimationDirections.Right}-${PlayerAnimationTypes.Walk}`,
|
2020-06-04 18:54:34 +02:00
|
|
|
frameModel: name,
|
2021-03-11 16:13:05 +01:00
|
|
|
frames: [6, 7, 8, 7],
|
2020-06-04 18:54:34 +02:00
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
2021-03-11 16:13:05 +01:00
|
|
|
key: `${name}-${PlayerAnimationDirections.Up}-${PlayerAnimationTypes.Walk}`,
|
2020-06-04 18:54:34 +02:00
|
|
|
frameModel: name,
|
2021-03-11 16:13:05 +01:00
|
|
|
frames: [9, 10, 11, 10],
|
2020-06-04 18:54:34 +02:00
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
2021-03-11 16:13:05 +01:00
|
|
|
},{
|
|
|
|
key: `${name}-${PlayerAnimationDirections.Down}-${PlayerAnimationTypes.Idle}`,
|
|
|
|
frameModel: name,
|
|
|
|
frames: [1],
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: 1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationDirections.Left}-${PlayerAnimationTypes.Idle}`,
|
|
|
|
frameModel: name,
|
|
|
|
frames: [4],
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: 1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationDirections.Right}-${PlayerAnimationTypes.Idle}`,
|
|
|
|
frameModel: name,
|
|
|
|
frames: [7],
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: 1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationDirections.Up}-${PlayerAnimationTypes.Idle}`,
|
|
|
|
frameModel: name,
|
|
|
|
frames: [10],
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: 1
|
2020-06-04 18:54:34 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2021-03-11 16:13:05 +01:00
|
|
|
protected playAnimation(direction : PlayerAnimationDirections, moving: boolean): void {
|
2021-01-07 17:11:22 +01:00
|
|
|
if (this.invisible) return;
|
2020-07-28 17:43:33 +02:00
|
|
|
for (const [texture, sprite] of this.sprites.entries()) {
|
|
|
|
if (!sprite.anims) {
|
|
|
|
console.error('ANIMS IS NOT DEFINED!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (moving && (!sprite.anims.currentAnim || sprite.anims.currentAnim.key !== direction)) {
|
2021-03-11 16:13:05 +01:00
|
|
|
sprite.play(texture+'-'+direction+'-'+PlayerAnimationTypes.Walk, true);
|
2020-07-28 17:43:33 +02:00
|
|
|
} else if (!moving) {
|
2021-03-11 16:13:05 +01:00
|
|
|
sprite.anims.play(texture + '-' + direction + '-'+PlayerAnimationTypes.Idle, true);
|
2020-07-28 17:43:33 +02:00
|
|
|
}
|
2020-06-10 15:43:22 +02:00
|
|
|
}
|
2020-07-28 17:43:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected getBody(): Phaser.Physics.Arcade.Body {
|
|
|
|
const body = this.body;
|
|
|
|
if (!(body instanceof Phaser.Physics.Arcade.Body)) {
|
|
|
|
throw new Error('Container does not have arcade body');
|
2020-06-04 18:54:34 +02:00
|
|
|
}
|
2020-07-28 17:43:33 +02:00
|
|
|
return body;
|
2020-04-12 16:12:08 +02:00
|
|
|
}
|
2020-04-12 19:06:31 +02:00
|
|
|
|
2020-05-06 01:50:01 +02:00
|
|
|
move(x: number, y: number) {
|
2020-07-28 17:43:33 +02:00
|
|
|
const body = this.getBody();
|
2020-04-12 19:06:31 +02:00
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
body.setVelocity(x, y);
|
2020-04-12 19:06:31 +02:00
|
|
|
|
2020-05-11 19:19:42 +02:00
|
|
|
// up or down animations are prioritized over left and right
|
2020-07-28 17:43:33 +02:00
|
|
|
if (body.velocity.y < 0) { //moving up
|
2021-03-11 16:13:05 +01:00
|
|
|
this.lastDirection = PlayerAnimationDirections.Up;
|
|
|
|
this.playAnimation(PlayerAnimationDirections.Up, true);
|
2020-07-28 17:43:33 +02:00
|
|
|
} else if (body.velocity.y > 0) { //moving down
|
2021-03-11 16:13:05 +01:00
|
|
|
this.lastDirection = PlayerAnimationDirections.Down;
|
|
|
|
this.playAnimation(PlayerAnimationDirections.Down, true);
|
2020-07-28 17:43:33 +02:00
|
|
|
} else if (body.velocity.x > 0) { //moving right
|
2021-03-11 16:13:05 +01:00
|
|
|
this.lastDirection = PlayerAnimationDirections.Right;
|
|
|
|
this.playAnimation(PlayerAnimationDirections.Right, true);
|
2020-07-28 17:43:33 +02:00
|
|
|
} else if (body.velocity.x < 0) { //moving left
|
2021-03-11 16:13:05 +01:00
|
|
|
this.lastDirection = PlayerAnimationDirections.Left;
|
|
|
|
this.playAnimation(PlayerAnimationDirections.Left, true);
|
2020-04-13 15:15:20 +02:00
|
|
|
}
|
2020-05-08 17:35:40 +02:00
|
|
|
|
|
|
|
this.setDepth(this.y);
|
2021-04-01 18:51:51 +02:00
|
|
|
|
|
|
|
if (this.companion) {
|
|
|
|
this.companion.setTarget(this.x, this.y, this.lastDirection);
|
|
|
|
}
|
2020-05-03 22:24:14 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:41:11 +02:00
|
|
|
stop(){
|
2020-07-28 17:43:33 +02:00
|
|
|
this.getBody().setVelocity(0, 0);
|
|
|
|
this.playAnimation(this.lastDirection, false);
|
2020-04-13 15:41:11 +02:00
|
|
|
}
|
2020-04-27 18:12:36 +02:00
|
|
|
|
2020-04-12 19:35:51 +02:00
|
|
|
say(text: string) {
|
|
|
|
if (this.bubble) return;
|
|
|
|
this.bubble = new SpeechBubble(this.scene, this, text)
|
|
|
|
setTimeout(() => {
|
2020-06-04 18:11:07 +02:00
|
|
|
if (this.bubble !== null) {
|
|
|
|
this.bubble.destroy();
|
|
|
|
this.bubble = null;
|
|
|
|
}
|
2020-04-12 19:35:51 +02:00
|
|
|
}, 3000)
|
|
|
|
}
|
2020-05-03 22:24:14 +02:00
|
|
|
|
2021-01-06 15:00:54 +01:00
|
|
|
destroy(): void {
|
2020-07-29 11:40:05 +02:00
|
|
|
for (const sprite of this.sprites.values()) {
|
2020-12-18 13:56:25 +01:00
|
|
|
if(this.scene) {
|
|
|
|
this.scene.sys.updateList.remove(sprite);
|
|
|
|
}
|
2020-07-29 11:40:05 +02:00
|
|
|
}
|
2021-03-31 11:21:06 +02:00
|
|
|
this.list.forEach(objectContaining => objectContaining.destroy())
|
2021-01-06 15:00:54 +01:00
|
|
|
super.destroy();
|
2021-03-31 11:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
playEmote(emoteKey: string) {
|
2021-05-10 17:10:41 +02:00
|
|
|
this.cancelPreviousEmote();
|
2021-05-21 16:25:12 +02:00
|
|
|
|
|
|
|
const scalingFactor = waScaleManager.uiScalingFactor * 0.05;
|
|
|
|
const emoteY = -30 - scalingFactor * 10;
|
2021-03-31 11:21:06 +02:00
|
|
|
|
|
|
|
this.playerName.setVisible(false);
|
2021-05-21 16:25:12 +02:00
|
|
|
this.emote = new Sprite(this.scene, 0, 0, emoteKey);
|
|
|
|
this.emote.setAlpha(0);
|
|
|
|
this.emote.setScale(0.1 * scalingFactor);
|
2021-03-31 11:21:06 +02:00
|
|
|
this.add(this.emote);
|
|
|
|
this.scene.sys.updateList.add(this.emote);
|
2021-05-21 16:25:12 +02:00
|
|
|
|
|
|
|
this.createStartTransition(scalingFactor, emoteY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private createStartTransition(scalingFactor: number, emoteY: number) {
|
|
|
|
this.emoteTween = this.scene.tweens.add({
|
|
|
|
targets: this.emote,
|
|
|
|
props: {
|
|
|
|
scale: scalingFactor,
|
|
|
|
alpha: 1,
|
|
|
|
y: emoteY,
|
|
|
|
},
|
|
|
|
ease: 'Power2',
|
|
|
|
duration: 500,
|
|
|
|
onComplete: () => {
|
|
|
|
this.startPulseTransition(emoteY, scalingFactor);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private startPulseTransition(emoteY: number, scalingFactor: number) {
|
|
|
|
this.emoteTween = this.scene.tweens.add({
|
|
|
|
targets: this.emote,
|
|
|
|
props: {
|
|
|
|
y: emoteY * 1.3,
|
|
|
|
scale: scalingFactor * 1.1
|
|
|
|
},
|
|
|
|
duration: 250,
|
|
|
|
yoyo: true,
|
|
|
|
repeat: 1,
|
|
|
|
completeDelay: 200,
|
|
|
|
onComplete: () => {
|
|
|
|
this.startExitTransition(emoteY);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private startExitTransition(emoteY: number) {
|
|
|
|
this.emoteTween = this.scene.tweens.add({
|
|
|
|
targets: this.emote,
|
|
|
|
props: {
|
|
|
|
alpha: 0,
|
|
|
|
y: 2 * emoteY,
|
|
|
|
},
|
|
|
|
ease: 'Power2',
|
|
|
|
duration: 500,
|
|
|
|
onComplete: () => {
|
|
|
|
this.destroyEmote();
|
|
|
|
}
|
2021-03-31 11:21:06 +02:00
|
|
|
});
|
2020-05-03 22:24:14 +02:00
|
|
|
}
|
2021-05-10 17:10:41 +02:00
|
|
|
|
|
|
|
cancelPreviousEmote() {
|
|
|
|
if (!this.emote) return;
|
|
|
|
|
2021-05-21 16:25:12 +02:00
|
|
|
this.emoteTween?.remove();
|
|
|
|
this.destroyEmote()
|
|
|
|
}
|
|
|
|
|
|
|
|
private destroyEmote() {
|
2021-05-10 17:10:41 +02:00
|
|
|
this.emote?.destroy();
|
|
|
|
this.emote = null;
|
|
|
|
this.playerName.setVisible(true);
|
|
|
|
}
|
2020-04-27 18:12:36 +02:00
|
|
|
}
|