Starsector API
Loading...
Searching...
No Matches
QuadParticles.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.eventide;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.List;
7
8import org.lwjgl.opengl.GL11;
9import org.lwjgl.util.vector.Vector2f;
10
11import com.fs.starfarer.api.util.Misc;
12
13public class QuadParticles {
14
15 public static class QuadParticleData {
16 public Vector2f loc = new Vector2f();
17 public Vector2f vel = new Vector2f();
18 public Color color;
19 public float size;
20 public float elapsed;
21 public float maxDur;
22 public float fadeTime;
23 public float floorMod = 0;
24 }
25
26 public List<QuadParticleData> particles = new ArrayList<QuadParticleData>();
27 public float gravity = 0f;
28 public float floor = -10000000f;
29 public float maxFloorMod = 0f;
30 public float floorFriction = 1f; // fraction of speed lost per second
31 public boolean additiveBlend = false;
32
33 public Color minColor = null;
34 public Color maxColor = null;
35 public float minDur, maxDur;
36 public float minSize, maxSize;
37 public float fadeTime = 0.5f;
38
39 public void addParticle(float x, float y, float dx, float dy) {
40 float size = (float) Math.random() * (maxSize - minSize) + minSize;
41 addParticle(x, y, dx, dy, size);
42 }
43 public void addParticle(float x, float y, float dx, float dy, float size) {
44 float r1 = (float) Math.random();
45 int r = (int) Math.round(Misc.interpolate(minColor.getRed(), maxColor.getRed(), r1));
46 if (r < 0) r = 0;
47 if (r > 255) r = 255;
48
49 r1 = (float) Math.random();
50 int g = (int) Math.round(Misc.interpolate(minColor.getGreen(), maxColor.getGreen(), r1));
51 if (g < 0) g = 0;
52 if (g > 255) g = 255;
53
54 r1 = (float) Math.random();
55 int b = (int) Math.round(Misc.interpolate(minColor.getBlue(), maxColor.getBlue(), r1));
56 if (b < 0) b = 0;
57 if (b > 255) b = 255;
58
59 r1 = (float) Math.random();
60 int a = (int) Math.round(Misc.interpolate(minColor.getAlpha(), maxColor.getAlpha(), r1));
61 if (a < 0) a = 0;
62 if (a > 255) a = 255;
63
64 addParticle(x, y, dx, dy, size, new Color(r, g, b, a));
65 }
66
67 public void addParticle(float x, float y, float dx, float dy, float size, Color color) {
68 float dur = (float) Math.random() * (maxDur - minDur) + minDur;
69 addParticle(x, y, dx, dy, size, color, dur, fadeTime);
70 }
71 public void addParticle(float x, float y, float dx, float dy, float size, Color color, float maxDur) {
72 addParticle(x, y, dx, dy, size, color, maxDur, fadeTime);
73 }
74 public void addParticle(float x, float y, float dx, float dy, float size, Color color, float maxDur, float fadeTime) {
75 QuadParticleData p = new QuadParticleData();
76 p.loc.x = x;
77 p.loc.y = y;
78 p.vel.x = dx;
79 p.vel.y = dy;
80 p.size = size;
81 p.color = color;
82 p.maxDur = maxDur;
83 p.fadeTime = fadeTime;
84 p.floorMod = (float) Math.random() * maxFloorMod;
85 particles.add(p);
86 }
87
88 public boolean isParticleOnFloor(QuadParticleData curr) {
89 return curr.loc.y <= floor + curr.floorMod;
90 }
91
92 public void advance(float amount) {
93 Iterator<QuadParticleData> iter = particles.iterator();
94 while (iter.hasNext()) {
95 QuadParticleData curr = iter.next();
96 curr.elapsed += amount;
97
98 if (gravity > 0) {
99 curr.vel.y -= gravity * amount;
100 }
101
102 curr.loc.x += curr.vel.x * amount;
103 curr.loc.y += curr.vel.y * amount;
104 if (curr.loc.y < floor + curr.floorMod) {
105 curr.loc.y = floor + curr.floorMod;
106
107 if (floorFriction > 0) {
108 curr.vel.scale(Math.max(0, (1f - floorFriction * amount)));
109 }
110 }
111
112 if (curr.elapsed > curr.maxDur) {
113 iter.remove();
114 }
115 }
116 }
117
118 public void render(float alphaMult, boolean onFloor) {
119 GL11.glDisable(GL11.GL_TEXTURE_2D);
120 GL11.glEnable(GL11.GL_BLEND);
121
122 if (additiveBlend) {
123 GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
124 } else {
125 GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
126 }
127
128 for (QuadParticleData p : particles) {
129 if (isParticleOnFloor(p) != onFloor) continue;
130
131 float a = alphaMult;
132 float left = p.maxDur - p.elapsed;
133 if (left < 0) left = 0;
134 if (left < p.fadeTime) {
135 a *= left / p.fadeTime;
136 }
137 Misc.renderQuad(p.loc.x - p.size/2f, p.loc.y - p.size/2f, p.size, p.size, p.color, a);
138 }
139 }
140
141
142 public boolean isDone() {
143 return particles.isEmpty();
144 }
145
146
147}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
void addParticle(float x, float y, float dx, float dy, float size, Color color, float maxDur, float fadeTime)
void addParticle(float x, float y, float dx, float dy, float size, Color color)
void addParticle(float x, float y, float dx, float dy, float size, Color color, float maxDur)
void addParticle(float x, float y, float dx, float dy)
void addParticle(float x, float y, float dx, float dy, float size)