-
Notifications
You must be signed in to change notification settings - Fork 6
/
arrowSection.d
185 lines (133 loc) · 3.41 KB
/
arrowSection.d
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module arrowSection;
import ldc.intrinsics;
import tango.stdc.stringz;
import ncurses;
import tango.io.Stdout;
import tango.util.Convert;
import tango.math.random.Random;
import tango.io.stream.TextFile;
import tango.stdc.posix.unistd;
import asciiSprite;
class ArrowSection {
AsciiSprite _frame, hit, l, r, u, d;
double sleep;
ubyte _input;
TextFileInput chartFile;
long misses, good, great, actualMisses;
bool noMoreBeats;
this(char[] arrowFile) {
chartFile = new TextFileInput("arrow_charts/" ~ arrowFile);
if(chartFile is null){ assert(false);_exit(1);}
auto bpm = chartFile.next;
//if(bpm[0..4] == "BPM:"){
sleep = 60.0 / to!(double)(bpm);
/*}else{
assert(false, "BAD arrowchart!");
_exit(1);
}*/
// burn the second line -- how long the song is
bpm = chartFile.next;
_frame = new AsciiSprite("graphics/arrow-frame.txt", null, true, 60, 0);
hit = new AsciiSprite("graphics/hit_it_now_bar.txt", null, true, 62, 5);
l = new AsciiSprite("graphics/arrow-left.txt", null, true, 63, 0);
d = new AsciiSprite("graphics/arrow-down.txt", null, true, 71, 0);
u = new AsciiSprite("graphics/arrow-up.txt", null, true, 79, 0);
r = new AsciiSprite("graphics/arrow-right.txt", null, true, 87, 0);
for(int i = 0; i < beatsOnScreen; i++){
beats ~= new Beat;
}
}
void draw(bool fast) {
_frame.drawSprite();
// draw arrows and shit
bool first = false;
hit.drawSprite();
if(!fast){
// XXX: atomic swap on _input
ubyte diff, cachedInput;
cachedInput = llvm_atomic_swap!(ubyte)(&_input, 0);
if(offset > 1){
if(beats[0].arrows == cachedInput){
good += lut[cachedInput];
beats[0].arrows = 0;
first = true;
}
}
if(offset < 3 && !first){
if(cachedInput == beats[1].arrows){
if(offset == 0){
great += lut[cachedInput];
}else{
good += lut[cachedInput];
}
beats[1].arrows = 0;
}
}
if(offset == 0){
// parse shite frum file, appendto arrows and drop top if required
Beat* beat = new Beat;
char[] line = chartFile.next;
if(line is null){beat.end = true;}
foreach(ch; line){
switch(ch){
case 'l': beat.arrows |= 1; break;
case 'r': beat.arrows |= 2; break;
case 'u': beat.arrows |= 4; break;
case 'd': beat.arrows |= 8; break;
case 'x': beat.arrows |= randomArrow(); break;
}
}
beats ~= beat;
if(beats.length > beatsOnScreen){
// score Misses on dis
if(beats[0].arrows != 0){misses++;}
actualMisses += lut[beats[0].arrows];
if(beats[0].end){noMoreBeats = true;}
beats = beats[1..$];
}
}
offset--;
if(offset < 0){offset = 4;}
}
// Draw
for(int i = 0; i < beats.length; i++){
ubyte arrows = beats[i].arrows;
int row = 1 + offset + 5*i;
// draw left arrow
if(arrows & 1){
l.setY(row);
l.drawSprite();
}
// draw right arrow
if(arrows & 2){
r.setY(row);
r.drawSprite();
}
// draw up arrow
if(arrows & 4){
u.setY(row);
u.drawSprite();
}
// draw down arrow
if(arrows & 8){
d.setY(row);
d.drawSprite();
}
}
}
private:
Beat*[] beats;
int beatsOnScreen = 6, offset;
ubyte randomArrows(){
return rand.uniformR(16);
}
ubyte randomArrow(){
return (1 << rand.uniformR(4));
}
int[] lut = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];
}
struct Beat{
bool end;
ubyte arrows; //lrud
double period;
}