-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (99 loc) · 2.47 KB
/
index.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
const fs = require('fs');
const term = require('terminal-kit').terminal;
const argv = require('yargs').argv;
const path = require('path');
const walk1 = fs.readFileSync(path.join(__dirname, '/ascii/pv-1-80.txt'), 'utf8');
const walk2 = fs.readFileSync(path.join(__dirname, '/ascii/pv-2-80.txt'), 'utf8');
const jump1 = fs.readFileSync(path.join(__dirname, '/ascii/pv-3-80.txt'), 'utf8');
const jump2 = fs.readFileSync(path.join(__dirname, '/ascii/pv-4-80.txt'), 'utf8');
const statesWalking = [walk1, walk2];
const statesJumping = [jump1, jump2];
const millisLimit = parseSArg() || -1;
let stateWalking = 0,
stateJumping = 0;
// setup terminal
term.clear();
term.moveTo(1, 1);
term.hideCursor();
term.bgBlack();
term.brightGreen();
function parseSArg() {
if(!argv.s) {
return null;
}
if(!Number.isInteger(argv.s)) {
return null;
}
return parseInt(argv.s, 10) * 1000;
}
function nextWalkState() {
stateWalking++;
if(stateWalking > statesWalking.length - 1) {
stateWalking = 0;
}
return stateWalking;
};
function nextJumpState() {
stateJumping++;
if(stateJumping > statesJumping.length - 1) {
stateJumping = 0;
}
return stateJumping;
};
function mainLoop() {
// every 3rd loop, do the jump
let sinceLastJump = 0;
const start = Date.now();
const loop = setInterval(() => {
term.clear();
term.moveTo(1, 1);
let next = null,
iter = 1;
if([3, 4].includes(sinceLastJump)) {
next = statesJumping[nextJumpState()];
} else {
next = statesWalking[nextWalkState()];
}
let lines = next.split('\n');
lines.forEach((line) => {
term.moveTo(1, iter, line);
iter++;
});
sinceLastJump++;
if(sinceLastJump >= 5) {
sinceLastJump = 0;
}
if(millisLimit !== -1 && Date.now() - start > millisLimit) {
clearInterval(loop);
}
}, 300);
}
function walkOnLeft(cb) {
let x = term.width;
let amountToSubstring = 0;
const loop = setInterval(() => {
term.clear();
term.moveTo(1, 1);
let next = statesWalking[nextWalkState()];
let iter = 1;
let lines = next.split('\n');
lines.forEach((line) => {
let sub = line.substr(0, amountToSubstring);
term.moveTo(x, iter, sub);
iter++;
});
// move to the left.
x -= 7;
amountToSubstring += 7;
if(amountToSubstring >= 80) {
amountToSubstring = 80;
}
if(x <= 7) {
clearInterval(loop);
return cb();
}
}, 150);
}
walkOnLeft(() => {
mainLoop();
});