Advertisement
DarkRevenant

Untitled

May 8th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.16 KB | None | 0 0
  1. package data.scripts.plugins;
  2.  
  3. import com.fs.starfarer.api.Global;
  4. import com.fs.starfarer.api.combat.CombatEngineAPI;
  5. import com.fs.starfarer.api.combat.EveryFrameCombatPlugin;
  6. import com.fs.starfarer.api.combat.ShipAPI;
  7. import com.fs.starfarer.api.combat.ShipAPI.HullSize;
  8. import com.fs.starfarer.api.input.InputEventAPI;
  9. import com.fs.starfarer.api.util.IntervalUtil;
  10.  
  11. import java.awt.Color;
  12. import java.util.*;
  13.  
  14. import data.scripts.util.AnamorphicFlare;
  15. import org.dark.shaders.light.LightShader;
  16. import org.dark.shaders.light.StandardLight;
  17.  
  18. import org.lwjgl.util.vector.Vector2f;
  19. import org.lazywizard.lazylib.CollisionUtils;
  20. import org.lazywizard.lazylib.combat.entities.AnchoredEntity;
  21.  
  22. public class ShipDestructionEffects implements EveryFrameCombatPlugin {
  23.  
  24.     private CombatEngineAPI engine;
  25.     private static final Set<ShipAPI> deadShips = new LinkedHashSet<>(); // Faster for what we're doing
  26.     private static final List<ExplodingShip> explodingShips = new ArrayList<>();
  27.  
  28.     private static final Map<HullSize, Float> explosionLength = new HashMap<>();
  29.  
  30.     static {
  31.         explosionLength.put(HullSize.FIGHTER, 0.5f);
  32.         explosionLength.put(HullSize.FRIGATE, 2.5f);
  33.         explosionLength.put(HullSize.DESTROYER, 5f);
  34.         explosionLength.put(HullSize.DEFAULT, 5f);
  35.         explosionLength.put(HullSize.CRUISER, 7.5f);
  36.         explosionLength.put(HullSize.CAPITAL_SHIP, 10f);
  37.     }
  38.  
  39.     private static final Map<HullSize, Float> explosionSizeMod = new HashMap<>();
  40.  
  41.     static {
  42.         explosionSizeMod.put(HullSize.FIGHTER, 0.5f);
  43.         explosionSizeMod.put(HullSize.FRIGATE, 0.75f);
  44.         explosionSizeMod.put(HullSize.DESTROYER, 1f);
  45.         explosionSizeMod.put(HullSize.DEFAULT, 1f);
  46.         explosionSizeMod.put(HullSize.CRUISER, 1.25f);
  47.         explosionSizeMod.put(HullSize.CAPITAL_SHIP, 1.5f);
  48.     }
  49.  
  50.     private static final Map<HullSize, Float> flareBrightness = new HashMap<>();
  51.  
  52.     static {
  53.         flareBrightness.put(HullSize.FIGHTER, 10f);
  54.         flareBrightness.put(HullSize.FRIGATE, 25f);
  55.         flareBrightness.put(HullSize.DESTROYER, 50f);
  56.         flareBrightness.put(HullSize.DEFAULT, 50f);
  57.         flareBrightness.put(HullSize.CRUISER, 100f);
  58.         flareBrightness.put(HullSize.CAPITAL_SHIP, 250f);
  59.     }
  60.  
  61.     private static final Map<HullSize, Float> flareThickness = new HashMap<>();
  62.  
  63.     static {
  64.         flareThickness.put(HullSize.FIGHTER, 0.33f);
  65.         flareThickness.put(HullSize.FRIGATE, 0.10f);
  66.         flareThickness.put(HullSize.DESTROYER, 0.06f);
  67.         flareThickness.put(HullSize.DEFAULT, 0.06f);
  68.         flareThickness.put(HullSize.CRUISER, 0.04f);
  69.         flareThickness.put(HullSize.CAPITAL_SHIP, 0.01f);
  70.     }
  71.  
  72.     private static final Map<HullSize, Float> lightDuration = new HashMap<>();
  73.  
  74.     static {
  75.         lightDuration.put(HullSize.FIGHTER, 0f);
  76.         lightDuration.put(HullSize.FRIGATE, 0.5f);
  77.         lightDuration.put(HullSize.DESTROYER, 1f);
  78.         lightDuration.put(HullSize.DEFAULT, 1f);
  79.         lightDuration.put(HullSize.CRUISER, 1.5f);
  80.         lightDuration.put(HullSize.CAPITAL_SHIP, 2f);
  81.     }
  82.  
  83.     private static final Map<HullSize, Float> lightIntensity = new HashMap<>();
  84.  
  85.     static {
  86.         lightIntensity.put(HullSize.FIGHTER, 0.5f);
  87.         lightIntensity.put(HullSize.FRIGATE, 1f);
  88.         lightIntensity.put(HullSize.DESTROYER, 1.5f);
  89.         lightIntensity.put(HullSize.DEFAULT, 1.5f);
  90.         lightIntensity.put(HullSize.CRUISER, 1.75f);
  91.         lightIntensity.put(HullSize.CAPITAL_SHIP, 2f);
  92.     }
  93.  
  94.     @Override
  95.     public void init(CombatEngineAPI engine) {
  96.     }
  97.  
  98.     private final IntervalUtil interval = new IntervalUtil(0.1f, 0.1f);
  99.  
  100.     @Override
  101.     public void advance(float amount, List<InputEventAPI> events) {
  102.         if (engine != Global.getCombatEngine()) {
  103.             deadShips.clear();
  104.             for (ExplodingShip exploder : explodingShips) {
  105.                 exploder.flamePoints.clear();
  106.             }
  107.             explodingShips.clear();
  108.             this.engine = Global.getCombatEngine();
  109.             return;
  110.         }
  111.  
  112.         if (engine.isPaused()) {
  113.             return;
  114.         }
  115.  
  116.         interval.advance(amount);
  117.  
  118.         List<ShipAPI> ships = engine.getShips();
  119.  
  120.         // We run through all the ships and check for newly-destroyed ships, adding them to the graphics loop
  121.         for (ShipAPI ship : ships) {
  122.             if (ship == null) {
  123.                 continue;
  124.             }
  125.  
  126.             if (ship.isHulk() == true) {
  127.                 if (!deadShips.contains(ship)) {
  128.                     deadShips.add(ship);
  129.  
  130.                     if (ship.getHullSize() != ShipAPI.HullSize.FIGHTER && !ship.isDrone()) {
  131.                         float intensity = flareBrightness.get(ship.getHullSize()) / 10f;
  132.                         float intensity2 = flareThickness.get(ship.getHullSize()) / 3f;
  133.                         AnamorphicFlare.createFlare(ship, new Vector2f(ship.getLocation()), engine, intensity, intensity2, 0f, 15f, 1f, new Color(255, 180, 150), new Color(255, 255, 255));
  134.                     }
  135.  
  136.                     StandardLight light = new StandardLight();
  137.                     light.setSize(ship.getCollisionRadius() * explosionSizeMod.get(ship.getHullSize()) * 4f);
  138.                     light.setIntensity(lightIntensity.get(ship.getHullSize()));
  139.                     light.setColor(1f, 0.7f, 0.6f);
  140.                     light.setLocation(new Vector2f(ship.getLocation()));
  141.                     light.setLifetime(lightDuration.get(ship.getHullSize()));
  142.                     light.setAutoFadeOutTime(0.5f + lightDuration.get(ship.getHullSize()));
  143.                     LightShader.addLight(light);
  144.  
  145.                     if (!ship.isShuttlePod() && !ship.isDrone()) {
  146.                         int count = (int) (ship.getCollisionRadius() * explosionSizeMod.get(ship.getHullSize()) / 4f);
  147.                         float length = explosionLength.get(ship.getHullSize()) * ((float) Math.random() * 0.5f + 0.75f);
  148.  
  149.                         ExplodingShip exploder = new ExplodingShip(ship, count, length / (float) count);
  150.                         explodingShips.add(exploder);
  151.                     }
  152.                 }
  153.             }
  154.         }
  155.  
  156.         // If a ship is vaporized, we add some extra effects
  157.         Iterator<ShipAPI> iter = deadShips.iterator();
  158.         while (iter.hasNext()) {
  159.             ShipAPI ship = iter.next();
  160.  
  161.             if (ship != null && !ships.contains(ship)) {
  162.                 //Vector2f velocity = new Vector2f(ship.getVelocity());
  163.                 Vector2f velocity = new Vector2f();
  164.  
  165.                 engine.addHitParticle(ship.getLocation(), velocity, ship.getCollisionRadius() * 15f, 0.75f, ship.getCollisionRadius() / 15f, new Color(255, 255, 255, 255));
  166.                 engine.addSmoothParticle(ship.getLocation(), velocity, ship.getCollisionRadius() * 10f, 0.25f, ship.getCollisionRadius() / 10f, new Color(255, 255, 255, 50));
  167.  
  168.                 float sizeMod = explosionSizeMod.get(ship.getHullSize());
  169.                 int particles = (int) (sizeMod * ship.getCollisionRadius() / 2f * ((float) Math.random() * 0.5f + 0.75f));
  170.  
  171.                 for (int i = 0; i < particles; i++) {
  172.                     Vector2f point = new Vector2f(ship.getLocation());
  173.                     Vector2f vel = new Vector2f(velocity);
  174.                     vel.x += ((float) Math.random() + (float) Math.random() + (float) Math.random()) * 100f / 3f - 50f;
  175.                     vel.y += ((float) Math.random() + (float) Math.random() + (float) Math.random()) * 100f / 3f - 50f;
  176.                     point.x += ship.getCollisionRadius() * ((float) Math.random() * 1f - 0.5f);
  177.                     point.y += ship.getCollisionRadius() * ((float) Math.random() * 1f - 0.5f);
  178.  
  179.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 180f + 40f), (int) ((float) Math.random() * 100f));
  180.                     Color color2 = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  181.  
  182.                     engine.addHitParticle(point, vel, (float) Math.random() * 5f + 5f, 1f, (ship.getCollisionRadius() / 5f) * ((float) Math.random() * 1.5f + 0.25f), color);
  183.                     engine.addHitParticle(point, vel, (float) Math.random() * 15f + 30f, 0.2f, (ship.getCollisionRadius() / 5f) * ((float) Math.random() * 1.5f + 0.25f), color2);
  184.                 }
  185.  
  186.                 int fire = (int) (sizeMod * ship.getCollisionRadius() / 25f * ((float) Math.random() * 0.5f + 0.75f));
  187.  
  188.                 for (int i = 0; i < fire; i++) {
  189.                     Vector2f point = new Vector2f(ship.getLocation());
  190.                     Vector2f vel = new Vector2f(velocity);
  191.                     vel.x += (float) Math.random() * 20f - 10f;
  192.                     vel.y += (float) Math.random() * 20f - 10f;
  193.                     point.x += ship.getCollisionRadius() * ((float) Math.random() * 1.5f - 0.75f);
  194.                     point.y += ship.getCollisionRadius() * ((float) Math.random() * 1.5f - 0.75f);
  195.  
  196.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  197.                     float size = ((float) Math.random() * 30f + 100f) * sizeMod;
  198.  
  199.                     engine.spawnExplosion(point, vel, color, size, (ship.getCollisionRadius() / 25f) * ((float) Math.random() * 0.5f + 0.75f));
  200.                 }
  201.  
  202.                 /*int debris = (int) ((Float)(explosionSizeMod.get(ship.getHullSize())) * ship.getCollisionRadius() / 5f * ((float)Math.random() * 0.5f + 0.75f));
  203.                    
  204.                  for (int i = 0; i < debris; i++)
  205.                  {
  206.                  Vector2f point = new Vector2f(ship.getLocation());
  207.                  Vector2f vel = new Vector2f(ship.getVelocity());
  208.                  vel.x += (float)Math.random() * 20f - 10f;
  209.                  vel.y += (float)Math.random() * 20f - 10f;
  210.                  point.x += ship.getCollisionRadius() * ((float)Math.random() * 1.5f - 0.75f);
  211.                  point.y += ship.getCollisionRadius() * ((float)Math.random() * 1.5f - 0.75f);
  212.                  vel.x += (point.x - ship.getLocation().x) * (float)Math.random();
  213.                  vel.y += (point.y - ship.getLocation().y) * (float)Math.random();
  214.                        
  215.                  CombatEntityAPI chunk = engine.spawnProjectile(engine.getPlayerShip(), null, "debris", point, (float)Math.random() * 360f, vel);
  216.                  //chunk.setFacing((float)Math.atan2(vel.y, vel.x) * 180f / (float)Math.PI);
  217.                  }*/
  218.                 iter.remove();
  219.             }
  220.         }
  221.  
  222.         // Now for the juicy bit...
  223.         Iterator<ExplodingShip> iter2 = explodingShips.iterator();
  224.         while (iter2.hasNext()) {
  225.             ExplodingShip exploder = iter2.next();
  226.  
  227.             if (exploder.ship == null) {
  228.                 exploder.flamePoints.clear();
  229.                 iter2.remove();
  230.                 continue;
  231.             }
  232.  
  233.             // If the ship is vaporized, it gets removed from this graphics loop
  234.             if (!engine.getShips().contains(exploder.ship)) {
  235.                 exploder.flamePoints.clear();
  236.                 iter2.remove();
  237.                 continue;
  238.             }
  239.  
  240.             // Draw fire contrails from our burning wrecks
  241.             Iterator<FlamePoint> iter3 = exploder.flamePoints.iterator();
  242.             while (iter3.hasNext()) {
  243.                 FlamePoint flamePoint = iter3.next();
  244.  
  245.                 // Check if the flame contrail has expired yet
  246.                 if (!flamePoint.tick(amount)) {
  247.                     // Don't want to generate too many flames...
  248.                     if (interval.intervalElapsed()) {
  249.                         Vector2f point = new Vector2f(flamePoint.anchor.getLocation());
  250.                         Vector2f vel = new Vector2f();
  251.                         Vector2f vel2 = new Vector2f();
  252.  
  253.                         Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  254.                         Color color2 = new Color((int) ((float) Math.random() * 40f + 20f), (int) ((float) Math.random() * 20f + 20f), 20, (int) ((float) Math.random() * 30f + 30f));
  255.  
  256.                         float sizeMod = explosionSizeMod.get(exploder.ship.getHullSize());
  257.                         float size = ((float) Math.random() * 25f) * sizeMod * flamePoint.scale;
  258.                         float size2 = ((float) Math.random() * 10f + 40f) * sizeMod * flamePoint.scale;
  259.                         float size3 = ((float) Math.random() * 40f + 80f) * sizeMod * flamePoint.scale;
  260.                         float size4 = ((float) Math.random() * 15f + 5f) * flamePoint.scale;
  261.  
  262.                         vel.x += (float) Math.random() * 40f - 20f;
  263.                         vel.y += (float) Math.random() * 40f - 20f;
  264.                         vel2.x += (float) Math.random() * 20f - 10f;
  265.                         vel2.y += (float) Math.random() * 20f - 10f;
  266.  
  267.                         // Make fewer flames if the wreck is not moving as quickly
  268.                         if ((float) Math.random() <= (flamePoint.anchor.getVelocity().length() + 50f) / 75f) {
  269.                             engine.addHitParticle(point, flamePoint.anchor.getVelocity(), size4, 0.65f, (float) Math.random() * 0.15f + 0.3f, new Color(255, 225, 175));
  270.                             if ((float) Math.random() >= 0.5f) {
  271.                                 engine.spawnExplosion(point, vel, color, size, (float) Math.random() * 1f + 0.5f);
  272.                                 engine.addSmokeParticle(point, vel2, size2, (float) Math.random() * 0.025f + 0.1f, (float) Math.random() * 4f + 4f, color2);
  273.                             } else {
  274.                                 engine.addHitParticle(point, vel, size3, (float) Math.random() * 0.05f + 0.05f, (float) Math.random() * 0.5f + 0.25f, color);
  275.                             }
  276.                         }
  277.                     }
  278.                 } else {
  279.                     iter3.remove();
  280.                 }
  281.             }
  282.  
  283.             // If the ship is an inert wreck, we flag it for removal from the loop
  284.             if (exploder.flamePoints.isEmpty() && exploder.count <= 0) {
  285.                 iter2.remove();
  286.                 continue;
  287.             }
  288.  
  289.             if (!exploder.tick(amount)) {
  290.                 continue;
  291.             }
  292.  
  293.             if (exploder.count <= 0) {
  294.                 continue;
  295.             }
  296.  
  297.             ShipAPI ship = exploder.ship;
  298.  
  299.             // And now we make it explode nicely...
  300.             while (true) {
  301.                 Vector2f point = new Vector2f(ship.getLocation());
  302.                 point.x += ship.getCollisionRadius() * (((float) Math.random() * 2f) - 1);
  303.                 point.y += ship.getCollisionRadius() * (((float) Math.random() * 2f) - 1);
  304.  
  305.                 if (CollisionUtils.isPointWithinBounds(point, ship)) {
  306.                     Vector2f vel = new Vector2f(ship.getVelocity());
  307.                     Vector2f vel2 = new Vector2f(ship.getVelocity());
  308.                     float rand = (float) Math.random() * 0.5f + 0.5f;
  309.                     float sizeMod = explosionSizeMod.get(ship.getHullSize());
  310.                     float size = (30f + (float) Math.random() * 30f) * sizeMod;
  311.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  312.  
  313.                     vel.x *= rand;
  314.                     vel.y *= rand;
  315.                     vel2.x *= rand * 0.5f;
  316.                     vel2.y *= rand * 0.5f;
  317.  
  318.                     engine.spawnExplosion(point, vel, color, size, size / 25f);
  319.                     engine.addSmokeParticle(point, vel2, size, 0.25f, size / 10f, new Color(50, 40, 30, 90));
  320.                     //engine.applyDamage(ship, point, size, DamageType.HIGH_EXPLOSIVE, 0f, true, false, null);
  321.                     if ((float) Math.random() >= 0.75) {
  322.                         Global.getSoundPlayer().playSound("explosion_from_damage", 0.95f + (float) Math.random() * 0.05f, sizeMod / 2f, point, vel);
  323.                         engine.addHitParticle(point, new Vector2f(ship.getVelocity()), size * 3f, 1f, size / 60f, new Color(255, 255, 255, 255));
  324.  
  325.                         float length = explosionLength.get(ship.getHullSize()) * ((float) Math.random() * 5f + 2.5f);
  326.                         exploder.flamePoints.add(new FlamePoint(new AnchoredEntity(ship, point), length, (float) Math.random() * 1.3f + 0.35f));
  327.                     } else {
  328.                         engine.addHitParticle(point, new Vector2f(ship.getVelocity()), size, 0.5f, size / 60f, new Color(255, 255, 255, 255));
  329.                     }
  330.                     break;
  331.                 }
  332.             }
  333.  
  334.             exploder.count -= 1;
  335.         }
  336.     }
  337.  
  338.     public static final class ExplodingShip {
  339.  
  340.         public ShipAPI ship;
  341.         public int count;
  342.         public float interval;
  343.  
  344.         public float ticker;
  345.  
  346.         public List<FlamePoint> flamePoints;
  347.  
  348.         public ExplodingShip(ShipAPI ship, int count, float interval) {
  349.             this.ship = ship;
  350.             this.count = count;
  351.             this.interval = interval;
  352.             this.flamePoints = new ArrayList<>();
  353.             ticker = 0f;
  354.         }
  355.  
  356.         public boolean tick(float amount) {
  357.             ticker += amount;
  358.             if (ticker >= interval) {
  359.                 ticker -= interval;
  360.                 return true;
  361.             } else {
  362.                 return false;
  363.             }
  364.         }
  365.     }
  366.  
  367.     public static final class FlamePoint {
  368.  
  369.         public AnchoredEntity anchor;
  370.         public float time;
  371.         public float scale;
  372.  
  373.         public float ticker;
  374.  
  375.         public FlamePoint(AnchoredEntity anchor, float time, float scale) {
  376.             this.anchor = anchor;
  377.             this.time = time;
  378.             this.scale = scale;
  379.             ticker = 0f;
  380.         }
  381.  
  382.         public boolean tick(float amount) {
  383.             ticker += amount;
  384.             if (ticker >= time) {
  385.                 ticker -= time;
  386.                 return true;
  387.             } else {
  388.                 return false;
  389.             }
  390.         }
  391.     }
  392.  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement