1
0
Fork 0
spacetac/src/ui/common/Messages.ts

68 lines
2 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
// A single displayed message
class Message extends Phaser.Group {
2017-05-22 20:41:34 +00:00
view: BaseView
background: Phaser.Graphics
text: Phaser.Text
2017-02-16 22:59:41 +00:00
constructor(parent: Messages, text: string, duration: number) {
2017-05-22 20:41:34 +00:00
super(parent.view.game);
this.view = parent.view;
this.background = new Phaser.Graphics(this.game);
this.add(this.background);
2017-02-16 22:59:41 +00:00
this.text = new Phaser.Text(this.game, 0, 0, text,
2017-08-15 22:30:19 +00:00
{ font: "bold 14pt SpaceTac", fill: "#90FEE3" });
2017-05-22 20:41:34 +00:00
this.add(this.text);
2017-05-22 20:41:34 +00:00
this.position.set(parent.view.getWidth(), 10);
parent.view.timer.schedule(duration, () => this.hide());
}
// Hide the message
hide() {
var tween = this.game.tweens.create(this);
2017-02-09 00:00:35 +00:00
tween.to({ y: this.y + 50, alpha: 0 }, 400, Phaser.Easing.Circular.In);
tween.onComplete.addOnce(() => {
this.destroy();
});
tween.start();
}
2017-05-22 20:41:34 +00:00
update() {
UITools.drawBackground(this.text, this.background, 6);
this.x = this.view.getWidth() - this.width - 10;
}
}
// Visual notifications of game-related messages (eg. "Game saved"...)
export class Messages {
// Link to parent view
2017-05-22 20:41:34 +00:00
view: BaseView;
// Main group to hold the visual messages
2017-02-16 22:59:41 +00:00
container: Phaser.Group;
constructor(parent: BaseView) {
2017-05-22 20:41:34 +00:00
this.view = parent;
this.container = new Phaser.Group(parent.game);
parent.add.existing(this.container);
}
// Add a new message to the notifications
2017-05-22 20:41:34 +00:00
addMessage(text: string, duration: number = 3000): void {
this.container.forEachExists((child: Message) => {
2017-05-22 20:41:34 +00:00
child.y += child.height + 5;
}, this);
2017-02-16 22:59:41 +00:00
var message = new Message(this, text, duration);
this.container.addChild(message);
}
}
}