forked from cgolden15/tetris
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TtyBlock.js
84 lines (66 loc) · 2.1 KB
/
TtyBlock.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
function TtyBlock (divName, numLines, rollOverLength, rollOverRemove) {
var i;
this.elem = document.getElementById(divName);
// slow scorlling effect variables
this.curPos = 0;
this.cursorShown = false;
// TODO: make these random starting values
this.timePassedType = 0;
this.timePassedFlash = 0;
// time in ms
this.typePeriod = 30;
this.flashPeriod = 300;
this.lines = [];
for (i = 0; i < numLines; i += 1) {
this.lines.push("");
}
this.rollOverLength = rollOverLength || 9;
this.rollOverRemove = rollOverRemove || 3;
this.backlog = [];
}
/**
updates the text block
*/
TtyBlock.prototype.draw = function (dTime) {
var i,
outputString = "",
lastLine;
this.timePassedType += dTime;
while (this.timePassedType > this.typePeriod) {
this.curPos += 1;
this.timePassedType -= this.typePeriod;
}
lastLine = this.lines[this.lines.length-1];
if (this.curPos > lastLine.length) {
this.timePassedFlash += dTime;
while (this.timePassedFlash > this.flashPeriod) {
this.cursorShown = !this.cursorShown;
this.timePassedFlash -= this.flashPeriod;
}
}
// if I'm past the end of the last line, and there is a backlog, shift all the lines
if (this.curPos > lastLine.length && this.backlog.length > 0) {
this.lines.shift();
lastLine = this.backlog.shift();
this.lines.push(lastLine);
this.curPos = 0;
}
// print all of the lines but the last one
for (i = 0; i < this.lines.length - 1; i += 1) {
outputString += this.lines[i] + "<br/>";
}
outputString += lastLine.slice(0, Math.min(this.curPos, lastLine.length));
if (this.cursorShown) {
outputString += "_";
}
// rewirte for html gaurds
outputString.replace('>', '>');
this.elem.innerHTML = outputString;
};
TtyBlock.prototype.addLine = function(str) {
// if the backlog is too long, then remove the last 3 values
if (this.backlog.length > this.rollOverLength) {
this.backlog.splice(this.backlog.length - this.rollOverRemove, this.rollOverRemove);
}
this.backlog.push(" > " + str);
};