Starsector API
Loading...
Searching...
No Matches
GBIGenerateSlipstream.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.ghosts;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.EveryFrameScript;
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.campaign.CampaignTerrainAPI;
11import com.fs.starfarer.api.impl.campaign.ids.Tags;
12import com.fs.starfarer.api.impl.campaign.ids.Terrain;
13import com.fs.starfarer.api.impl.campaign.velfield.SlipstreamTerrainPlugin2;
14import com.fs.starfarer.api.impl.campaign.velfield.SlipstreamTerrainPlugin2.SlipstreamParams2;
15import com.fs.starfarer.api.impl.campaign.velfield.SlipstreamTerrainPlugin2.SlipstreamSegment;
16import com.fs.starfarer.api.util.Misc;
17
19
20 public static interface GhostBehaviorWithSlipstream {
21 void setSlipstream(SlipstreamTerrainPlugin2 plugin);
22 }
23
24 protected boolean addedScript = false;
25 protected float minWidth;
26 protected float maxWidth;
27 protected int burnLevel;
28 protected SensorGhost ghost;
30 protected float widenRate = 50f;
31 protected int maxSegments = 20;
32 protected float duration;
33 public GBIGenerateSlipstream(float minWidth, float maxWidth, int burnLevel, float widenRate, int maxSegments, float duration) {
34 super(0f);
35 this.minWidth = minWidth;
36 this.maxWidth = maxWidth;
37 this.burnLevel = burnLevel;
38 this.widenRate = widenRate;
39 this.maxSegments = maxSegments;
40 this.duration = duration;
41 }
42
43 @Override
45 return false;
46 }
47
48 public void advance(float amount, SensorGhost ghost, GhostBehavior behavior) {
49 this.behavior = behavior;
50 if (!addedScript) {
51 if (ghost != null && ghost.getEntity() != null && ghost.getEntity().getContainingLocation() != null) {
52 this.ghost = ghost;
53 //ghost.getEntity().getContainingLocation().addScript(this);
54 ghost.getEntity().addScript(this);
55 ghost.getEntity().addTag(Tags.UNAFFECTED_BY_SLIPSTREAM);
56 }
57 addedScript = true;
58 }
59 }
60
61
62 public boolean isDone() {
63 return duration <= 0;
64 }
65
66 public boolean runWhilePaused() {
67 return false;
68 }
69
70 protected CampaignTerrainAPI slipstream = null;
71 protected SlipstreamTerrainPlugin2 plugin = null;
72 protected Vector2f prev = null;
73
74
75 public void advance(float amount) {
76 float days = Global.getSector().getClock().convertToDays(amount);
77 duration -= days;
78 if (ghost.getEntity() == null || ghost.getEntity().getContainingLocation() == null || duration <= 0f) {
79 if (!plugin.isDespawning()) {
80 plugin.despawn(0f, 1f + Misc.random.nextFloat(), Misc.random);
81 }
82 return;
83 }
84 Vector2f loc = ghost.getEntity().getLocation();
85 boolean forceAdd = false;
86 if (slipstream == null) {
87 SlipstreamParams2 params = new SlipstreamParams2();
88
89 params.burnLevel = burnLevel;
90 params.widthForMaxSpeed = minWidth + widenRate * 5f;
91 //params.widthForMaxSpeedMaxMult = 1f; // no faster than base burn, otherwise will catch up with ghost too easily
92 params.minSpeed = Misc.getSpeedForBurnLevel(params.burnLevel - 5);
93 params.maxSpeed = Misc.getSpeedForBurnLevel(params.burnLevel + 5);
94 params.lineLengthFractionOfSpeed = 0.25f * Math.max(0.25f, Math.min(1f, 30f / (float) params.burnLevel));
95
96 //slipstream = (CampaignTerrainAPI) Global.getSector().getCurrentLocation().addTerrain(Terrain.SLIPSTREAM, params);
97 slipstream = (CampaignTerrainAPI) ghost.getEntity().getContainingLocation().addTerrain(Terrain.SLIPSTREAM, params);
98 slipstream.setLocation(loc.x, loc.y);
99 plugin = (SlipstreamTerrainPlugin2) slipstream.getPlugin();
100 plugin.setDynamic(true);
101 prev = new Vector2f(loc);
102 forceAdd = true;
103
104 if (behavior instanceof GhostBehaviorWithSlipstream) {
105 GhostBehaviorWithSlipstream b = (GhostBehaviorWithSlipstream) behavior;
106 b.setSlipstream(plugin);
107 }
108 }
109 //ghost.getMovement().getLocation()
110
111 //System.out.println("Location: [" + (int)ghost.getEntity().getLocation().x + "," + (int)ghost.getEntity().getLocation().y + "] (from GBIGenerateSlipstream)");
112
113 float distPerSegment = 300f;
114
115 float dist = Misc.getDistance(loc, prev);
116 if (dist >= distPerSegment || forceAdd) {
117
118// if (plugin.getSegments().size() > 0 &&
119// plugin.getSegments().get(plugin.getSegments().size() - 1).loc.y < loc.y) {
120// System.out.println("ADDING BAD STREAM SEGMENT at [" + (int)loc.x + ", " + (int)loc.y + "] (from GBIGenerateSlipstream)");
121// } else {
122// System.out.println("ADDING STREAM SEGMENT at [" + (int)loc.x + ", " + (int)loc.y + "] (from GBIGenerateSlipstream)");
123// }
124
125 float width = minWidth + (maxWidth - minWidth) * Misc.random.nextFloat();
126 plugin.addSegment(loc, width);
127 prev = new Vector2f(loc);
128
129 List<SlipstreamSegment> segments = plugin.getSegments();
130 if (segments.size() == 1) {
131 segments.get(0).bMult = 0f;
132 segments.get(0).fader.forceOut();
133 } else {
134 segments.get(segments.size() - 1).fader.forceOut();
135 //segments.get(segments.size() - 1).bMult = 0.67f;
136 }
137
138 List<SlipstreamSegment> remove = new ArrayList<SlipstreamSegment>();
139 for (int i = 0; i < segments.size() - maxSegments - 1; i++) {
140 SlipstreamSegment curr = segments.get(i);
141 SlipstreamSegment next = segments.get(i + 1);
142 curr.fader.setDurationOut(3f);
143 curr.fader.fadeOut();
144 if (curr.fader.isFadedOut() && next.fader.isFadedOut()) {
145 remove.add(curr);
146 }
147 }
148 // don't do it - messes up texture offsets
149 //segments.removeAll(remove);
150 plugin.recompute();
151 }
152
153
154 List<SlipstreamSegment> segments = plugin.getSegments();
155 float fadeInDist = Math.min(minWidth * 4f, distPerSegment * maxSegments / 4f);
156 for (int i = Math.max(0, segments.size() - maxSegments); i < segments.size() - 1; i++) {
157 SlipstreamSegment curr = segments.get(i);
158 SlipstreamSegment next = segments.get(i + 1);
159
160 if (!curr.fader.isFadedOut()) {
161 dist = Misc.getDistance(ghost.getEntity().getLocation(), curr.loc);
162 float b = dist / fadeInDist;
163 if (b < 0) b = 0;
164 if (b > 1) b = 1;
165 curr.bMult = b;
166 }
167
168
169 if (next.fader.getBrightness() == 0 && !next.fader.isFadingOut()) {
170 float durIn = distPerSegment / Math.max(ghost.getEntity().getVelocity().length(), 1f);
171 if (durIn > 2f) durIn = 2f;
172 durIn *= 2f;
173 curr.fader.setDurationIn(durIn);
174 curr.fader.fadeIn();
175 }
176
177 curr.width += widenRate * amount;
178// if (curr.width > 100f) {
179// curr.width -= widenRate * amount;
180// }
181 }
182 }
183
184
185}
186
187
188
189
static SectorAPI getSector()
Definition Global.java:59
void advance(float amount, SensorGhost ghost, GhostBehavior behavior)
boolean shouldInterruptBehavior(SensorGhost ghost, GhostBehavior behavior)
GBIGenerateSlipstream(float minWidth, float maxWidth, int burnLevel, float widenRate, int maxSegments, float duration)