001package com.fs.starfarer.api.impl.combat;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.List;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.combat.MutableShipStatsAPI;
009import com.fs.starfarer.api.combat.ShipAPI;
010
011public class RecallDeviceStats extends BaseShipSystemScript {
012        public static final Object KEY_JITTER = new Object();
013        public static final Color JITTER_COLOR = new Color(100,165,255,155);
014
015        
016        public void apply(MutableShipStatsAPI stats, String id, State state, float effectLevel) {
017                ShipAPI ship = null;
018                if (stats.getEntity() instanceof ShipAPI) {
019                        ship = (ShipAPI) stats.getEntity();
020                } else {
021                        return;
022                }
023                
024                
025                if (effectLevel > 0) {
026                        float jitterLevel = effectLevel;
027                        
028                        boolean firstTime = false;
029                        final String fightersKey = ship.getId() + "_recall_device_target";
030                        List<ShipAPI> fighters = null;
031                        if (!Global.getCombatEngine().getCustomData().containsKey(fightersKey)) {
032                                fighters = getFighters(ship);
033                                Global.getCombatEngine().getCustomData().put(fightersKey, fighters);
034                                firstTime = true;
035                        } else {
036                                fighters = (List<ShipAPI>) Global.getCombatEngine().getCustomData().get(fightersKey);
037                        }
038                        if (fighters == null) { // shouldn't be possible, but still
039                                fighters = new ArrayList<ShipAPI>();
040                        }
041                        
042                        for (ShipAPI fighter : fighters) {
043                                if (fighter.isHulk()) continue;
044                                
045                                float maxRangeBonus = fighter.getCollisionRadius() * 1f;
046                                float jitterRangeBonus = 5f + jitterLevel * maxRangeBonus;
047                                
048                                if (firstTime) {
049                                        Global.getSoundPlayer().playSound("system_phase_skimmer", 1f, 0.5f, fighter.getLocation(), fighter.getVelocity());
050                                }
051                                
052                                fighter.setJitter(KEY_JITTER, JITTER_COLOR, jitterLevel, 10, 0f, jitterRangeBonus);
053                                if (fighter.isAlive()) {
054                                        fighter.setPhased(true);
055                                }
056                                
057                                if (state == State.IN) {
058                                        float alpha = 1f - effectLevel * 0.5f;
059                                        fighter.setExtraAlphaMult(alpha);
060                                }
061        
062                                if (effectLevel == 1) {
063                                        if (fighter.getWing() != null && fighter.getWing().getSource() != null) {
064                                                fighter.getWing().getSource().makeCurrentIntervalFast();
065                                                fighter.getWing().getSource().land(fighter);
066                                        } else {
067                                                fighter.setExtraAlphaMult(1);
068                                        }
069                                }
070                        }
071                }
072        }
073        
074        public static List<ShipAPI> getFighters(ShipAPI carrier) {
075                List<ShipAPI> result = new ArrayList<ShipAPI>();
076                
077                for (ShipAPI ship : Global.getCombatEngine().getShips()) {
078                        if (!ship.isFighter()) continue;
079                        if (ship.getWing() == null) continue;
080                        if (ship.getWing().getSourceShip() == carrier) {
081                                result.add(ship);
082                        }
083                }
084                
085                return result;
086        }
087        
088        
089        public void unapply(MutableShipStatsAPI stats, String id) {
090                ShipAPI ship = null;
091                if (stats.getEntity() instanceof ShipAPI) {
092                        ship = (ShipAPI) stats.getEntity();
093                } else {
094                        return;
095                }
096                
097                final String fightersKey = ship.getId() + "_recall_device_target";
098                Global.getCombatEngine().getCustomData().remove(fightersKey);
099                
100//              for (ShipAPI fighter : getFighters(ship)) {
101//                      fighter.setPhased(false);
102//                      fighter.setCopyLocation(null, 1f, fighter.getFacing());
103//              }
104        }
105        
106        
107        public StatusData getStatusData(int index, State state, float effectLevel) {
108                return null;
109        }
110
111        
112}
113
114
115
116
117
118
119
120