Support for unit missiles

This commit is contained in:
Anuken
2021-12-07 16:58:11 -05:00
parent f5e9df1265
commit 0b7d8f371e
16 changed files with 276 additions and 77 deletions

View File

@@ -10,7 +10,7 @@ public class Trail{
public int length;
private final FloatSeq points;
private float lastX = -1, lastY = -1, lastAngle = -1, counter = 0f;
private float lastX = -1, lastY = -1, lastAngle = -1, counter = 0f, lastW = 0f;
public Trail(int length){
this.length = length;
@@ -26,6 +26,10 @@ public class Trail{
return out;
}
public float width(){
return lastW;
}
public void clear(){
points.clear();
}
@@ -49,23 +53,43 @@ public class Trail{
public void draw(Color color, float width){
Draw.color(color);
float[] items = points.items;
float lx = lastX, ly = lastY, lastAngle = this.lastAngle;
float lastAngle = this.lastAngle;
float size = width / (points.size / 3);
for(int i = 0; i < points.size - 3; i+= 3){
float x1 = items[i], y1 = items[i + 1], w1 = items[i + 2],
x2 = items[i + 3], y2 = items[i + 4], w2 = items[i + 5];
float size = width / (points.size/3);
float z1 = lastAngle;
float z2 = -Angles.angleRad(x2, y2, lx, ly);
for(int i = 0; i < points.size; i += 3){
float x1 = items[i], y1 = items[i + 1], w1 = items[i + 2];
float x2, y2, w2;
//last position is always lastX/Y/W
if(i < points.size - 3){
x2 = items[i + 3];
y2 = items[i + 4];
w2 = items[i + 5];
}else{
x2 = lastX;
y2 = lastY;
w2 = lastW;
}
float z2 = -Angles.angleRad(x1, y1, x2, y2);
//end of the trail (i = 0) has the same angle as the next.
float z1 = i == 0 ? z2 : lastAngle;
if(w1 <= 0.001f || w2 <= 0.001f) continue;
float cx = Mathf.sin(z1) * i/3f * size * w1, cy = Mathf.cos(z1) * i/3f * size * w1,
nx = Mathf.sin(z2) * (i/3f + 1) * size * w2, ny = Mathf.cos(z2) * (i/3f + 1) * size * w2;
Fill.quad(x1 - cx, y1 - cy, x1 + cx, y1 + cy, x2 + nx, y2 + ny, x2 - nx, y2 - ny);
float
cx = Mathf.sin(z1) * i/3f * size * w1,
cy = Mathf.cos(z1) * i/3f * size * w1,
nx = Mathf.sin(z2) * (i/3f + 1) * size * w2,
ny = Mathf.cos(z2) * (i/3f + 1) * size * w2;
Fill.quad(
x1 - cx, y1 - cy,
x1 + cx, y1 + cy,
x2 + nx, y2 + ny,
x2 - nx, y2 - ny
);
lastAngle = z2;
lx = x2;
ly = y2;
}
Draw.reset();
@@ -89,19 +113,21 @@ public class Trail{
/** Adds a new point to the trail at intervals. */
public void update(float x, float y, float width){
if((counter += Time.delta) >= 0.99f){
//TODO fix longer trails at low FPS
if((counter += Time.delta) >= 1f){
if(points.size > length*3){
points.removeRange(0, 2);
}
lastAngle = -Angles.angleRad(x, y, lastX, lastY);
points.add(x, y, width);
lastX = x;
lastY = y;
counter = 0f;
counter %= 1f;
}
//update last position regardless, so it joins
lastAngle = -Angles.angleRad(x, y, lastX, lastY);
lastX = x;
lastY = y;
lastW = width;
}
}