001package com.fs.starfarer.api.impl.campaign;
002
003import java.awt.Color;
004
005import org.lwjgl.util.vector.Vector2f;
006import org.lwjgl.util.vector.Vector3f;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.CampaignEngineLayers;
010import com.fs.starfarer.api.campaign.CustomEntitySpecAPI;
011import com.fs.starfarer.api.campaign.PlanetAPI;
012import com.fs.starfarer.api.campaign.SectorEntityToken;
013import com.fs.starfarer.api.combat.ViewportAPI;
014import com.fs.starfarer.api.graphics.SpriteAPI;
015import com.fs.starfarer.api.util.FlickerUtilV2;
016import com.fs.starfarer.api.util.Misc;
017
018public class FusionLampEntityPlugin extends BaseCustomEntityPlugin {
019
020        public static Color GLOW_COLOR = new Color(255,165,100,255);
021        public static Color LIGHT_COLOR = new Color(255,165,100,255);
022        
023        public static String VOLATILES_SHORTAGE_KEY = "$core_volatilesShortage";
024        
025        public static String GLOW_COLOR_KEY = "$core_lampGlowColor";
026        public static String LIGHT_COLOR_KEY = "$core_lampLightColor";
027        public static float GLOW_FREQUENCY = 0.2f; // on/off cycles per second
028        
029        
030        transient private SpriteAPI sprite;
031        transient private SpriteAPI glow;
032        
033        public void init(SectorEntityToken entity, Object pluginParams) {
034                super.init(entity, pluginParams);
035                //this.entity = entity;
036                entity.setDetectionRangeDetailsOverrideMult(0.75f);
037                readResolve();
038        }
039        
040        Object readResolve() {
041                //sprite = Global.getSettings().getSprite("campaignEntities", "fusion_lamp");
042                glow = Global.getSettings().getSprite("campaignEntities", "fusion_lamp_glow");
043                return this;
044        }
045        
046        protected float phase = 0f;
047        protected FlickerUtilV2 flicker = new FlickerUtilV2();
048
049        public void advance(float amount) {
050                phase += amount * GLOW_FREQUENCY;
051                while (phase > 1) phase --;
052                
053                flicker.advance(amount * 1f);
054                
055                SectorEntityToken focus = entity.getOrbitFocus();
056                if (focus instanceof PlanetAPI) {
057                        PlanetAPI planet = (PlanetAPI) focus;
058                        float lightAlpha = getLightAlpha();
059                        lightAlpha *= entity.getSensorFaderBrightness();
060                        lightAlpha *= entity.getSensorContactFaderBrightness();
061                        planet.setSecondLight(
062                                        new Vector3f(entity.getLocation().x, entity.getLocation().y, entity.getCircularOrbitRadius() * 0.75f), 
063                                        Misc.scaleColor(getLightColor(), lightAlpha));
064                }
065        }
066        
067        public float getFlickerBasedMult() {
068                float shortage = entity.getMemoryWithoutUpdate().getFloat(VOLATILES_SHORTAGE_KEY);
069                shortage *= 0.33f;
070                if (shortage <= 0f) return 1f;
071                
072                //float f = (1f - shortage) + (shortage * flicker.getBrightness());
073                float f = 1f - shortage * flicker.getBrightness();
074                return f;
075        }
076        
077        public float getGlowAlpha() {
078                float glowAlpha = 0f;
079                if (phase < 0.5f) glowAlpha = phase * 2f;
080                if (phase >= 0.5f) glowAlpha = (1f - (phase - 0.5f) * 2f);
081                glowAlpha = 0.75f + glowAlpha * 0.25f;
082                glowAlpha *= getFlickerBasedMult();
083                if (glowAlpha < 0) glowAlpha = 0;
084                if (glowAlpha > 1) glowAlpha = 1;
085                return glowAlpha;
086        }
087        public float getLightAlpha() {
088                //if (true) return 0f;
089                float lightAlpha = 0f;
090                if (phase < 0.5f) lightAlpha = phase * 2f;
091                if (phase >= 0.5f) lightAlpha = (1f - (phase - 0.5f) * 2f);
092                lightAlpha = 0.5f + lightAlpha * 0.5f;
093                lightAlpha *= getFlickerBasedMult();
094                if (lightAlpha < 0) lightAlpha = 0;
095                if (lightAlpha > 1) lightAlpha = 1;
096                return lightAlpha;
097        }
098        
099        public Color getGlowColor() {
100                Color glowColor = GLOW_COLOR;
101                if (entity.getMemoryWithoutUpdate().contains(GLOW_COLOR_KEY)) {
102                        glowColor = (Color) entity.getMemoryWithoutUpdate().get(GLOW_COLOR_KEY);
103                }
104                return glowColor;
105        }
106        public Color getLightColor() {
107                Color lightColor = LIGHT_COLOR;
108                if (entity.getMemoryWithoutUpdate().contains(LIGHT_COLOR_KEY)) {
109                        lightColor = (Color) entity.getMemoryWithoutUpdate().get(LIGHT_COLOR_KEY);
110                }
111                return lightColor;
112        }
113        
114        public void setGlowColor(Color color) {
115                entity.getMemoryWithoutUpdate().set(GLOW_COLOR_KEY, color);
116        }
117        public void setLightColor(Color color) {
118                entity.getMemoryWithoutUpdate().set(LIGHT_COLOR_KEY, color);
119        }
120
121        public float getRenderRange() {
122                return entity.getRadius() + 1200f;
123        }
124
125        public void render(CampaignEngineLayers layer, ViewportAPI viewport) {
126                float alphaMult = viewport.getAlphaMult();
127                alphaMult *= entity.getSensorFaderBrightness();
128                alphaMult *= entity.getSensorContactFaderBrightness();
129                if (alphaMult <= 0) return;
130                
131                CustomEntitySpecAPI spec = entity.getCustomEntitySpec();
132                if (spec == null) return;
133                
134                float w = spec.getSpriteWidth();
135                float h = spec.getSpriteHeight();
136                
137                Vector2f loc = entity.getLocation();
138                
139                if (sprite != null) {
140                        sprite.setAngle(entity.getFacing() - 90f);
141                        sprite.setSize(w, h);
142                        sprite.setAlphaMult(alphaMult);
143                        sprite.setNormalBlend();
144                        sprite.renderAtCenter(loc.x, loc.y);
145                }
146                
147                
148                float glowAlpha = getGlowAlpha();
149                
150                float glowAngle1 = (((phase * 1.3f) % 1) - 0.5f) * 12f;
151                float glowAngle2 = (((phase * 1.9f) % 1) - 0.5f) * 12f;
152                
153                glow.setColor(getGlowColor());
154                
155                w = 600f;
156                h = 600f;
157                
158                glow.setSize(w, h);
159                glow.setAlphaMult(alphaMult * glowAlpha * 0.5f);
160                glow.setAdditiveBlend();
161                
162                glow.renderAtCenter(loc.x, loc.y);
163                
164                for (int i = 0; i < 5; i++) {
165                        w *= 0.3f;
166                        h *= 0.3f;
167                        //glow.setSize(w * 0.1f, h * 0.1f);
168                        glow.setSize(w, h);
169                        glow.setAlphaMult(alphaMult * glowAlpha * 0.67f);
170                        glow.renderAtCenter(loc.x, loc.y);
171                }
172                
173//              glow.setSize(w, h);
174//              glow.setAlphaMult(alphaMult * glowAlpha);
175//              glow.setAdditiveBlend();
176//              
177//              glow.setAngle(entity.getFacing() - 90f + glowAngle1);
178//              glow.renderAtCenter(loc.x, loc.y);
179//              
180//              glow.setAngle(entity.getFacing() - 90f + glowAngle2);
181//              glow.setAlphaMult(alphaMult * glowAlpha * 0.5f);
182//              glow.renderAtCenter(loc.x, loc.y);
183        }
184        
185
186//      @Override
187//      public void createMapTooltip(TooltipMakerAPI tooltip, boolean expanded) {
188//              String post = "";
189//              Color color = entity.getFaction().getBaseUIColor();
190//              Color postColor = color;
191//              if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.DESTROYED.getBeaconFlag())) {
192//                      post = " - Low";
193//                      postColor = Misc.getPositiveHighlightColor();
194//              } else if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.SUPPRESSED.getBeaconFlag())) {
195//                      post = " - Medium";
196//                      postColor = Misc.getHighlightColor();
197//              } else if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.RESURGENT.getBeaconFlag())) {
198//                      post = " - High";
199//                      postColor = Misc.getNegativeHighlightColor();
200//              }
201//              
202//              tooltip.addPara(entity.getName() + post, 0f, color, postColor, post.replaceFirst(" - ", ""));
203//      }
204//
205//      @Override
206//      public boolean hasCustomMapTooltip() {
207//              return true;
208//      }
209//      
210//      @Override
211//      public void appendToCampaignTooltip(TooltipMakerAPI tooltip, VisibilityLevel level) {
212//              if (level == VisibilityLevel.COMPOSITION_AND_FACTION_DETAILS || 
213//                              level == VisibilityLevel.COMPOSITION_DETAILS) {
214//                      
215//                      String post = "";
216//                      Color color = Misc.getTextColor();
217//                      Color postColor = color;
218//                      if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.DESTROYED.getBeaconFlag())) {
219//                              post = "low";
220//                              postColor = Misc.getPositiveHighlightColor();
221//                      } else if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.SUPPRESSED.getBeaconFlag())) {
222//                              post = "medium";
223//                              postColor = Misc.getHighlightColor();
224//                      } else if (entity.getMemoryWithoutUpdate().getBoolean(RemnantSystemType.RESURGENT.getBeaconFlag())) {
225//                              post = "high";
226//                              postColor = Misc.getNegativeHighlightColor();
227//                      }
228//                      if (!post.isEmpty()) {
229//                              tooltip.setParaFontDefault();
230//                              tooltip.addPara(BaseIntelPlugin.BULLET + "Danger level: " + post, 10f, color, postColor, post);
231//                      }
232//              }
233//              
234//      }
235}
236
237
238
239
240
241
242
243
244