forked from SynapticWall/SynapticWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.pde
88 lines (75 loc) · 2.19 KB
/
Signal.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
76
77
78
79
80
81
82
83
84
85
86
87
88
abstract class Signal extends Drawable {
public int fCurrIndex, fFiringTime;
public float fSpeed, fLength, fDecay, fStrength;
protected int fType, fFinalIndex;
protected Path fPath;
protected Signalable fDest;
protected boolean fFired;
protected float fParam;
Signal() {}
Signal (int type, float speed, float length, float decay, float strength, Path p) {
super(p.getVertex(0).x, p.getVertex(0).y, (type == EPSP) ? EX_COLOR : IN_COLOR);
fType = type;
fSpeed = speed;
fLength = length;
fDecay = decay;
fStrength = strength;
fPath = p;
//fDest is private ?
fDest = p.fDest;
fCurrIndex = 0;
fParam = 0;
fFinalIndex = p.size() - ((gSmoothPaths) ? 4 : 1);
fFiringTime = 0;
fFired = false;
}
public int getType() {
return SIGNAL;
}
public void setIndex(int i) {
fCurrIndex = constrain(i, 0, fFinalIndex);
fLoc = Util.clone(fPath.getVertex(fCurrIndex));
}
protected void fire() {
if (!fFired) {
fDest.onSignal(this);
fFiringTime = millis();
fFired = true;
}
}
protected float advance(int index, float param, PVector p) {
if (index + 1 == fPath.size()) return param;
PVector a = fPath.getVertex(index);
PVector b = fPath.getVertex(index + 1);
if (gSmoothPaths) {
PVector c = fPath.getVertex(index + 2);
PVector d = fPath.getVertex(index + 3);
float x = curvePoint(a.x, b.x, c.x, d.x, param);
float y = curvePoint(a.y, b.y, c.y, d.y, param);
p.set(x, y, 0);
// note: use linear distance as an approximation
return param + fSpeed / PVector.dist(b, c);
}
else {
p.set(lerp(a.x, b.x, param), lerp(a.y, b.y, param), 0);
// note: use linear distance as an approximation
return param + fSpeed / PVector.dist(a, b);
}
}
public void update() {
if (fFired) return;
fParam = advance(fCurrIndex, fParam, fLoc);
if (fParam >= 1.0) {
// Move on to the next segment and reset parameter
fParam = fParam - 1;
fCurrIndex += 1;
if (fCurrIndex >= fFinalIndex) {
fire();
}
}
}
public boolean firingComplete() {
return fFired;
}
public abstract Signal makeCopy(Path p);
}