partey_workadventure/front/src/Phaser/Entity/PlayableCaracter.ts

107 lines
4.3 KiB
TypeScript
Raw Normal View History

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";
import BitmapText = Phaser.GameObjects.BitmapText;
export const PLAYER_RESOURCES: Array<any> = [
{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}
];
export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
2020-04-12 19:35:51 +02:00
private bubble: SpeechBubble;
private playerName: BitmapText;
public PlayerValue: string;
public PlayerTexture: string;
constructor(scene: Phaser.Scene, x: number, y: number, texture: string, name: string, frame?: string | number) {
super(scene, x, y, texture, frame);
this.PlayerValue = name;
this.PlayerTexture = texture;
2020-05-01 23:48:30 +02:00
this.playerName = new BitmapText(scene, x, y - 25, 'main_font', name, 8);
this.playerName.setOrigin(0.5).setCenterAlign().setDepth(99999);
scene.add.existing(this.playerName);
this.scene.sys.updateList.add(this);
this.scene.sys.displayList.add(this);
2020-04-27 18:12:36 +02:00
//this.setScale(2);
this.scene.physics.world.enableBody(this);
this.setImmovable(true);
this.setCollideWorldBounds(true);
2020-05-11 19:19:42 +02:00
this.setSize(16, 16); //edit the hitbox to better match the character model
2020-04-13 16:53:19 +02:00
this.setOffset(8, 16);
this.setDepth(-1);
this.scene.events.on('postupdate', this.postupdate.bind(this));
}
move(x: number, y: number) {
this.setVelocity(x, y);
2020-05-11 19:19:42 +02:00
// up or down animations are prioritized over left and right
2020-04-13 15:41:11 +02:00
if (this.body.velocity.y < 0) { //moving up
this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkUp}`, true);
2020-04-30 19:36:28 +02:00
} else if (this.body.velocity.y > 0) { //moving down
this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkDown}`, true);
2020-04-30 19:36:28 +02:00
} else if (this.body.velocity.x > 0) { //moving right
this.play(`${this.PlayerTexture}-${PlayerAnimationNames.WalkRight}`, true);
2020-04-30 19:36:28 +02:00
} else if (this.body.velocity.x < 0) { //moving left
this.anims.playReverse(`${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`, true);
}
2020-04-13 15:15:20 +02:00
if (this.bubble) {
2020-04-13 15:15:20 +02:00
this.bubble.moveBubble(this.x, this.y);
}
//update depth user
this.setDepth(this.y);
}
postupdate(time: number, delta: number) {
//super.update(delta);
this.playerName.setPosition(this.x, this.y - 25);
}
2020-04-13 15:41:11 +02:00
stop(){
this.setVelocity(0, 0);
this.anims.stop();
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(() => {
this.bubble.destroy();
this.bubble = null;
}, 3000)
}
destroy(fromScene?: boolean): void {
if (this.scene) {
this.scene.events.removeListener('postupdate', this.postupdate.bind(this));
}
super.destroy(fromScene);
this.playerName.destroy();
}
2020-04-27 18:12:36 +02:00
}