//Based on: Electrical Damage Plugin, by Psiyon
package data.scripts.plugins;
import java.awt.Color;
import java.util.Iterator;
import java.util.List;
import org.lwjgl.util.vector.Vector2f;
import com.fs.starfarer.api.combat.CombatEngineAPI;
import com.fs.starfarer.api.combat.EveryFrameCombatPlugin;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.util.IntervalUtil;
import org.lazywizard.lazylib.CollisionUtils;
import org.lazywizard.lazylib.MathUtils;
public class ExplosionDamageShipEffect implements EveryFrameCombatPlugin
{
/**
* Set this to true to have the plugin actually do stuff.
*/
private static boolean TEST_MODE = true;
//Generates an interval between 0.5 and 3 seconds.
private IntervalUtil interval = new IntervalUtil(0.5f, 3f);
private CombatEngineAPI engine;
@Override
public void init(CombatEngineAPI engine)
{
this.engine = engine;
}
@Override
public void advance(float amount, List events)
{
//Makes sure this code doesn't run if test mode is inactive, or if the game is paused.
if (!TEST_MODE)
{
return;
}
if (engine.isPaused())
{
return;
}
//Advances the interval.
interval.advance(amount);
//When the interval has elapsed...
if (interval.intervalElapsed())
{
//cycles through all the ships in play.
List ships = engine.getShips();
Iterator it = ships.iterator();
//While there are still more ships to cycle through...
while (it.hasNext())
{
//loads the current ship the iterator is on into the object "ship"
ShipAPI ship = (ShipAPI) it.next();
//If the ship is disabled or is a fighter, we don't want to bother with arcs, so we'll start the loop over
if (ship.isHulk())
{
continue;
}
if (ship.isFighter())
{
continue;
}
//If the ship has less than an eigth of its HP
//if (ship.getHitpoints() <= ship.getMaxHitpoints() / 2.5f) {
//Gets the (x,y) for the center of the ship in the map, for example if the map
//Has 20000x20000 and the ship is in the middle of the map, its center would be 10000 x and 10000 y.
Vector2f ShipCoords = getRandomPointInBounds(ship);
// Colors for the effects
Color explosioncolor = new Color(155, 100, 25, 255);
Color explosionsmoke = new Color(255, 255, 255, 255);
//spawns the explosion and other effects
engine.addHitParticle(ShipCoords, ship.getVelocity(), 1f, 1f, 2f, explosionsmoke);
// addSmoothParticle(Vector2f loc, Vector2f vel, float size, float brightness, float duration, Color color);
engine.addSmokeParticle(ShipCoords, ship.getVelocity(), 1f, 255f, 5f, explosionsmoke);
// addSmokeParticle(Vector2f loc, Vector2f vel, float size, float opacity, float duration, Color color);
engine.spawnExplosion(ShipCoords, ship.getVelocity(), explosioncolor, 2f, 3f);
// spawnExplosion(Vector2f loc, Vector2f vel, Color color, float size, float maxDuration);
}
}
}
private static Vector2f getRandomPointInBounds(ShipAPI ship)
{
int tries = 0;
Vector2f point = new Vector2f();
// Pick random points in the collision circle until one is inside the ship's bounds
do
{
point.set(MathUtils.getRandomPointInCircle(ship.getLocation(),
ship.getCollisionRadius()));
if (CollisionUtils.isPointWithinBounds(point, ship))
{
return point;
}
tries++;
}
while (tries < 30); // HIGHLY unlikely it will take more than two or three tries
// Too many tries, give up and just return the center
return ship.getLocation();
}
}