Starsector API
Loading...
Searching...
No Matches
CampaignEntityMovementUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2
3import org.lwjgl.util.vector.Vector2f;
4
5import com.fs.starfarer.api.campaign.SectorEntityToken;
6
8
9 public static interface EngineGlowControls {
10 void showAccelerating();
11 void showIdling();
12 void showSuppressed();
13 void showOtherAction();
14 }
15
16 public static float DIRECTION_UNSET = Float.MAX_VALUE;
17
18 protected SectorEntityToken entity;
21 protected boolean turnThenAccelerate = true;
22 protected boolean faceInOppositeDirection = false;
23 protected float moveDir;
24 protected float desiredFacing = DIRECTION_UNSET;
25 protected Vector2f moveDest;
26 protected EngineGlowControls engineGlow;
27
28 public CampaignEntityMovementUtil(SectorEntityToken entity,
29 float turnAccel, float maxTurnRate, float accel, float maxSpeed) {
30 this.entity = entity;
31
32 facingUtil = new SmoothFacingUtil(turnAccel, maxTurnRate);
34
36 movementUtil.setMaxSpeed(maxSpeed);
37
39 moveDest = null;
40 }
41
42 public boolean isFaceInOppositeDirection() {
44 }
45
47 this.faceInOppositeDirection = faceInOppositeDirection;
48 }
49
50 public boolean isTurnThenAccelerate() {
51 return turnThenAccelerate;
52 }
53
55 this.turnThenAccelerate = turnThenAccelerate;
56 }
57
59 return facingUtil;
60 }
61
65
66 public void moveInDirection(float dir) {
67 moveDir = dir;
68 moveDest = null;
69 leaveOrbit();
70 }
71 public void moveToLocation(Vector2f loc) {
73 moveDest = loc;
74 leaveOrbit();
75 }
76
77 public void stop() {
79 moveDest = new Vector2f(entity.getLocation());
80 leaveOrbit();
81 }
82
83 public void leaveOrbit() {
84 if (entity.getOrbit() != null) {
85 setFacing(entity.getFacing());
86 setLocation(entity.getLocation());
87 setVelocity(entity.getVelocity());
88 entity.setOrbit(null);
89 }
90 }
91
93 Vector2f p = new Vector2f(entity.getLocation());
94 if (entity.getVelocity().length() > 0) {
95 Vector2f back = new Vector2f(entity.getVelocity());
97 back.negate();
98 back.scale(10f);
99 Vector2f.add(back, entity.getLocation(), back);
100 p = back;
101 }
102 return p;
103 }
104
105 public void advance(float amount) {
106 //movementUtil.setAcceleration(20f);
107 if (entity.getOrbit() == null) {
108 Vector2f dest;
109
110 if (moveDir != DIRECTION_UNSET) {
112 dest.scale(100000000f);
113 Vector2f.add(dest, entity.getLocation(), dest);
115 } else if (moveDest != null) {
117 dest = moveDest;
118 } else {
119 desiredFacing = entity.getFacing();
121 }
122
124
125 float angleDiff = Misc.getAngleDiff(entity.getFacing(), desiredFacing);
126 boolean turnOnly = turnThenAccelerate && angleDiff > 2f;
127 if (turnOnly) {
129 }
130
131 movementUtil.setDest(dest, new Vector2f());
134
135 entity.setFacing(facingUtil.getFacing());
136 entity.getLocation().set(movementUtil.getLocation());
137 entity.getVelocity().set(movementUtil.getVelocity());
138
139 if (engineGlow != null) {
140 if (turnOnly || angleDiff > 30f) {
141 engineGlow.showOtherAction();
142 } else {
143 engineGlow.showAccelerating();
144 }
145 }
146
147 } else {
148 if (engineGlow != null) {
149 engineGlow.showIdling();
150 }
152 }
153 }
154
155 public boolean isDesiredFacingSet() {
157 }
158
159 public float getDesiredFacing() {
160 return desiredFacing;
161 }
162
163
164 public SectorEntityToken getEntity() {
165 return entity;
166 }
167
168 public void setFacing(float facing) {
169 facingUtil.setFacing(facing);
170 entity.setFacing(facing);
171 }
172
173 public void setLocation(Vector2f loc) {
174 movementUtil.getLocation().set(loc);
175 entity.getLocation().set(movementUtil.getLocation());
176 }
177 public void setVelocity(Vector2f vel) {
178 movementUtil.getVelocity().set(vel);
179 entity.getVelocity().set(movementUtil.getVelocity());
180 }
181
182 public EngineGlowControls getEngineGlow() {
183 return engineGlow;
184 }
185
186 public void setEngineGlow(EngineGlowControls engineGlow) {
187 this.engineGlow = engineGlow;
188 }
189
190}
191
192
193
194
195
196
197
CampaignEntityMovementUtil(SectorEntityToken entity, float turnAccel, float maxTurnRate, float accel, float maxSpeed)
static Vector2f getUnitVectorAtDegreeAngle(float degrees)
Definition Misc.java:1189
static float getAngleDiff(float from, float to)
Definition Misc.java:1709
static float getAngleInDegrees(Vector2f v)
Definition Misc.java:1119
static Vector2f normalise(Vector2f v)
Definition Misc.java:1127
void advance(float desiredFacing, float amount)
void setDest(Vector2f dest, Vector2f destVel)