Starsector API
Loading...
Searching...
No Matches
RadioChatterTerrainPlugin.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
2
3import java.util.EnumSet;
4
5import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
8import org.lwjgl.util.vector.Vector2f;
9
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.CampaignEngineLayers;
12import com.fs.starfarer.api.campaign.CampaignFleetAPI;
13import com.fs.starfarer.api.campaign.SectorEntityToken;
14import com.fs.starfarer.api.combat.ViewportAPI;
15import com.fs.starfarer.api.util.Misc;
16
18
19 public static class RadioChatterParams extends RingParams {
20 public RadioChatterParams(float bandWidthInEngine, float middleRadius, SectorEntityToken relatedEntity) {
21 super(bandWidthInEngine, middleRadius, relatedEntity);
22 }
23 }
24
25 protected RadioChatterParams params;
26
27
28 public void init(String terrainId, SectorEntityToken entity, Object param) {
29 super.init(terrainId, entity, param);
30 this.params = (RadioChatterParams) param;
31 }
32
33 @Override
34 protected Object readResolve() {
35 super.readResolve();
36 layers = EnumSet.noneOf(CampaignEngineLayers.class);
37 return this;
38 }
39
40 Object writeReplace() {
41 return this;
42 }
43
44 transient private EnumSet<CampaignEngineLayers> layers = EnumSet.noneOf(CampaignEngineLayers.class);
45 public EnumSet<CampaignEngineLayers> getActiveLayers() {
46 return layers;
47 }
48
49
50
51 public void render(CampaignEngineLayers layer, ViewportAPI viewport) {
52
53 }
54
55 protected transient float phase = 0f;
56 public void advance(float amount) {
57 super.advance(amount);
58
59 float period = (float)Math.PI * 2f;
60 phase += period/10f * amount;
61 }
62 @Override
63 public void applyEffect(SectorEntityToken entity, float days) {
64 if (!entity.isPlayerFleet()) return;
65
66 float prox = getProximitySoundFactor();
67 float volumeMult = prox;
68 float suppressionMult = prox;
69 if (volumeMult <= 0) return;
70 volumeMult = (float) Math.sqrt(volumeMult);
71 //volumeMult = 1f;
72
73 Global.getSector().getCampaignUI().suppressMusic(getSpec().getMusicSuppression() * suppressionMult);
74
75 float dirToEntity = Misc.getAngleInDegrees(entity.getLocation(), this.entity.getLocation());
76 Vector2f playbackLoc = Misc.getUnitVectorAtDegreeAngle(dirToEntity);
77 playbackLoc.scale(500f);
78 Vector2f.add(entity.getLocation(), playbackLoc, playbackLoc);
79
80 try {
81 JSONArray sounds = getSpec().getCustom().getJSONArray("chatter");
82 float num = sounds.length();
83 float period = (float)Math.PI * 2f;
84
85// float marketSize = 10f;
86// if (params.relatedEntity != null && params.relatedEntity.getMarket() != null) {
87// marketSize = params.relatedEntity.getMarket().getSize();
88// }
89
90 //{"sound":"terrain_radio_chatter_1", "phaseMult":1, "threshold":-1},
91 for (int i = 0; i < sounds.length(); i++) {
92 JSONObject sound = sounds.getJSONObject(i);
93 float threshold = (float) sound.getDouble("threshold");
94 float phaseMult = (float) sound.getDouble("phaseMult");
95 String soundId = sound.getString("sound");
96
97// float thresholdRange = 1f - threshold;
98// float sizePenalty = Math.max(6f - marketSize, 0) / 6f * thresholdRange;
99// threshold += sizePenalty;
100
101 //phaseMult = 1f;
102 float offset = period / num * (float) i;
103 //float cos = (float) Math.cos((phase + offset) * ((float) i * 0.5f + 1f));
104 float cos = (float) Math.cos(phase * phaseMult + offset);
105
106 float volume = 0f;
107 if (cos > threshold) {
108 volume = (cos - threshold) / (1f - threshold);
109 if (volume < 0) volume = 0;
110 if (volume > 1) volume = 1;
111 }
112
113
114 Global.getSoundPlayer().playLoop(soundId, params.relatedEntity,
115 //volume * 0.25f + 0.75f, volume * volumeMult,
116 1f, volume * volumeMult,
117 playbackLoc, Misc.ZERO);
118 }
119
120
121 } catch (JSONException e) {
122 }
123 }
124
125
126 @Override
127 public float getProximitySoundFactor() {
128 CampaignFleetAPI player = Global.getSector().getPlayerFleet();
129 float dist = Misc.getDistance(player.getLocation(), entity.getLocation());
130 float radSum = params.relatedEntity.getRadius() + player.getRadius();
131
132 //if (dist < radSum) return 1f;
133 dist -= radSum;
134
135 float f = 1f - dist / Math.max(1, (params.bandWidthInEngine - params.relatedEntity.getRadius()));
136 if (f < 0) f = 0;
137 if (f > 1) f = 1;
138 return f;
139 }
140
141 @Override
142 protected float getExtraSoundRadius() {
143 return 0f;
144 }
145
146 @Override
147 public boolean containsPoint(Vector2f point, float radius) {
148 return super.containsPoint(point, radius);
149 }
150
151 public boolean hasTooltip() {
152 return false;
153 }
154
155
156 public String getTerrainName() {
157 return null;
158 }
159
160 public String getNameForTooltip() {
161 return null;
162 }
163
164 public String getEffectCategory() {
165 return "radio_chatter";
166 }
167
168 public boolean canPlayerHoldStationIn() {
169 return false;
170 }
171}
172
173
174
175
176
177
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:43
static SectorAPI getSector()
Definition Global.java:59
void init(String terrainId, SectorEntityToken entity, Object param)
void render(CampaignEngineLayers layer, ViewportAPI viewport)
void playLoop(String id, Object playingEntity, float pitch, float volume, Vector2f loc, Vector2f vel)