caa-game/src/helpers/textbox.js

39 lines
889 B
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Container, Text, Rect } = asdf;
const fillStyle = {fill: "#ffffff", font: "16px Minecraft"};
class TextBox extends Container {
constructor(string, lifespan = 2.5) {
super();
this.pos = { x: 0, y: 0 };
this.string = string;
this.lifespan = lifespan;
const background = new Rect(640, 24, {fill: "rgba(0,0,0,0.5)"});
background.pos = this.pos;
this.add(background);
const text = new Text(string, fillStyle);
var letters = string.split("");
// 10px is the average letter width
text.pos = {
x: (640 / 2) - ((letters.length * 10) / 2),
y: 16
};
this.add(text);
}
update(dt) {
super.update(dt);
if (this.lifespan <= 0) {
this.dead = true;
} else {
this.lifespan -= dt;
}
}
}
module.exports = TextBox;