Starsector API
Loading...
Searching...
No Matches
BaseToggleAbility.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.abilities;
2
3import org.lwjgl.util.vector.Vector2f;
4
5import com.fs.starfarer.api.Global;
6import com.fs.starfarer.api.ui.TooltipMakerAPI;
7import com.fs.starfarer.api.util.Misc;
8
20public abstract class BaseToggleAbility extends BaseAbilityPlugin {
21
22 public float getLoopSoundUIVolume() { return level; }
23 public float getLoopSoundUIPitch() { return 1f; }
24
25 public float getLoopSoundWorldVolume() { return level; }
26 public float getLoopSoundWorldPitch() { return 1f; }
27
28
31
32 public float getActivationDays() { return spec.getActivationDays(); }
33 public float getDeactivationDays() { return spec.getDeactivationDays(); }
34
35
36 protected abstract void activateImpl();
37
42 protected abstract void applyEffect(float amount, float level);
43 protected abstract void deactivateImpl();
44 protected abstract void cleanupImpl();
45
46
47 protected boolean turnedOn = false;
48 protected float cooldownLeft = 0;
49 protected boolean isActivateCooldown = false;
50
51 protected float level = 0;
52
53 public float getCooldownLeft() {
54 return cooldownLeft;
55 }
56 public void setCooldownLeft(float cooldownLeft) {
57 this.cooldownLeft = cooldownLeft;
58 }
59
60
61 @Override
62 public void advance(float amount) {
63 super.advance(amount);
64
67 String soundId = getLoopSoundWorld();
68 if (soundId != null) {
73 }
74 }
75
77 String soundId = getLoopSoundUI();
78 if (soundId != null) {
83 }
84 }
85
86
87 if (cooldownLeft > 0) {
88 float days = Global.getSector().getClock().convertToDays(amount);
89 cooldownLeft -= days;
90 if (cooldownLeft < 0) cooldownLeft = 0;
91 }
92
93 float prevLevel = level;
94 if (turnedOn && level < 1) {
95 float days = Global.getSector().getClock().convertToDays(amount);
96 level += days / getActivationDays();
97 if (level > 1) level = 1;
98 } else if (!turnedOn && level > 0) {
99 float days = Global.getSector().getClock().convertToDays(amount);
100 level -= days / getDeactivationDays();
101 if (level < 0) level = 0;
102 }
103 if (prevLevel != level || level > 0) {
104 applyEffect(amount, level);
105 //disableIncompatible();
106 }
107 }
108
109 protected void addIncompatibleToTooltip(TooltipMakerAPI tooltip, boolean expanded) {
110 addIncompatibleToTooltip(tooltip, "Interrupts the following abilities when activated:",
111 "Expand tooltip to view conflicting abilities",
112 expanded);
113 }
114
115 @Override
116 public boolean isUsable() {
117 if (!isActivateCooldown &&
119 getDeactivationDays() > 0) return false;
120 return super.isUsable();
121 }
122
123 @Override
124 public float getCooldownFraction() {
125 if (cooldownLeft <= 0) return 1f;
126 if (isActivateCooldown) {
127 float max = getActivateCooldownDays();
128 if (max <= 0f) {
129 return 1f - cooldownLeft / 1f;
130 }
131 return 1f - cooldownLeft / max;
132 } else {
133 float max = getDeactivateCooldownDays();
134 if (max <= 0f) {
135 return 1f - cooldownLeft / 1f;
136 }
137 return 1f - cooldownLeft / max;
138 }
139 }
140
141 @Override
142 public float getProgressFraction() {
143 return level;
144 }
145
146 @Override
147 public boolean showProgressIndicator() {
148 return level > 0 && (!turnedOn || level < 1);
149 }
150
151 @Override
152 public boolean showActiveIndicator() {
153 return isActive() && level >= 1;
154 }
155
156 public void pressButton() {
157 if (isActive()) {
158 deactivate();
159 } else {
160 activate();
161 }
162 if (entity.isPlayerFleet()) {
163 if (isActive()) {
164 String soundId = getOnSoundUI();
165 if (soundId != null) {
167 Global.getSoundPlayer().playSound(soundId, 1f, 1f, Global.getSoundPlayer().getListenerPos(), new Vector2f());
168 } else {
169 Global.getSoundPlayer().playUISound(soundId, 1f, 1f);
170 }
171 }
172 } else {
173 String soundId = getOffSoundUI();
174 if (soundId != null) {
176 Global.getSoundPlayer().playSound(soundId, 1f, 1f, Global.getSoundPlayer().getListenerPos(), new Vector2f());
177 } else {
178 Global.getSoundPlayer().playUISound(soundId, 1f, 1f);
179 }
180 }
181 }
182 }
183 }
184
185 public void activate() {
186 if (!isActive() && isUsable()) {
187 turnedOn = true;
189 String soundId = getOnSoundWorld();
190 if (soundId != null) {
192 }
193 }
194 if (getActivationDays() <= 0) {
195 level = 1;
196 }
197
199 isActivateCooldown = true;
200
204 }
205 }
206 activateImpl();
207 applyEffect(0f, level);
209
210 super.activate();
211 }
212 }
213
214 public void deactivate() {
215 if (isActive()) {// && isUsable()) {
216 turnedOn = false;
218 String soundId = getOffSoundWorld();
219 if (soundId != null) {
221 }
222 }
223 if (getDeactivationDays() <= 0) {
224 level = 0;
225 }
227 isActivateCooldown = false;
228
229 applyEffect(0f, level);
230
234 }
235 }
237
238 super.deactivate();
239 }
240 }
241
242
243 @Override
244 public void cleanup() {
245 super.cleanup();
246
247 applyEffect(0, 0);
248
249 cleanupImpl();
250 }
251
252 public boolean isActive() {
253 return turnedOn;
254 }
255
256 public boolean hasCustomButtonPressSounds() {
257 return getOnSoundUI() != null;
258 }
259
260 @Override
261 public boolean runWhilePaused() {
262 return false;
263 }
264 public float getLevel() {
265 return level;
266 }
267
268}
269
270
271
272
273
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:49
static SectorAPI getSector()
Definition Global.java:65
abstract void applyEffect(float amount, float level)
void addIncompatibleToTooltip(TooltipMakerAPI tooltip, boolean expanded)
static Color setAlpha(Color color, int alpha)
Definition Misc.java:1316
SoundAPI playUISound(String id, float pitch, float volume)
void playLoop(String id, Object playingEntity, float pitch, float volume, Vector2f loc, Vector2f vel)
SoundAPI playSound(String id, float pitch, float volume, Vector2f loc, Vector2f vel)
void addFloatingText(String text, Color color, float duration)