Starsector API
Loading...
Searching...
No Matches
GravityPullEffect.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.combat;
2
3import java.util.Iterator;
4import java.util.List;
5
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
10import com.fs.starfarer.api.combat.CombatEntityAPI;
11import com.fs.starfarer.api.combat.ShipAPI;
12import com.fs.starfarer.api.input.InputEventAPI;
13import com.fs.starfarer.api.util.Misc;
14
15public class GravityPullEffect extends BaseEveryFrameCombatPlugin {
16 protected float maxForce;
17 protected float duration = 0.05f;
18 protected float elapsed = 0f;
19 protected CombatEntityAPI source;
20 protected float range;
21
22 public GravityPullEffect(CombatEntityAPI source, float range, float maxForce) {
23 this.source = source;
24 this.range = range;
25 this.maxForce = maxForce;
26 duration = 0.05f;
27 }
28
29
30
31
32 @Override
33 public void advance(float amount, List<InputEventAPI> events) {
34 if (Global.getCombatEngine().isPaused()) return;
35
36 elapsed += amount;
37
38 Iterator<Object> iter = Global.getCombatEngine().getAllObjectGrid().getCheckIterator(source.getLocation(),
39 range * 2f, range * 2f);
40
41 while (iter.hasNext()) {
42 Object o = iter.next();
43 if (o == source) continue;
44 if (!(o instanceof CombatEntityAPI)) continue;
45 if (!(o instanceof ShipAPI)) continue;
46// if (!(o instanceof MissileAPI)) continue;
47// if (!(o instanceof DamagingProjectileAPI)) continue;
48// if (!(o instanceof AsteroidAPI)) continue;
49
50 CombatEntityAPI other = (CombatEntityAPI) o;
51 if (other.getMass() <= 0) continue;
52 applyForce(other, amount);
53 }
54
55
56 if (elapsed > duration) {
57 Global.getCombatEngine().removePlugin(this);
58 }
59 }
60
61 protected void applyForce(CombatEntityAPI other, float amount) {
62 float dist = Misc.getDistance(source.getLocation(), other.getLocation());
63 dist -= other.getCollisionRadius();
64 if (dist > range) return;
65
66 float fTime = 0.0f + 1f * (Math.min(1f, elapsed / duration));
67 float fDist = 0.25f + 0.75f * Math.min(1f, (1f - dist / range));
68 float fConstant = 20f;
69
70 float acceleration = maxForce / other.getMass() * fTime * fDist * fConstant;
71
72 Vector2f dir = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(other.getLocation(), source.getLocation()));
73 dir.scale(acceleration);
74
75 other.getVelocity().x += dir.x * amount;
76 other.getVelocity().y += dir.y * amount;
77 }
78
79// public class HyperStormBoost implements EveryFrameScript {
80//
81// public static float MAX_BURN = Global.getSettings().getFloat("maxStormStrikeBurn");
82// public static float STORM_SPEED_BURST = Global.getSettings().getSpeedPerBurnLevel() * 50f;
83// public static float DURATION_SECONDS = 1f;
84//
85// protected CampaignFleetAPI fleet;
86// protected float elapsed;
87// protected float angle;
88// protected CellStateTracker cell;
89//
90// public HyperStormBoost(CellStateTracker cell, CampaignFleetAPI fleet) {
91// this.cell = cell;
92// this.fleet = fleet;
93//
94// DURATION_SECONDS = 1.25f;
95// STORM_SPEED_BURST = Global.getSettings().getSpeedPerBurnLevel() * 75f;
98//
99// if (Misc.getHyperspaceTerrain().getPlugin() instanceof HyperspaceTerrainPlugin) {
100// HyperspaceTerrainPlugin hyper = (HyperspaceTerrainPlugin) Misc.getHyperspaceTerrain().getPlugin();
101//
102// float x = hyper.getEntity().getLocation().x;
103// float y = hyper.getEntity().getLocation().y;
104// float size = hyper.getTileSize();
105//
106// float w = hyper.getTiles().length * size;
107// float h = hyper.getTiles()[0].length * size;
108//
109// x -= w/2f;
110// y -= h/2f;
111//
112// float tx = x + cell.i * size + size/2f;
113// float ty = y + cell.j * size + size/2f;
114//
115// angle = Misc.getAngleInDegrees(new Vector2f(tx, ty), fleet.getLocation());
116//
117// Vector2f v = fleet.getVelocity();
118// float angle2 = Misc.getAngleInDegrees(v);
119// float speed = v.length();
120// if (speed < 10) angle2 = fleet.getFacing();
121//
122// float bestAngleAt = Global.getSettings().getBaseTravelSpeed() + Global.getSettings().getSpeedPerBurnLevel() * 20f;
123// float mult = 0.5f + 0.4f * speed / bestAngleAt;
124// if (mult < 0.5f) mult = 0.5f;
125// if (mult > 0.9f) mult = 0.9f;
126//
127// angle += Misc.getClosestTurnDirection(angle, angle2) * Misc.getAngleDiff(angle, angle2) * mult;
128// }
129// }
130//
131// public void advance(float amount) {
132// elapsed += amount;
133//
134// Vector2f boost = Misc.getUnitVectorAtDegreeAngle(angle);
135//
136// float mult = 1f;
137// mult = 1f - elapsed / DURATION_SECONDS;
138// mult *= Math.pow(Math.min(1f, elapsed / 0.25f), 2f);
139// if (mult < 0) mult = 0;
140// if (mult > 1) mult = 1;
141// boost.scale(STORM_SPEED_BURST * amount * mult);
142//
143// Vector2f v = fleet.getVelocity();
144//
145// if (fleet.getCurrBurnLevel() < MAX_BURN) {
146// fleet.setVelocity(v.x + boost.x, v.y + boost.y);
147// }
148//
149// float angleHeading = Misc.getAngleInDegrees(v);
150// if (v.length() < 10) angleHeading = fleet.getFacing();
151//
152// boost = Misc.getUnitVectorAtDegreeAngle(angleHeading);
153// boost.negate();
154// if (boost.length() >= 1) {
155// float durIn = 1f;
156// float durOut = 3f;
157// float intensity = 1f;
158// float sizeNormal = 5f + 30f * intensity;
159// String modId = "boost " + cell.i + cell.j * 100;
160// Color glowColor = new Color(100, 100, 255, 75);
161// for (FleetMemberViewAPI view : fleet.getViews()) {
162// view.getWindEffectDirX().shift(modId, boost.x * sizeNormal, durIn, durOut, 1f);
163// view.getWindEffectDirY().shift(modId, boost.y * sizeNormal, durIn, durOut, 1f);
164// view.getWindEffectColor().shift(modId, glowColor, durIn, durOut, intensity);
165// }
166// }
167// }
168//
169// public boolean isDone() {
170// return elapsed >= DURATION_SECONDS || !fleet.isInHyperspace();
171// }
172//
173// public boolean runWhilePaused() {
174// // TODO Auto-generated method stub
175// return false;
176// }
177}
static CombatEngineAPI getCombatEngine()
Definition Global.java:63
void applyForce(CombatEntityAPI other, float amount)
void advance(float amount, List< InputEventAPI > events)
GravityPullEffect(CombatEntityAPI source, float range, float maxForce)