Starsector API
Loading...
Searching...
No Matches
FlareManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.List;
6import java.util.Random;
7
8import org.lwjgl.util.vector.Vector2f;
9
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.SectorEntityToken;
12import com.fs.starfarer.api.util.FaderUtil;
13import com.fs.starfarer.api.util.IntervalUtil;
14import com.fs.starfarer.api.util.Misc;
15
16public class FlareManager {
17
18 public static interface FlareManagerDelegate {
19 SectorEntityToken getFlareCenterEntity();
20
21 List<Color> getFlareColorRange();
22 float getFlareProbability();
23 float getFlareSkipLargeProbability();
24 int getFlareMinSmallCount();
25 int getFlareMaxSmallCount();
26
27
28 float getFlareOccurrenceAngle();
29 float getFlareOccurrenceArc();
30
31 float getFlareArcMin();
32 float getFlareArcMax();
33 float getFlareFadeInMin();
34 float getFlareFadeInMax();
35 float getFlareFadeOutMin();
36 float getFlareFadeOutMax();
37 float getFlareExtraLengthMultMin();
38 float getFlareExtraLengthMultMax();
39 float getFlareExtraLengthFlatMin();
40 float getFlareExtraLengthFlatMax();
41 float getFlareShortenFlatModMin();
42 float getFlareShortenFlatModMax();
43
44 float getFlareSmallArcMin();
45 float getFlareSmallArcMax();
46 float getFlareSmallFadeInMin();
47 float getFlareSmallFadeInMax();
48 float getFlareSmallFadeOutMin();
49 float getFlareSmallFadeOutMax();
50 float getFlareSmallExtraLengthMultMin();
51 float getFlareSmallExtraLengthMultMax();
52 float getFlareSmallExtraLengthFlatMin();
53 float getFlareSmallExtraLengthFlatMax();
54 float getFlareSmallShortenFlatModMin();
55 float getFlareSmallShortenFlatModMax();
56 }
57
58 public static class Flare {
59 public float direction;
60 public float arc;
61 public float extraLengthMult;
62 public float extraLengthFlat;
63 public float shortenFlatMod;
64 transient public List<Color> colors = new ArrayList<Color>();
65 public String c = null;
66 public FaderUtil fader;
67
68 Object readResolve() {
69 if (c != null) {
70 colors = Misc.colorsFromString(c);
71 } else {
72 colors = new ArrayList<Color>();
73 }
74 return this;
75 }
76
77 Object writeReplace() {
78 c = Misc.colorsToString(colors);
79 return this;
80 }
81 }
82
83 private IntervalUtil flareTracker = new IntervalUtil(0.5f, 1.5f);
84 private List<Flare> flares = new ArrayList<Flare>();
85 private FlareManagerDelegate delegate;
86
87
88 public FlareManager(FlareManagerDelegate delegate) {
89 this.delegate = delegate;
90 }
91
92 Object readResolve() {
93 return this;
94 }
95
96 Object writeReplace() {
97 if (flares != null && flares.isEmpty()) flares = null;
98 return this;
99 }
100
101 public List<Flare> getFlares() {
102 if (flares == null) {
103 flares = new ArrayList<Flare>();
104 }
105 return flares;
106 }
107
108 public void advance(float amount) {
109 float days = Global.getSector().getClock().convertToDays(amount);
110
111 Flare curr = null;
112 if (!getFlares().isEmpty()) {
113 curr = getFlares().get(0);
114 }
115 if (curr != null) {
116 curr.fader.advance(days);
117 if (curr.fader.isFadedOut()) {
118 curr = null;
119 getFlares().remove(0);
120 if (!getFlares().isEmpty()) {
121 getFlares().get(0).fader.fadeIn();
122 }
123 }
124 }
125
126 flareTracker.advance(days);
127 if (flareTracker.intervalElapsed() && getFlares().isEmpty()) {
128 if (Math.random() < delegate.getFlareProbability()) {
130 }
131 }
132 }
133
134 public Flare getActiveFlare() {
135 if (getFlares().isEmpty()) return null;
136 return getFlares().get(0);
137 }
138
139 public boolean isInActiveFlareArc(Vector2f point) {
140 float angle = Misc.getAngleInDegrees(delegate.getFlareCenterEntity().getLocation(), point);
141 return isInActiveFlareArc(angle);
142 }
143
144 public boolean isInActiveFlareArc(SectorEntityToken other) {
145 float angle = Misc.getAngleInDegrees(delegate.getFlareCenterEntity().getLocation(), other.getLocation());
146 return isInActiveFlareArc(angle);
147 }
148
149 public boolean isInActiveFlareArc(float angle) {
150 Flare curr = getActiveFlare();
151 if (curr == null) return false;
152 return Misc.isInArc(curr.direction, curr.arc, angle);
153 }
154
155 public Color getColorForAngle(Color baseColor, float angle) {
156 Flare curr = getActiveFlare();
157 if (curr == null) return baseColor;
158
159 if (!Misc.isInArc(curr.direction, curr.arc, angle)) return baseColor;
160
161 angle = Misc.normalizeAngle(angle);
162
163 float arcStart = curr.direction - curr.arc / 2f;
164 float arcEnd = curr.direction + curr.arc / 2f;
165
166 angle -= arcStart;
167 if (angle < 0) angle += 360f;
168
169 float progress = angle / (arcEnd - arcStart);
170 if (progress < 0) progress = 0;
171 if (progress > 1) progress = 1;
172
173 float numColors = curr.colors.size();
174
175 float fractionalIndex = ((numColors - 1f) * progress);
176 int colorOne = (int) fractionalIndex;
177 int colorTwo = (int) Math.ceil(fractionalIndex);
178
179 float interpProgress = fractionalIndex - (int)fractionalIndex;
180 Color one = curr.colors.get(colorOne);
181 Color two = curr.colors.get(colorTwo);
182
183 Color result = Misc.interpolateColor(one, two, interpProgress);
184 result = Misc.interpolateColor(baseColor, result, curr.fader.getBrightness());
185
186 return result;
187 }
188
189
190 public float getExtraLengthFlat(float angle) {
191 Flare curr = getActiveFlare();
192 if (curr == null) return 0f;
193
194 if (!Misc.isInArc(curr.direction, curr.arc, angle)) return 0f;
195
196 return curr.extraLengthFlat * (float)Math.sqrt(curr.fader.getBrightness());
197 }
198
199 public float getExtraLengthMult(float angle) {
200 Flare curr = getActiveFlare();
201 if (curr == null) return 1f;
202
203 if (!Misc.isInArc(curr.direction, curr.arc, angle)) return 1f;
204
205 return 1f + (curr.extraLengthMult - 1f) * (float)Math.sqrt(curr.fader.getBrightness());
206 }
207
208 public float getShortenMod(float angle) {
209 Flare curr = getActiveFlare();
210 if (curr == null) return 0f;
211
212 if (!Misc.isInArc(curr.direction, curr.arc, angle)) return 0f;
213
214 return curr.shortenFlatMod * (float)Math.sqrt(curr.fader.getBrightness());
215 }
216
217 public float getInnerOffsetMult(float angle) {
218 Flare curr = getActiveFlare();
219 if (curr == null) return 0f;
220
221 if (!Misc.isInArc(curr.direction, curr.arc, angle)) return 0f;
222
223 //return curr.fader.getBrightness();
224 return (float)Math.sqrt(curr.fader.getBrightness());
225 }
226
227 protected void initNewFlareSequence() {
228 getFlares().clear();
229
230 int numSmall = delegate.getFlareMinSmallCount() +
231 (int)Math.ceil((float) (delegate.getFlareMaxSmallCount() - delegate.getFlareMinSmallCount()) * (float) Math.random());
232
233 Flare large = genLargeFlare();
234 for (int i = 0; i < numSmall; i++) {
235 getFlares().add(genSmallFlare(large.direction, large.arc));
236 }
237
238 if (Math.random() > delegate.getFlareSkipLargeProbability()) {
239 getFlares().add(large);
240 }
241
242 if (!getFlares().isEmpty()) {
243 getFlares().get(0).fader.fadeIn();
244 }
245 }
246
247 protected Flare genSmallFlare(float dir, float arc) {
248 Flare flare = new Flare();
249 flare.direction = dir -
250 arc / 2f +
251 (float) Math.random() * arc;
252 flare.arc = delegate.getFlareSmallArcMin() +
253 (delegate.getFlareSmallArcMax() - delegate.getFlareSmallArcMin()) * (float) Math.random();
254 flare.extraLengthFlat = delegate.getFlareSmallExtraLengthFlatMin() +
255 (delegate.getFlareSmallExtraLengthFlatMax() - delegate.getFlareSmallExtraLengthFlatMin()) * (float) Math.random();
256 flare.extraLengthMult = delegate.getFlareSmallExtraLengthMultMin() +
257 (delegate.getFlareSmallExtraLengthMultMax() - delegate.getFlareSmallExtraLengthMultMin()) * (float) Math.random();
258 flare.shortenFlatMod = delegate.getFlareSmallShortenFlatModMin() +
259 (delegate.getFlareSmallShortenFlatModMax() - delegate.getFlareSmallShortenFlatModMin()) * (float) Math.random();
260
261 flare.fader = new FaderUtil(0,
262 delegate.getFlareSmallFadeInMin() +
263 (delegate.getFlareSmallFadeInMax() - delegate.getFlareSmallFadeInMin()) * (float) Math.random(),
264 delegate.getFlareSmallFadeOutMin() +
265 (delegate.getFlareSmallFadeOutMax() - delegate.getFlareSmallFadeOutMin()) * (float) Math.random(),
266 false, true);
267
268 setColors(flare);
269 return flare;
270 }
271
272 protected Flare genLargeFlare() {
273 Flare flare = new Flare();
274 flare.direction = delegate.getFlareOccurrenceAngle() -
275 delegate.getFlareOccurrenceArc() / 2f +
276 (float) Math.random() * delegate.getFlareOccurrenceArc();
277 flare.arc = delegate.getFlareArcMin() +
278 (delegate.getFlareArcMax() - delegate.getFlareArcMin()) * (float) Math.random();
279 flare.extraLengthFlat = delegate.getFlareExtraLengthFlatMin() +
280 (delegate.getFlareExtraLengthFlatMax() - delegate.getFlareExtraLengthFlatMin()) * (float) Math.random();
281 flare.extraLengthMult = delegate.getFlareExtraLengthMultMin() +
282 (delegate.getFlareExtraLengthMultMax() - delegate.getFlareExtraLengthMultMin()) * (float) Math.random();
283 flare.shortenFlatMod = delegate.getFlareShortenFlatModMin() +
284 (delegate.getFlareShortenFlatModMax() - delegate.getFlareShortenFlatModMin()) * (float) Math.random();
285
286 flare.fader = new FaderUtil(0,
287 delegate.getFlareFadeInMin() +
288 (delegate.getFlareFadeInMax() - delegate.getFlareFadeInMin()) * (float) Math.random(),
289 delegate.getFlareFadeOutMin() +
290 (delegate.getFlareFadeOutMax() - delegate.getFlareFadeOutMin()) * (float) Math.random(),
291 false, true);
292
293 flare.direction = Misc.normalizeAngle(flare.direction);
294
295 setColors(flare);
296
297 return flare;
298 }
299
300 protected void setColors(Flare flare) {
301 float colorRangeFraction = flare.arc / delegate.getFlareArcMax();
302 int totalColors = delegate.getFlareColorRange().size();
303 int numColors = Math.round(colorRangeFraction * totalColors);
304 if (numColors < 1) numColors = 1;
305
306 flare.colors.clear();
307 Random r = new Random();
308 int start = 0;
309 if (numColors < totalColors) {
310 start = r.nextInt(totalColors - numColors);
311 }
312 for (int i = start; i < totalColors; i++) {
313 flare.colors.add(delegate.getFlareColorRange().get(i));
314 }
315 }
316}
317
318
319
320
321
322
323
324
325
326
327
328
329
static SectorAPI getSector()
Definition Global.java:59
Color getColorForAngle(Color baseColor, float angle)