Starsector API
Loading...
Searching...
No Matches
GateExplosionScript.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign;
2
3import java.awt.Color;
4
5import org.lwjgl.util.vector.Vector2f;
6
7import com.fs.starfarer.api.EveryFrameScript;
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.LocationAPI;
10import com.fs.starfarer.api.campaign.SectorEntityToken;
11import com.fs.starfarer.api.campaign.StarSystemAPI;
12import com.fs.starfarer.api.impl.campaign.ExplosionEntityPlugin.ExplosionFleetDamage;
13import com.fs.starfarer.api.impl.campaign.ExplosionEntityPlugin.ExplosionParams;
14import com.fs.starfarer.api.impl.campaign.ids.Entities;
15import com.fs.starfarer.api.impl.campaign.ids.Factions;
16import com.fs.starfarer.api.impl.campaign.ids.Tags;
17import com.fs.starfarer.api.util.IntervalUtil;
18import com.fs.starfarer.api.util.Misc;
19
20public class GateExplosionScript implements EveryFrameScript {
21
22 public static class SystemCutOffRemoverScript implements EveryFrameScript {
23 public StarSystemAPI system;
24 public IntervalUtil interval = new IntervalUtil(0.5f, 1.5f);
25 public boolean done;
26 public float elapsed = 0f;
27
28 public SystemCutOffRemoverScript(StarSystemAPI system) {
29 super();
30 this.system = system;
31 }
32 public boolean isDone() {
33 return done;
34 }
35 public boolean runWhilePaused() {
36 return false;
37 }
38
39 public void advance(float amount) {
40 if (done) return;
41
42 float days = Global.getSector().getClock().convertToDays(amount);
43 elapsed += days;
44 interval.advance(days);
45 if (interval.intervalElapsed() && elapsed > 10f) { // make sure gate's already exploded
46 boolean allJPUsable = true;
47 boolean anyJPUsable = false;
48 for (SectorEntityToken jp : system.getJumpPoints()) {
49 allJPUsable &= !jp.getMemoryWithoutUpdate().getBoolean(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY);
50 anyJPUsable |= !jp.getMemoryWithoutUpdate().getBoolean(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY);
51 }
52 //if (allJPUsable) {
53 if (anyJPUsable) {
54 system.removeTag(Tags.SYSTEM_CUT_OFF_FROM_HYPER);
55 done = true;
56 }
57 }
58 }
59
60 }
61
62 public static float UNSTABLE_DAYS_MIN = 200;
63 public static float UNSTABLE_DAYS_MAX = 400;
64
65 protected boolean done = false;
66 protected boolean playedWindup = false;
67 protected SectorEntityToken explosion = null;
68 protected float delay = 0.5f;
69 protected float delay2 = 1f;
70
71 protected SectorEntityToken gate;
72
73
74 public GateExplosionScript(SectorEntityToken gate) {
75 this.gate = gate;
76 //GateEntityPlugin plugin = (GateEntityPlugin) gate.getCustomPlugin();
77
78 // do this immediately so player can't establish a colony between when the gate explosion begins
79 // and when it ends
80 StarSystemAPI system = gate.getStarSystem();
81 if (system != null) {
82 system.addTag(Tags.SYSTEM_CUT_OFF_FROM_HYPER);
83 system.addScript(new SystemCutOffRemoverScript(system));
84 }
85
86 delay = 1.2f; // plus approximately 2 seconds from how long plugin.jitter() takes to build up
87
88 }
89
90
91 public void advance(float amount) {
92 if (done) return;
93
94 if (!playedWindup) {
95 if (gate.isInCurrentLocation()) {
96 Global.getSoundPlayer().playSound("gate_explosion_windup", 1f, 1f, gate.getLocation(), Misc.ZERO);
97 }
98 playedWindup = true;
99 }
100
101
102 GateEntityPlugin plugin = (GateEntityPlugin) gate.getCustomPlugin();
103 plugin.jitter();
104
105 if (plugin.getJitterLevel() > 0.9f) {
106 delay -= amount;
107 }
108 if (delay <= 0 && explosion == null) {
109 //Misc.fadeAndExpire(gate);
110
111 LocationAPI cl = gate.getContainingLocation();
112 Vector2f loc = gate.getLocation();
113 Vector2f vel = gate.getVelocity();
114
115 float size = gate.getRadius() + 2000f;
116 Color color = new Color(255, 165, 100);
117 color = new Color(100, 255, 165);
118 color = new Color(150, 255, 200);
119 color = new Color(100, 200, 150, 255);
120 color = new Color(255, 255, 100, 255);
121 color = new Color(100, 255, 150, 255);
122 //color = new Color(255, 155, 255);
123 //ExplosionParams params = new ExplosionParams(color, cl, loc, size, 1f);
124 ExplosionParams params = new ExplosionParams(color, cl, loc, size, 2f);
125 params.damage = ExplosionFleetDamage.HIGH;
126
127 explosion = cl.addCustomEntity(Misc.genUID(), "Gate Explosion",
128 Entities.EXPLOSION, Factions.NEUTRAL, params);
129 explosion.setLocation(loc.x, loc.y);
130 }
131
132 if (explosion != null) {
133 delay2 -= amount;
134 if (!explosion.isAlive() || delay2 <= 0) {
135 done = true;
136
137 StarSystemAPI system = gate.getStarSystem();
138 if (system != null) {
139 for (SectorEntityToken jp : system.getJumpPoints()) {
140 float days = UNSTABLE_DAYS_MIN + (UNSTABLE_DAYS_MAX - UNSTABLE_DAYS_MIN) * (float) Math.random();
141 jp.getMemoryWithoutUpdate().set(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY, true, days);
142 }
143 }
144 }
145 }
146 }
147
148
149
150
151 public boolean isDone() {
152 return done;
153 }
154
155 public boolean runWhilePaused() {
156 return false;
157 }
158
159}
160
161
162
163
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:43
static SectorAPI getSector()
Definition Global.java:59
SoundAPI playSound(String id, float pitch, float volume, Vector2f loc, Vector2f vel)