2020-04-12 16:12:08 +02:00
|
|
|
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "../Player/Animation";
|
|
|
|
import {ActiveEventList, UserInputEvent} from "../UserInput/UserInputManager";
|
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-04-12 16:12:08 +02:00
|
|
|
|
|
|
|
export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
2020-04-12 19:35:51 +02:00
|
|
|
private bubble: SpeechBubble;
|
2020-05-01 23:38:09 +02:00
|
|
|
private playerName: BitmapText;
|
2020-04-12 16:12:08 +02:00
|
|
|
|
2020-05-01 23:38:09 +02:00
|
|
|
constructor(scene: Phaser.Scene, x: number, y: number, texture: string, name: string, frame?: string | number) {
|
2020-04-12 16:12:08 +02:00
|
|
|
super(scene, x, y, texture, frame);
|
|
|
|
|
2020-05-01 23:38:09 +02:00
|
|
|
// Yes, I know, I'm declaring a sprite inside a sprite. ARP, save me from this madness :)
|
|
|
|
this.playerName = new BitmapText(scene, x, y - 32, 'main_font', name, 8);
|
|
|
|
this.playerName.setOrigin(0.5).setCenterAlign();
|
|
|
|
scene.add.existing(this.playerName);
|
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
this.scene.sys.updateList.add(this);
|
|
|
|
this.scene.sys.displayList.add(this);
|
2020-04-27 18:12:36 +02:00
|
|
|
//this.setScale(2);
|
2020-04-13 13:42:21 +02:00
|
|
|
this.scene.physics.world.enableBody(this);
|
2020-04-12 16:12:08 +02:00
|
|
|
this.setImmovable(true);
|
2020-04-13 13:42:21 +02:00
|
|
|
this.setCollideWorldBounds(true);
|
2020-04-13 16:53:19 +02:00
|
|
|
this.setSize(16, 16); //edit the hitbox to better match the caracter model
|
|
|
|
this.setOffset(8, 16);
|
2020-04-12 16:12:08 +02:00
|
|
|
}
|
2020-04-12 19:06:31 +02:00
|
|
|
|
|
|
|
move(x: number, y: number){
|
|
|
|
|
|
|
|
this.setVelocity(x, y);
|
|
|
|
|
2020-04-30 19:36:28 +02:00
|
|
|
//up or down animationss are prioritized over left and right
|
2020-04-13 15:41:11 +02:00
|
|
|
if (this.body.velocity.y < 0) { //moving up
|
2020-04-12 19:06:31 +02:00
|
|
|
this.play(PlayerAnimationNames.WalkUp, true);
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (this.body.velocity.y > 0) { //moving down
|
2020-04-12 19:06:31 +02:00
|
|
|
this.play(PlayerAnimationNames.WalkDown, true);
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (this.body.velocity.x > 0) { //moving right
|
|
|
|
this.play(PlayerAnimationNames.WalkRight, true);
|
|
|
|
} else if (this.body.velocity.x < 0) { //moving left
|
|
|
|
this.anims.playReverse(PlayerAnimationNames.WalkLeft, true);
|
2020-04-12 19:06:31 +02:00
|
|
|
}
|
2020-04-13 15:15:20 +02:00
|
|
|
|
|
|
|
if(this.bubble) {
|
|
|
|
this.bubble.moveBubble(this.x, this.y);
|
|
|
|
}
|
2020-05-01 23:38:09 +02:00
|
|
|
this.playerName.setPosition(this.x, this.y - 32);
|
2020-04-12 19:06:31 +02:00
|
|
|
}
|
2020-04-13 15:41:11 +02:00
|
|
|
|
|
|
|
stop(){
|
|
|
|
this.setVelocity(0, 0);
|
|
|
|
this.play(PlayerAnimationNames.None, true);
|
|
|
|
}
|
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)
|
|
|
|
//todo make the buble destroy on player movement?
|
|
|
|
setTimeout(() => {
|
|
|
|
this.bubble.destroy();
|
|
|
|
this.bubble = null;
|
|
|
|
}, 3000)
|
|
|
|
}
|
2020-04-27 18:12:36 +02:00
|
|
|
}
|