Starsector API
Loading...
Searching...
No Matches
AsteroidImpact.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
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.AsteroidAPI;
10import com.fs.starfarer.api.campaign.CampaignFleetAPI;
11import com.fs.starfarer.api.fleet.FleetMemberAPI;
12import com.fs.starfarer.api.util.Misc;
13import com.fs.starfarer.api.util.WeightedRandomPicker;
14
15public class AsteroidImpact implements EveryFrameScript {
16
17 //public static float SAFE_BURN_LEVEL = 5.01f;
18// public static float IMPACT_SPEED_DELTA = Global.getSettings().getSpeedPerBurnLevel();
19 public static float DURATION_SECONDS = 0.2f;
20
21 protected CampaignFleetAPI fleet;
22 protected float elapsed;
23// protected float angle;
24// protected float impact = IMPACT_SPEED_DELTA;
25 protected Vector2f dV;
26
27 public AsteroidImpact(CampaignFleetAPI fleet, boolean dealDamage) {
28 this.fleet = fleet;
29
30 //System.out.println("ADDING IMPACT TO " + fleet.getName() + " " + fleet.getId());
31
32 Vector2f v = fleet.getVelocity();
33 float angle = Misc.getAngleInDegrees(v);
34 float speed = v.length();
35 if (speed < 10) angle = fleet.getFacing();
36
37 float mult = Misc.getFleetRadiusTerrainEffectMult(fleet);
38
39 //float arc = 120f;
40 float arc = 120f - 60f * mult; // larger fleets suffer more direct collisions that slow them down more
41
42 angle += (float) Math.random() * arc - arc/2f;
43 angle += 180f;
44
45 //if (fleet.getCurrBurnLevel() <= SAFE_BURN_LEVEL || mult <= 0) {
46 if (Misc.isSlowMoving(fleet) || mult <= 0) {
48 dV = new Vector2f();
49 } else if (fleet.isInCurrentLocation()) {
50 if (dealDamage) {
51 WeightedRandomPicker<FleetMemberAPI> targets = new WeightedRandomPicker<FleetMemberAPI>();
52 for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
53 float w = 1f;
54 switch (member.getHullSpec().getHullSize()) {
55 case CAPITAL_SHIP: w = 20f; break;
56 case CRUISER: w = 10f; break;
57 case DESTROYER: w = 5f; break;
58 case FRIGATE: w = 1f; break;
59 }
60 targets.add(member, w);
61 }
62
63 FleetMemberAPI member = targets.pick();
64 if (member != null) {
65 float damageMult = fleet.getCurrBurnLevel() - Misc.getGoSlowBurnLevel(fleet);
66 if (damageMult < 1) damageMult = 1;
67 Misc.applyDamage(member, null, damageMult, true, "asteroid_impact", "Asteroid impact",
68 true, null, member.getShipName() + " suffers damage from an asteroid impact");
69 } else {
70 dealDamage = false;
71 }
72 }
73
74 if (!dealDamage && fleet.isPlayerFleet()) {
75 Global.getSector().getCampaignUI().addMessage("Asteroid impact on drive bubble", Misc.getNegativeHighlightColor());
76 }
77
78 Vector2f test = Global.getSector().getPlayerFleet().getLocation();
79 float dist = Misc.getDistance(test, fleet.getLocation());
81// float volumeMult = 1f - (dist / HyperspaceTerrainPlugin.STORM_STRIKE_SOUND_RANGE);
82// volumeMult = (float) Math.sqrt(volumeMult);
83 //volumeMult *= 0.75f;
84 float volumeMult = 0.75f;
85 volumeMult *= 0.5f + 0.5f * mult;
86 if (volumeMult > 0) {
87 if (dealDamage) {
88 Global.getSoundPlayer().playSound("hit_heavy", 1f, 1f * volumeMult, fleet.getLocation(), Misc.ZERO);
89 } else {
90 Global.getSoundPlayer().playSound("hit_shield_heavy_gun", 1f, 1f * volumeMult, fleet.getLocation(), Misc.ZERO);
91 }
92 }
93 }
94
95// if (fleet.isPlayerFleet()) {
96// System.out.println("wefwefwef");
97// }
98 //mult = 2f;
99// if (fleet.isPlayerFleet()) {
100// System.out.println("SPAWNED ---------");
101// }
102
103// int num = (int) (6f + (float) Math.random() * 5f);
104// for (int i = 0; i < num; i++) {
105// angle = angle + (float) Math.random() * 30f - 15f;
106// float size = 10f + (float) Math.random() * 6f;
107// float size = 10f + (float) Math.random() * 6f;
108// size = 4f + 4f * (float) Math.random();
109//
110// AsteroidAPI asteroid = fleet.getContainingLocation().addAsteroid(size);
111// asteroid.setFacing((float) Math.random() * 360f);
112// Vector2f av = Misc.getUnitVectorAtDegreeAngle(angle + 180f);
113// av.scale(fleet.getVelocity().length() + (20f + 20f * (float) Math.random()) * mult);
114// asteroid.getVelocity().set(av);
115// Vector2f al = Misc.getUnitVectorAtDegreeAngle(angle + 180f);
116// //al.scale(fleet.getRadius() + asteroid.getRadius());
117// al.scale(fleet.getRadius());
118// Vector2f.add(al, fleet.getLocation(), al);
119// asteroid.setLocation(al.x, al.y);
120//
121// float sign = Math.signum(asteroid.getRotation());
122// asteroid.setRotation(sign * (50f + 50f * (float) Math.random()));
123//
124// Misc.fadeInOutAndExpire(asteroid, 0.2f, 1.5f + 1f * (float) Math.random(), 1f);
125// }
126
127 float size = 10f + (float) Math.random() * 6f;
128 size *= 0.67f;
129 AsteroidAPI asteroid = fleet.getContainingLocation().addAsteroid(size);
130 asteroid.setFacing((float) Math.random() * 360f);
131 Vector2f av = Misc.getUnitVectorAtDegreeAngle(angle + 180f);
132 av.scale(fleet.getVelocity().length() + (20f + 20f * (float) Math.random()) * mult);
133 asteroid.getVelocity().set(av);
134 Vector2f al = Misc.getUnitVectorAtDegreeAngle(angle + 180f);
135 //al.scale(fleet.getRadius() + asteroid.getRadius());
136 al.scale(fleet.getRadius());
137 Vector2f.add(al, fleet.getLocation(), al);
138 asteroid.setLocation(al.x, al.y);
139
140 float sign = Math.signum(asteroid.getRotation());
141 asteroid.setRotation(sign * (50f + 50f * (float) Math.random()));
142
143 Misc.fadeInOutAndExpire(asteroid, 0.2f, 1f + 1f * (float) Math.random(), 1f);
144
145 //mult = 1f;
146 Vector2f iv = fleet.getVelocity();
147 iv = new Vector2f(iv);
148 iv.scale(0.7f);
149 float glowSize = 100f + 100f * mult + 50f * (float) Math.random();
150 Color color = new Color(255, 165, 100, 255);
151 Misc.addHitGlow(fleet.getContainingLocation(), al, iv, glowSize, color);
152 }
153
154 dV = Misc.getUnitVectorAtDegreeAngle(angle);
155
156 float impact = speed * 1f * (0.5f + mult * 0.5f);
157 dV.scale(impact);
158 dV.scale(1f / DURATION_SECONDS);
159
160// if (fleet.isPlayerFleet()) {
161// fleet.addFloatingText("Impact!", Misc.getNegativeHighlightColor(), 0.5f);
162// }
163 }
164
165 public void advance(float amount) {
166
167 fleet.setOrbit(null);
168
169// if (fleet.isPlayerFleet()) {
170// System.out.println("wefwefwef");
171// }
172
173 Vector2f v = fleet.getVelocity();
174 fleet.setVelocity(v.x + dV.x * amount, v.y + dV.y * amount);
175
176// Vector2f dir = Misc.getUnitVectorAtDegreeAngle(angle);
177//
178// //dir.scale(IMPACT_SPEED_DELTA * mult * amount * 75f * (0.5f + (float) Math.random() * 0.5f));
179//
180// float mult = Misc.getFleetRadiusTerrainEffectMult(fleet);
181// mult = 1f;
186// dir.scale(IMPACT_SPEED_DELTA * mult * amount *
187// (Math.min(20f, fleet.getCurrBurnLevel()) * 50f) * (0.5f + (float) Math.random() * 0.5f));
188//
189// Vector2f v = fleet.getVelocity();
190// fleet.setVelocity(v.x + dir.x, v.y + dir.y);
191//
194
195 elapsed += amount;
196
197 }
198
199 public boolean isDone() {
200 return elapsed >= DURATION_SECONDS;
201 }
202
203 public boolean runWhilePaused() {
204 return false;
205 }
206
207}
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:43
static SectorAPI getSector()
Definition Global.java:59
AsteroidImpact(CampaignFleetAPI fleet, boolean dealDamage)
void setLocation(float x, float y)
SoundAPI playSound(String id, float pitch, float volume, Vector2f loc, Vector2f vel)