48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
export default class Message {
|
|
constructor(game, text) {
|
|
this.game = game
|
|
this.text = text
|
|
this.textColor = "white"
|
|
this.backgroundColor = "black"
|
|
this.textIndex = 0
|
|
this.textProgress = 0.0
|
|
this.textSpeed = 0.08 // seconds per character
|
|
this.backgroundHeight = 40
|
|
this.textSize = 30
|
|
}
|
|
|
|
tick(dt) {
|
|
this.textProgress += (dt / 1000.0) / this.textSpeed
|
|
this.textIndex = Math.floor(this.textProgress)
|
|
|
|
const ijp = this.game.input.isInputJustPressed("interact")
|
|
if (ijp) {
|
|
if (this.messageComplete()) {
|
|
this.game.closeMessage(this)
|
|
} else {
|
|
this.textProgress = this.text.length
|
|
}
|
|
}
|
|
}
|
|
|
|
draw(ctx) {
|
|
ctx.fillStyle = this.backgroundColor
|
|
ctx.fillRect(0, 0, ctx.canvas.width, this.backgroundHeight)
|
|
ctx.font = `bold ${this.textSize}px sans-serif`
|
|
ctx.textBaseline = "top"
|
|
ctx.fillStyle = this.textColor
|
|
ctx.fillText(this.text.substring(0, this.textIndex), 5, 5)
|
|
if (this.messageComplete()) {
|
|
ctx.fillRect(
|
|
ctx.canvas.width - 20,
|
|
this.backgroundHeight - 20,
|
|
10, 10
|
|
)
|
|
}
|
|
}
|
|
|
|
messageComplete() {
|
|
return this.textIndex >= this.text.length + 3
|
|
}
|
|
}
|