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