Starsector API
Loading...
Searching...
No Matches
SpeedReduction.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.ghosts;
2
3import org.lwjgl.util.vector.Vector2f;
4
5import com.fs.starfarer.api.EveryFrameScript;
6import com.fs.starfarer.api.campaign.CampaignFleetAPI;
7import com.fs.starfarer.api.campaign.SectorEntityToken;
8import com.fs.starfarer.api.util.Misc;
9
10public class SpeedReduction implements EveryFrameScript {
11
12 protected float elapsed;
13 protected SectorEntityToken target;
14 protected float speedReductionRate;
15 protected float durationSeconds;
16
17 public SpeedReduction(SectorEntityToken target, float speedReductionFraction) {
18 this(target, speedReductionFraction, 0.2f);
19 }
20 public SpeedReduction(SectorEntityToken target, float speedReductionFraction, float durationSeconds) {
21 this.target = target;
22 this.durationSeconds = durationSeconds;
23 speedReductionRate = target.getVelocity().length() * speedReductionFraction / durationSeconds;
24 }
25
26 public void advance(float amount) {
27 if (target instanceof CampaignFleetAPI) {
28 target.setOrbit(null);
29 }
30
31 Vector2f v = target.getVelocity();
32
33 Vector2f dV = Misc.getUnitVector(new Vector2f(), v);
34 dV.scale(-1f * Math.min(v.length(), speedReductionRate * Math.min(durationSeconds, amount)));
35
37 if (ghost != null) {
38 v = ghost.getMovement().getVelocity();
39 ghost.getMovement().getVelocity().set(v.x + dV.x, v.y + dV.y);
40 } else if (target instanceof CampaignFleetAPI) {
41 v = ((CampaignFleetAPI)target).getVelocityFromMovementModule();
42 ((CampaignFleetAPI)target).setVelocity(v.x + dV.x, v.y + dV.y);
43 } else {
44 target.getVelocity().set(v.x + dV.x, v.y + dV.y);
45 }
46
47 elapsed += amount;
48 }
49
50 public boolean isDone() {
51 return elapsed >= durationSeconds;
52 }
53
54 public boolean runWhilePaused() {
55 return false;
56 }
57
58}
static SensorGhost getGhostFor(SectorEntityToken entity)
SpeedReduction(SectorEntityToken target, float speedReductionFraction, float durationSeconds)
SpeedReduction(SectorEntityToken target, float speedReductionFraction)