-
Notifications
You must be signed in to change notification settings - Fork 8
/
typewriter.js
126 lines (115 loc) · 3.79 KB
/
typewriter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function Typewriter() {
this.typedText;
this.timer;
this.pickedQuote;
var _that = this;
var game;
function init(gameInstance, options) {
game = gameInstance;
_that.time = options.time || Phaser.Timer.SECOND / 10;
_that.sound = options.sound || null;
_that.soundMarker = options.soundMarker || null;
_that.writerFn = options.writerFn || null;
_that.endFn = options.endFn || null;
_that.times = options.times || 10;
_that.text = options.text || "";
_that.x = options.x || 100;
_that.y = options.y || 100;
_that.maxWidth = options.maxWidth || 200;
_that.fontFamily = options.fontFamily || "blackFont";
_that.fontSize = options.fontSize || 28;
_that.writerObj = options.writerObj || null;
}
function start() {
enableTypingSpecificMessage(_that.text, _that.x, _that.y);
}
function stop() {
if (_that.timer !== undefined) {
_that.timer.stop();
game.time.events.remove(_that.timer);
}
if (_that.sound !== null) {
_that.sound.stop();
}
//if(_that.typedText !== undefined){ // This can cause problems if you repeatedly type to a text object. ~Tilde
// _that.typedText.destroy();
//}
}
function enableTypingSpecificMessage(text, x, y) {
if (_that.writerObj === null) {
_that.typedText = game.add.bitmapText(x, y, _that.fontFamily, text, _that.fontSize);
} else {
_that.typedText = _that.writerObj;
}
_that.typedText.maxWidth = _that.maxWidth;
_that.currentLetter = 0;
var length = _that.typedText.children.length;
for (var i = 0; i < length; i++) {
var letter = _that.typedText.getChildAt(i);
letter.alpha = 0;
}
if (_that.sound !== null) { // Made some alterations for sound markers here. ~Tilde
if (_that.soundMarker !== null)
_that.sound.play(_that.soundMarker, null, 1, true, true);
else
_that.sound.play('', null, 1, true, true);
}
_that.typedText.x = x;
_that.typedText.y = y;
if (_that.endFn !== null) {
countdown(typeWriter, length, _that.endFn);
} else {
countdown(typeWriter, length);
}
}
/**
* [countDown description]
* @param {Function} fn [description]
* @param {[type]} endFn [description]
* @return {[type]} [description]
*/
function countdown(fn, times, endFn) {
var _timer = game.time.create(false);
_timer.start();
endFn = endFn || function() {
game.time.events.remove(_timer);
if (_that.sound !== null) {
_that.sound.stop();
}
};
_timer.onComplete.add(endFn);
_timer.repeat(_that.time, times, fn, this);
_that.timer = _timer;
}
function typeWriter(text) {
if (_that.sound !== null) {
if (_that.sound.isPlaying === false) {
_that.sound.play();
}
}
var letter = _that.typedText.getChildAt(_that.currentLetter);
letter.alpha = 1;
_that.currentLetter++;
}
return {
init: function(gameInstance, options) {
init(gameInstance, options);
},
start: function() {
stop();
start();
},
destroy: function() {
_that.typedText.destroy();
},
hideText: function() {
_that.typedText.visible = false;
},
showText: function() {
_that.typedText.visible = true;
},
moveToTop: function() {
game.bringToTop(_that.typedText);
}
}
}