/
typing-next.js
110 lines (93 loc) · 2.63 KB
/
typing-next.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import phaser from 'phaser/src/phaser.js';
import TextPagePlugin from '../../plugins/textpage-plugin.js'
import TextTypingPlugin from '../../plugins/texttyping-plugin.js';
const GetFastValue = Phaser.Utils.Objects.GetFastValue;
class PageTypingText extends Phaser.GameObjects.Text {
constructor(scene, x, y, text, config) {
super(scene, x, y, text, config);
this.page = scene.plugins.get('rexTextPage').add(this, GetFastValue(config, 'page', undefined));
this.typing = scene.plugins.get('rexTextTyping').add(this, GetFastValue(config, 'type', undefined));
this.typing.on('complete', this.typeNextPage, this);
this.contents = [];
}
start(text, speed) {
this.page.setText(text);
if (speed !== undefined) {
this.typing.setTypingSpeed(speed);
}
this.typeNextPage();
}
typeNextPage() {
if (!this.page.isLastPage) {
var txt = this.page.getNextPage();
this.typing.start(txt);
} else if (this.contents.length === 0) {
this.emit('complete');
}
}
appendContent(content, speed) {
this.contents.push([content, speed]);
if (!this.typing.isTyping) {
this.startNext();
}
}
startNext() {
if (this.contents.length > 0) {
var cmd = this.contents.shift();
this.start(cmd[0], cmd[1]);
}
}
}
// PageTypingText class
class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}
preload() { }
create() {
var txt = new PageTypingText(this, 100, 100, '', {
wordWrap: {
width: 500
},
maxLines: 7
});
this.add.existing(txt);
txt.appendContent('hellohellohello', 50);
txt.appendContent('worldworldworld', 100);
txt.appendContent('goodbye', 300);
txt.setInteractive().on('pointerup', function () {
txt.startNext()
})
txt.once('complete', function () {
console.log('done');
})
}
update() { }
}
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
global: [{
key: 'rexTextPage',
plugin: TextPagePlugin,
start: true
},
{
key: 'rexTextTyping',
plugin: TextTypingPlugin,
start: true
}
]
}
};
var game = new Phaser.Game(config);