-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainbow.js
134 lines (120 loc) · 3.22 KB
/
rainbow.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
127
128
129
130
131
132
133
134
var mstates = {
"-1": 0,
0: 1,
1: 1,
2: 2,
3: 2,
4: 3,
5: 3,
6: 19,
7: 19,
8: 35,
9: 35,
10: 51,
11: 51,
12: 50,
13: 50,
14: 49,
15: 49,
16: 48,
17: 48,
18: 32,
19: 32,
20: 16,
21: 16,
22: 0,
23: 0
}
var states = {};
var ml = 24;
var ix = 2;
var int = function () {
ix = 3;
states = {};
for (var i = 0; i < 8; i++) {
var stateInner = {};
for (var j = 0; j < 8; j++) {
stateInner[j] = (i + j) % ml - 16;
}
states[i] = stateInner;
}
}
var restartable = true;
var running = false;
var intId = 0;
var vars = require("./vars.js");
module.exports = {
timeout: 50,
ready: function () {
restartable = false;
running = true;
int();
vars.timeout = 50;
return false;
},
update: function () {
if (!restartable) {
for (var i = 0; i < 8; i++) {
var lm = "";
var col = (i * 3 + ix) % 5;
for (var j = 0; j < col; j++) lm += ".";
lm += "O";
var butt1 = launchpad.getButton(7 - i, col);
states[i][col] = states[i][col] + 1;
if (running || (states[7][7] < ml - 2))
states[i][col] %= ml;
else
states[i][col] = Math.min(states[i][col], ml - 2);
butt1.light(mstates[Math.max(states[i][col], -1)]);
//sleep.msleep(1);
if (col < 3) {
var prCol = col;
col += 5;
for (var j = prCol + 1; j < col; j++) lm += ".";
lm += "O";
var butt2 = launchpad.getButton(7 - i, col);
states[i][col] = states[i][col] + 1;
if (running || (states[7][7] < ml - 2))
states[i][col] %= ml;
else
states[i][col] = Math.min(states[i][col], ml - 2);
butt2.light(mstates[Math.max(states[i][col], -1)]);
//sleep.msleep(1);
}
for (j = col + 1; j < 8; j++) lm += ".";
//console.log(lm);
}
if (!running) {
var rme = 64;
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 8; j++) {
if (states[i][j] >= ml - 2 || states[i][j] < 0) {
--rme;
}
}
}
//console.log(rme);
if (rme <= 0) {
restartable = true;
clearInterval(intId);
return true;
}
}
ix += 1;
return false;
}
},
press: function (btn) {
//console.log("running:" + (running==true?"true":"false") + ", intId = " + intId);
if (running) {
restartable = false;
running = false;
//console.log(running==true?"true":"false");
}
},
exit: function () {
restartable = false;
running = false;
return false;
}
};