/
Instruction.pde
75 lines (66 loc) · 1.56 KB
/
Instruction.pde
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
class Instruction {
String instructions[] = {"left", "right", "up", "down",
"1", "2", "3",
"loop", "if"};
String instruction;
Float x,y,w;
float seed;
int moves = 0;
int flipx = 0;
int flipy = 0;
int drum = 0;
float energy = 0;
float angle = 0;
ArrayList cxs = new ArrayList();
Instruction() {
setInstruction(instructions[int(random(instructions.length))]);
x = random(width);
y = random(height);
seed = random(10000);
flipx = random(1) > 0.5 ? 0 : 1;
flipy = random(1) > 0.5 ? 0 : 1;
drum = int(random(3));
}
int getDrum() {
return(this.drum);
}
void setInstruction(String instruction) {
this.instruction = instruction;
w = textWidth(instruction);
}
void move() {
float amount = energy * 0.25;
energy -= amount;
float dir = angle + noise(seed + (moves * 0.01)) * TWO_PI;
float ox = cos(dir) * amount;
float oy = sin(dir) * amount;
x += ox;
y += oy;
if (x < 0) {
x = 0 - x;
angle += 0 - dir;
}
if (x >= width) {
x = width - (x - width);
angle += PI - dir;
}
if (y < 0) {
y = 0 - y;
angle += (HALF_PI) - dir;
}
if (y >= height) {
y = height - (y -height);
angle += (PI * 1.5) - dir;
}
moves++;
}
void show() {
textSize(30);
//print("show " + instruction + " at " + x + "x" + y);
fill(255);
text(instruction, x + (w/2), y);
}
void addEnergy(float energy) {
this.energy += energy;
}
}