Advertisement
DarkRevenant

Untitled

May 21st, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.64 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.CombatEntityAPI;
  6. import com.fs.starfarer.api.combat.DamageType;
  7. import com.fs.starfarer.api.combat.DamagingProjectileAPI;
  8. import com.fs.starfarer.api.combat.EveryFrameCombatPlugin;
  9. import com.fs.starfarer.api.combat.ShipAPI;
  10. import com.fs.starfarer.api.input.InputEventAPI;
  11. import java.util.Iterator;
  12. import java.util.LinkedHashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15.  
  16. import org.lwjgl.util.vector.Vector2f;
  17.  
  18. import org.dark.shaders.distortion.DistortionShader;
  19. import org.dark.shaders.distortion.RippleDistortion;
  20. import org.dark.shaders.distortion.DistortionAPI;
  21. import org.dark.shaders.distortion.WaveDistortion;
  22. import org.lazywizard.lazylib.VectorUtils;
  23.  
  24. public class DistortionsPlugin implements EveryFrameCombatPlugin {
  25.  
  26.     public CombatEngineAPI engine;
  27.     public static final Map<DamagingProjectileAPI, DistortionAPI> projectiles = new LinkedHashMap<>();
  28.  
  29.     @Override
  30.     public void init(CombatEngineAPI engine) {
  31.     }
  32.  
  33.     @Override
  34.     public void advance(float amount, List<InputEventAPI> events) {
  35.         if (engine != Global.getCombatEngine()) {
  36.             projectiles.clear();
  37.             this.engine = Global.getCombatEngine();
  38.         }
  39.  
  40.         if (engine.isPaused()) {
  41.             return;
  42.         }
  43.  
  44.         List<DamagingProjectileAPI> activeProjectiles = engine.getProjectiles();
  45.         for (DamagingProjectileAPI projectile : activeProjectiles) {
  46.             if (projectile.didDamage() || projectile.isFading()) {
  47.                 continue;
  48.             }
  49.  
  50.             if (!projectiles.containsKey(projectile)) {
  51.                 if (projectile.getProjectileSpecId() != null && projectile.getProjectileSpecId().equals("mjolnir_shot")) {
  52.                     WaveDistortion wave = new WaveDistortion();
  53.                     wave.setIntensity(5f);
  54.                     wave.setSize(50f);
  55.                     wave.setLocation(projectile.getLocation());
  56.                     wave.flip(true);
  57.                     DistortionShader.addDistortion(wave);
  58.                     projectiles.put(projectile, wave);
  59.                 } else {
  60.                     projectiles.put(projectile, null);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         Iterator<Map.Entry<DamagingProjectileAPI, DistortionAPI>> iter = projectiles.entrySet().iterator();
  66.         while (iter.hasNext()) {
  67.             Map.Entry<DamagingProjectileAPI, DistortionAPI> entry = iter.next();
  68.             DamagingProjectileAPI projectile = entry.getKey();
  69.  
  70.             if (projectile.didDamage()) {
  71.                 CombatEntityAPI target = projectile.getDamageTarget();
  72.  
  73.                 if (target instanceof ShipAPI) {
  74.                     if (target.getShield() != null && target.getShield().isOn() && target.getShield().isWithinArc(projectile.getLocation())) {
  75.                         createHitRipple(projectile.getLocation(), target.getVelocity(), projectile.getDamageAmount(), projectile.getDamageType(), VectorUtils.getFacing(VectorUtils.getDirectionalVector(target.getShield().getLocation(), projectile.getLocation())), target.getShield().getRadius());
  76.                     }
  77.                 }
  78.  
  79.                 if (entry.getValue() != null) {
  80.                     if (projectile.getProjectileSpecId().equals("mjolnir_shot")) {
  81.                         WaveDistortion wave = (WaveDistortion) entry.getValue();
  82.                         if (!wave.isFading()) {
  83.                             wave.fadeOutIntensity(0.2f);
  84.                         }
  85.                     }
  86.                 }
  87.  
  88.                 iter.remove();
  89.             } else if (projectile.isFading()) {
  90.                 if (entry.getValue() != null) {
  91.                     if (projectile.getProjectileSpecId().equals("mjolnir_shot")) {
  92.                         WaveDistortion wave = (WaveDistortion) entry.getValue();
  93.                         if (!wave.isFading()) {
  94.                             wave.fadeOutIntensity(0.2f);
  95.                         }
  96.                     }
  97.                 }
  98.             } else if (!engine.isEntityInPlay(projectile)) {
  99.                 if (entry.getValue() != null) {
  100.                     if (projectile.getProjectileSpecId().equals("mjolnir_shot")) {
  101.                         WaveDistortion wave = (WaveDistortion) entry.getValue();
  102.                         wave.setLifetime(0f);
  103.                     }
  104.                 }
  105.                 iter.remove();
  106.             } else if (entry.getValue() != null) {
  107.                 if (projectile.getProjectileSpecId().equals("mjolnir_shot")) {
  108.                     WaveDistortion wave = (WaveDistortion) entry.getValue();
  109.                     wave.setLocation(projectile.getLocation());
  110.                 }
  111.             }
  112.         }
  113.     }
  114.  
  115.     private void createHitRipple(Vector2f location, Vector2f velocity, float damage, DamageType type, float direction, float shieldRadius) {
  116.         if (type == DamageType.FRAGMENTATION) {
  117.             damage *= 0.25f;
  118.         }
  119.         if (type == DamageType.HIGH_EXPLOSIVE) {
  120.             damage *= 0.5f;
  121.         }
  122.         if (type == DamageType.KINETIC) {
  123.             damage *= 2f;
  124.         }
  125.  
  126.         if (damage < 50f) {
  127.             return;
  128.         }
  129.  
  130.         float fadeTime = (float) Math.pow(damage, 0.25) * 0.1f;
  131.         float size = (float) Math.pow(damage, 0.3333333) * 15f;
  132.  
  133.         float ratio = Math.min(size / shieldRadius, 1f);
  134.         float arc = 90f - ratio * 14.54136f; // Don't question the magic number
  135.  
  136.         float start = direction - arc;
  137.         if (start < 0f) {
  138.             start += 360f;
  139.         }
  140.         float end = direction + arc;
  141.         if (end >= 360f) {
  142.             end -= 360f;
  143.         }
  144.  
  145.         RippleDistortion ripple = new RippleDistortion();
  146.         ripple.setSize(size);
  147.         ripple.setIntensity(size * 0.3f);
  148.         ripple.setLocation(location);
  149.         ripple.setVelocity(velocity);
  150.         ripple.setFrameRate(60f / fadeTime);
  151.         ripple.fadeInSize(fadeTime * 1.2f);
  152.         ripple.fadeOutIntensity(fadeTime);
  153.         ripple.setSize(size * 0.2f);
  154.         ripple.setArc(start, end);
  155.         DistortionShader.addDistortion(ripple);
  156.  
  157.         ripple = new RippleDistortion();
  158.         ripple.setSize(size);
  159.         ripple.setIntensity(size * 0.1f);
  160.         ripple.setLocation(location);
  161.         ripple.setVelocity(velocity);
  162.         ripple.setFrameRate(60f / fadeTime);
  163.         ripple.fadeInSize(fadeTime * 1.2f);
  164.         ripple.fadeOutIntensity(fadeTime);
  165.         ripple.setSize(size * 0.2f);
  166.         ripple.setArc(end, start);
  167.         DistortionShader.addDistortion(ripple);
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement