2020-05-04 01:48:14 +02:00
|
|
|
import {PlayerAnimationNames} from "../Player/Animation";
|
2020-04-12 19:35:51 +02:00
|
|
|
import {SpeechBubble} from "./SpeechBubble";
|
2020-05-01 23:38:09 +02:00
|
|
|
import BitmapText = Phaser.GameObjects.BitmapText;
|
2020-07-28 17:43:33 +02:00
|
|
|
import Container = Phaser.GameObjects.Container;
|
|
|
|
import Sprite = Phaser.GameObjects.Sprite;
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2020-06-19 16:36:40 +02:00
|
|
|
export interface PlayerResourceDescriptionInterface {
|
|
|
|
name: string,
|
|
|
|
img: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PLAYER_RESOURCES: Array<PlayerResourceDescriptionInterface> = [
|
2020-05-25 23:26:27 +02:00
|
|
|
{name: "male1", img: "resources/characters/pipoya/Male 01-1.png" /*, x: 32, y: 32*/},
|
|
|
|
{name: "male2", img: "resources/characters/pipoya/Male 02-2.png"/*, x: 64, y: 32*/},
|
|
|
|
{name: "male3", img: "resources/characters/pipoya/Male 03-4.png"/*, x: 96, y: 32*/},
|
|
|
|
{name: "male4", img: "resources/characters/pipoya/Male 09-1.png"/*, x: 128, y: 32*/},
|
|
|
|
|
|
|
|
{name: "male5", img: "resources/characters/pipoya/Male 10-3.png"/*, x: 32, y: 64*/},
|
|
|
|
{name: "male6", img: "resources/characters/pipoya/Male 17-2.png"/*, x: 64, y: 64*/},
|
|
|
|
{name: "male7", img: "resources/characters/pipoya/Male 18-1.png"/*, x: 96, y: 64*/},
|
|
|
|
{name: "male8", img: "resources/characters/pipoya/Male 16-4.png"/*, x: 128, y: 64*/},
|
|
|
|
|
|
|
|
{name: "Female1", img: "resources/characters/pipoya/Female 01-1.png"/*, x: 32, y: 96*/},
|
|
|
|
{name: "Female2", img: "resources/characters/pipoya/Female 02-2.png"/*, x: 64, y: 96*/},
|
|
|
|
{name: "Female3", img: "resources/characters/pipoya/Female 03-4.png"/*, x: 96, y: 96*/},
|
|
|
|
{name: "Female4", img: "resources/characters/pipoya/Female 09-1.png"/*, x: 128, y: 96*/},
|
|
|
|
|
|
|
|
{name: "Female5", img: "resources/characters/pipoya/Female 10-3.png"/*, x: 32, y: 128*/},
|
|
|
|
{name: "Female6", img: "resources/characters/pipoya/Female 17-2.png"/*, x: 64, y: 128*/},
|
|
|
|
{name: "Female7", img: "resources/characters/pipoya/Female 18-1.png"/*, x: 96, y: 128*/},
|
|
|
|
{name: "Female8", img: "resources/characters/pipoya/Female 16-4.png"/*, x: 128, y: 128*/}
|
2020-05-06 01:50:01 +02:00
|
|
|
];
|
|
|
|
|
2020-06-04 18:54:34 +02:00
|
|
|
interface AnimationData {
|
|
|
|
key: string;
|
|
|
|
frameRate: number;
|
|
|
|
repeat: number;
|
|
|
|
frameModel: string; //todo use an enum
|
|
|
|
frameStart: number;
|
|
|
|
frameEnd: number;
|
|
|
|
}
|
|
|
|
|
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;
|
2020-05-22 22:59:43 +02:00
|
|
|
private readonly playerName: BitmapText;
|
2020-05-06 01:50:01 +02:00
|
|
|
public PlayerValue: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
public sprites: Map<string, Sprite>;
|
|
|
|
private lastDirection: string = PlayerAnimationNames.WalkDown;
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2020-06-04 18:54:34 +02:00
|
|
|
constructor(scene: Phaser.Scene,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-07-28 17:43:33 +02:00
|
|
|
textures: string[],
|
2020-06-04 18:54:34 +02:00
|
|
|
name: string,
|
|
|
|
direction: string,
|
|
|
|
moving: boolean,
|
|
|
|
frame?: string | number
|
|
|
|
) {
|
2020-07-28 17:43:33 +02:00
|
|
|
super(scene, x, y/*, texture, frame*/);
|
|
|
|
|
|
|
|
this.sprites = new Map<string, Sprite>();
|
|
|
|
|
|
|
|
for (const texture of textures) {
|
|
|
|
const sprite = new Sprite(scene, 0, 0, texture, frame);
|
2020-07-29 11:40:05 +02:00
|
|
|
this.add(sprite);
|
2020-07-28 17:43:33 +02:00
|
|
|
this.getPlayerAnimations(texture).forEach(d => {
|
|
|
|
this.scene.anims.create({
|
|
|
|
key: d.key,
|
|
|
|
frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
|
|
|
|
frameRate: d.frameRate,
|
|
|
|
repeat: d.repeat
|
|
|
|
});
|
|
|
|
})
|
2020-07-29 11:40:05 +02:00
|
|
|
// Needed, otherwise, animations are not handled correctly.
|
2020-07-28 17:43:33 +02:00
|
|
|
this.scene.sys.updateList.add(sprite);
|
|
|
|
this.sprites.set(texture, sprite);
|
|
|
|
}
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2020-05-06 01:50:01 +02:00
|
|
|
this.PlayerValue = name;
|
2020-05-01 23:48:30 +02:00
|
|
|
this.playerName = new BitmapText(scene, x, y - 25, 'main_font', name, 8);
|
2020-05-11 19:16:36 +02:00
|
|
|
this.playerName.setOrigin(0.5).setCenterAlign().setDepth(99999);
|
2020-05-01 23:38:09 +02:00
|
|
|
scene.add.existing(this.playerName);
|
|
|
|
|
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
|
|
|
|
|
|
|
this.scene.events.on('postupdate', this.postupdate.bind(this));
|
2020-06-04 18:54:34 +02:00
|
|
|
|
|
|
|
this.playAnimation(direction, moving);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getPlayerAnimations(name: string): AnimationData[] {
|
|
|
|
return [{
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkDown}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 0,
|
|
|
|
frameEnd: 2,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkLeft}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 3,
|
|
|
|
frameEnd: 5,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkRight}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 6,
|
|
|
|
frameEnd: 8,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkUp}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 9,
|
|
|
|
frameEnd: 11,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected playAnimation(direction : string, moving: boolean): void {
|
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)) {
|
|
|
|
sprite.play(texture+'-'+direction, true);
|
|
|
|
} else if (!moving) {
|
|
|
|
/*if (this.anims.currentAnim) {
|
|
|
|
this.anims.stop();
|
|
|
|
}*/
|
|
|
|
sprite.play(texture+'-'+direction, true);
|
|
|
|
sprite.anims.stop();
|
|
|
|
}
|
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
|
|
|
|
this.lastDirection = PlayerAnimationNames.WalkUp;
|
|
|
|
this.playAnimation(PlayerAnimationNames.WalkUp, true);
|
|
|
|
//this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkUp}`, true);
|
|
|
|
} else if (body.velocity.y > 0) { //moving down
|
|
|
|
this.lastDirection = PlayerAnimationNames.WalkDown;
|
|
|
|
this.playAnimation(PlayerAnimationNames.WalkDown, true);
|
|
|
|
//this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkDown}`, true);
|
|
|
|
} else if (body.velocity.x > 0) { //moving right
|
|
|
|
this.lastDirection = PlayerAnimationNames.WalkRight;
|
|
|
|
this.playAnimation(PlayerAnimationNames.WalkRight, true);
|
|
|
|
//this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkRight}`, true);
|
|
|
|
} else if (body.velocity.x < 0) { //moving left
|
|
|
|
this.lastDirection = PlayerAnimationNames.WalkLeft;
|
|
|
|
this.playAnimation(PlayerAnimationNames.WalkLeft, true);
|
|
|
|
//this.anims.playReverse(`${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`, true);
|
2020-04-12 19:06:31 +02:00
|
|
|
}
|
2020-04-13 15:15:20 +02:00
|
|
|
|
2020-05-06 01:50:01 +02:00
|
|
|
if (this.bubble) {
|
2020-04-13 15:15:20 +02:00
|
|
|
this.bubble.moveBubble(this.x, this.y);
|
|
|
|
}
|
2020-05-08 17:35:40 +02:00
|
|
|
|
|
|
|
//update depth user
|
|
|
|
this.setDepth(this.y);
|
2020-05-03 22:24:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 11:49:55 +02:00
|
|
|
postupdate(time: number, delta: number) {
|
|
|
|
//super.update(delta);
|
|
|
|
this.playerName.setPosition(this.x, this.y - 25);
|
2020-04-12 19:06:31 +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)
|
2020-05-01 23:48:30 +02:00
|
|
|
//todo make the bubble destroy on player movement?
|
2020-04-12 19:35:51 +02:00
|
|
|
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
|
|
|
|
|
|
|
destroy(fromScene?: boolean): void {
|
2020-05-12 11:49:55 +02:00
|
|
|
if (this.scene) {
|
|
|
|
this.scene.events.removeListener('postupdate', this.postupdate.bind(this));
|
|
|
|
}
|
2020-07-29 11:40:05 +02:00
|
|
|
for (const sprite of this.sprites.values()) {
|
|
|
|
this.scene.sys.updateList.remove(sprite);
|
|
|
|
}
|
2020-05-03 22:24:14 +02:00
|
|
|
super.destroy(fromScene);
|
|
|
|
this.playerName.destroy();
|
|
|
|
}
|
2020-04-27 18:12:36 +02:00
|
|
|
}
|