forked from elBukkit/EffectLib
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWaveEffect.java
More file actions
200 lines (163 loc) · 5.54 KB
/
WaveEffect.java
File metadata and controls
200 lines (163 loc) · 5.54 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package de.slikey.effectlib.effect;
import java.util.HashSet;
import java.util.Collection;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.util.Vector;
import de.slikey.effectlib.Effect;
import de.slikey.effectlib.EffectType;
import de.slikey.effectlib.EffectManager;
import de.slikey.effectlib.util.MathUtils;
import de.slikey.effectlib.util.VectorUtils;
public class WaveEffect extends Effect {
public Particle mainParticle = Particle.DRIPPING_WATER;
public Particle cloudParticle = Particle.CLOUD;
public Color cloudColor = null;
/**
* Velocity of the wave
* Call velocity.zero() if the wave should be stationary
*/
public Vector velocity = new Vector();
/**
* Caches the Vectors used to build the wave
*/
protected final Collection<Vector> waterCache, cloudCache;
/**
* Amount of particles forming the tube
*/
public int particlesFront = 10;
/**
* Amount of particles forming the back
*/
public int particlesBack = 10;
/**
* Rows to build the wave in the width
*/
public int rows = 20;
/**
* The distance from the origin location to the first point of the wave
*/
public float lengthFront = 1.5F;
/**
* The distance from the origin location to the last point of the wave
*/
public float lengthBack = 3;
/**
* Depth of the parabola tube
*/
public float depthFront = 1;
/**
* Height of the parabola arc forming the back
*/
public float heightBack = 0.5F;
/**
* Height of the wave in blocks
*/
public float height = 2;
/**
* Width of the wave in blocks
*/
public float width = 5;
/**
* Do not mess with the following attributes. They build a cache to gain performance.
*/
protected boolean firstStep = true;
public WaveEffect(EffectManager effectManager) {
super(effectManager);
type = EffectType.REPEATING;
period = 5;
iterations = 50;
waterCache = new HashSet<>();
cloudCache = new HashSet<>();
}
@Override
public void reset() {
firstStep = true;
}
/**
* Call this method when you change anything related to the creation of the wave
*/
public void invalidate(Location location) {
firstStep = false;
waterCache.clear();
cloudCache.clear();
Vector s1 = new Vector(-lengthFront, 0, 0);
Vector s2 = new Vector(lengthBack, 0, 0);
Vector h = new Vector(-0.5 * lengthFront, height, 0);
Vector n1, n2, n_s1ToH, n_s2ToH, c1, c2, s1ToH, s2ToH;
float len_s1ToH, len_s2ToH, yaw;
s1ToH = h.clone().subtract(s1);
c1 = s1.clone().add(s1ToH.clone().multiply(0.5));
len_s1ToH = (float) s1ToH.length();
n_s1ToH = s1ToH.clone().multiply(1F / len_s1ToH);
n1 = new Vector(s1ToH.getY(), -s1ToH.getX(), 0).normalize();
if (n1.getX() < 0) n1.multiply(-1);
s2ToH = h.clone().subtract(s2);
c2 = s2.clone().add(s2ToH.clone().multiply(0.5));
len_s2ToH = (float) s2ToH.length();
n_s2ToH = s2ToH.clone().multiply(1F / len_s2ToH);
n2 = new Vector(s2ToH.getY(), -s2ToH.getX(), 0).normalize();
if (n2.getX() < 0) n2.multiply(-1);
yaw = (-location.getYaw() + 90) * MathUtils.degreesToRadians;
float ratio;
float x;
float y;
float z;
Vector v;
Vector vec;
for (int i = 0; i < particlesFront; i++) {
ratio = (float) i / particlesFront;
x = (ratio - 0.5F) * len_s1ToH;
y = (float) (-depthFront / Math.pow((len_s1ToH / 2), 2) * Math.pow(x, 2) + depthFront);
v = c1.clone();
v.add(n_s1ToH.clone().multiply(x));
v.add(n1.clone().multiply(y));
for (int j = 0; j < rows; j++) {
z = ((float) j / rows - 0.5F) * width;
vec = v.clone().setZ(v.getZ() + z);
VectorUtils.rotateAroundAxisY(vec, yaw);
if (i == 0 || i == particlesFront - 1) cloudCache.add(vec);
else waterCache.add(vec);
}
}
for (int i = 0; i < particlesBack; i++) {
ratio = (float) i / particlesBack;
x = (ratio - 0.5F) * len_s2ToH;
y = (float) (-heightBack / Math.pow((len_s2ToH / 2), 2) * Math.pow(x, 2) + heightBack);
v = c2.clone();
v.add(n_s2ToH.clone().multiply(x));
v.add(n2.clone().multiply(y));
for (int j = 0; j < rows; j++) {
z = ((float) j / rows - 0.5F) * width;
vec = v.clone().setZ(v.getZ() + z);
VectorUtils.rotateAroundAxisY(vec, yaw);
if (i == particlesFront - 1) cloudCache.add(vec);
else waterCache.add(vec);
}
}
}
@Override
public void onRun() {
Location location = getLocation();
if (location == null) {
cancel();
return;
}
if (firstStep) {
velocity.copy(location.getDirection().setY(0).normalize().multiply(0.2));
invalidate(location);
}
location.add(velocity);
for (Vector v : cloudCache) {
location.add(v);
display(cloudParticle, location, cloudColor, 0, 1);
location.subtract(v);
}
for (Vector v : waterCache) {
location.add(v);
display(mainParticle, location);
location.subtract(v);
}
}
}