001package com.fs.starfarer.api.impl.campaign;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.List;
006
007import org.lwjgl.util.vector.Vector2f;
008
009import com.fs.starfarer.api.EveryFrameScript;
010import com.fs.starfarer.api.campaign.PlanetAPI;
011import com.fs.starfarer.api.campaign.SectorEntityToken;
012import com.fs.starfarer.api.impl.campaign.world.ZigLeashAssignmentAI;
013import com.fs.starfarer.api.util.IntervalUtil;
014import com.fs.starfarer.api.util.Misc;
015
016public class CoronalTapParticleScript implements EveryFrameScript {
017
018        protected SectorEntityToken tap;
019        
020        protected IntervalUtil spawn = new IntervalUtil(0.05f, 0.1f);
021        
022        public CoronalTapParticleScript(SectorEntityToken tap) {
023                super();
024                this.tap = tap;
025        }
026
027        public void advance(float amount) {
028                if (tap.isDiscoverable()) return;
029                if (!tap.getMemoryWithoutUpdate().contains("$usable")) return;
030                if (!tap.isInCurrentLocation()) return;
031
032                if (spawn == null) { // nothing to see here, just making an older dev save work 
033                        spawn = new IntervalUtil(0.05f, 0.1f);
034                }
035                
036                float days = Misc.getDays(amount);
037                spawn.advance(days * 5f);
038                if (spawn.intervalElapsed()) {
039
040//                      float minDur = 0.5f;
041//                      float maxDur = 0.75f;
042                        float minSize = 10f;
043                        float maxSize = 20f;
044                        
045                        float sizeMult = 30f;
046                        
047                        minSize *= sizeMult;
048                        maxSize *= sizeMult;
049                        
050                        if ((float) Math.random() < 0.01f) {
051                                ZigLeashAssignmentAI.spawnMote(tap);
052                        }
053                        
054                        
055                        
056                        List<PlanetAPI> stars = new ArrayList<PlanetAPI>();
057                        PlanetAPI closest = null;
058                        float minDist = Float.MAX_VALUE;
059                        for (PlanetAPI star : tap.getContainingLocation().getPlanets()) {
060                                if (!star.isStar()) continue;
061                                
062                                float ctcDist = Misc.getDistance(tap.getLocation(), star.getLocation());
063                                float dist = ctcDist - star.getRadius() - tap.getRadius();
064                                if (dist > 2000) continue;
065                                
066                                if (ctcDist < minDist) {
067                                        minDist = ctcDist;
068                                        closest = star;
069                                }
070                                
071                                stars.add(star);
072                        }
073//                      float minDist = Float.MAX_VALUE;
074//                      closest = null;
075//                      for (PlanetAPI star : tap.getContainingLocation().getPlanets()) {
076//                              if (!star.isStar()) continue;
077//                              
078//                              float dist = Misc.getDistance(tap.getLocation(), star.getLocation());
079//                              if (dist < minDist) {
080//                                      minDist = dist;
081//                                      closest = star;
082//                              }
083//                      }
084//                      if (closest != null) {
085//                              stars.add(closest);
086//                      }
087                        
088                        for (PlanetAPI star : stars) {
089                                float dirToTap = Misc.getAngleInDegrees(star.getLocation(), tap.getLocation());
090                                Vector2f unitToTap = Misc.getUnitVectorAtDegreeAngle(dirToTap);
091                                Vector2f focusLoc = new Vector2f(unitToTap);
092                                focusLoc.scale(100f + star.getRadius() + tap.getRadius());
093                                Vector2f.add(star.getLocation(), focusLoc, focusLoc);
094                                
095                                //focusLoc = tap.getLocation();
096                                
097                                float ctcDist = Misc.getDistance(focusLoc, star.getLocation());
098                                float dist = ctcDist - star.getRadius() - tap.getRadius();
099                                
100                                int glows = 3;
101                                float minRadius = dist + (star.getRadius() * 0.2f);
102                                float maxRadius = dist + (star.getRadius() * 0.4f);
103                                
104                                Color color = star.getSpec().getCoronaColor();
105                                
106                                float colorScale = 0.25f;
107                                colorScale = 0.1f + 0.15f / (float) stars.size();
108                                if (closest == star) colorScale = 0.25f;
109                                color = Misc.scaleColor(color, colorScale);
110                                //Color white = Misc.scaleColor(Color.white, colorScale);
111                                
112                                float dirToStar = Misc.getAngleInDegrees(focusLoc, star.getLocation());
113                                float arc = star.getRadius() / ((float) Math.PI * (ctcDist - tap.getRadius())) * 360f; 
114                                
115                                for (int i = 0; i < glows; i++) {
116                                        float radius = minRadius + (float) Math.random() * (maxRadius - minRadius);
117
118                                        float angle = dirToStar - arc / 2f + arc * (float) Math.random();
119                                        
120                                        Vector2f unit = Misc.getUnitVectorAtDegreeAngle(angle);
121                                        float x = unit.x * radius + focusLoc.x;
122                                        float y = unit.y * radius + focusLoc.y;
123                                        
124                                        Vector2f loc = new Vector2f(x, y);
125                                        //Vector2f loc = Misc.getPointAtRadius(focusLoc, radius);
126                                        
127                                        Vector2f vel = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(loc, focusLoc));
128                                        float travelDist = Misc.getDistance(loc, focusLoc) - tap.getRadius() * 0.7f - 100f;
129                                        travelDist = Math.min(travelDist, maxRadius - minRadius + 100f);
130//                                      float dur = minDur + (float) Math.random() * (maxDur - minDur);
131//                                      dur *= 2f;
132//                                      float speed = travelDist / dur;
133                                        float speed = 100f + 100f * (float) Math.random();
134                                        float dur = travelDist / speed;
135                                        
136                                        vel.scale(speed);
137                                        
138                                        float size = minSize + (float) Math.random() * (maxSize - minSize);
139                                        
140                                        float rampUp = 0.5f;
141                                        //Misc.addGlowyParticle(tap.getContainingLocation(), loc, vel, size, rampUp, dur, color);
142                                        
143                                        tap.getContainingLocation().addParticle(loc, vel,
144                                                        size, 0.4f, rampUp, dur, color);
145                                        tap.getContainingLocation().addParticle(loc, vel,
146                                                        size * 0.25f, 0.4f, rampUp, dur, color);
147//                                      tap.getContainingLocation().addParticle(loc, vel,
148//                                                      size * 0.15f, 1f, rampUp, dur, white);
149                                }
150                        }
151                }
152        }
153
154        public boolean isDone() {
155                return false;
156        }
157
158        public boolean runWhilePaused() {
159                return false;
160        }
161}
162
163
164
165
166
167
168
169
170
171
172
173