Starsector API
Loading...
Searching...
No Matches
RiftBeamEffect.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.combat;
2
3import java.util.Iterator;
4import java.util.List;
5
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.combat.BeamAPI;
10import com.fs.starfarer.api.combat.CombatEngineAPI;
11import com.fs.starfarer.api.combat.CombatEntityAPI;
12import com.fs.starfarer.api.combat.EveryFrameWeaponEffectPlugin;
13import com.fs.starfarer.api.combat.MissileAPI;
14import com.fs.starfarer.api.combat.ShipAPI;
15import com.fs.starfarer.api.combat.WeaponAPI;
16import com.fs.starfarer.api.combat.WeaponAPI.WeaponType;
17import com.fs.starfarer.api.util.IntervalUtil;
18import com.fs.starfarer.api.util.Misc;
19import com.fs.starfarer.api.util.WeightedRandomPicker;
20
21public class RiftBeamEffect implements EveryFrameWeaponEffectPlugin {
22
23 public static float TARGET_RANGE = 100f;
24 public static float RIFT_RANGE = 50f;
25
26 protected IntervalUtil interval = new IntervalUtil(0.8f, 1.2f);
27
28 public RiftBeamEffect() {
29 interval.setElapsed((float) Math.random() * interval.getIntervalDuration());
30 }
31
32 //public void advance(float amount, CombatEngineAPI engine, BeamAPI beam) {
33 public void advance(float amount, CombatEngineAPI engine, WeaponAPI weapon) {
34 List<BeamAPI> beams = weapon.getBeams();
35 if (beams.isEmpty()) return;
36 BeamAPI beam = beams.get(0);
37 if (beam.getBrightness() < 1f) return;
38
39 interval.advance(amount * 2f);
40 if (interval.intervalElapsed()) {
41 if (beam.getLengthPrevFrame() < 10) return;
42
43 Vector2f loc;
44 CombatEntityAPI target = findTarget(beam, beam.getWeapon(), engine);
45 if (target == null) {
46 loc = pickNoTargetDest(beam, beam.getWeapon(), engine);
47 } else {
48 loc = target.getLocation();
49 }
50
51 Vector2f from = Misc.closestPointOnSegmentToPoint(beam.getFrom(), beam.getRayEndPrevFrame(), loc);
52 Vector2f to = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(from, loc));
53 //to.scale(Math.max(RIFT_RANGE * 0.5f, Math.min(Misc.getDistance(from, loc), RIFT_RANGE)));
54 to.scale(Math.min(Misc.getDistance(from, loc), RIFT_RANGE));
55 Vector2f.add(from, to, to);
56
57 spawnMine(beam.getSource(), to);
58// float thickness = beam.getWidth();
59// EmpArcEntityAPI arc = engine.spawnEmpArcVisual(from, null, to, null, thickness, beam.getFringeColor(), Color.white);
60// arc.setCoreWidthOverride(Math.max(20f, thickness * 0.67f));
61 //Global.getSoundPlayer().playSound("tachyon_lance_emp_impact", 1f, 1f, arc.getLocation(), arc.getVelocity());
62 }
63 }
64
65 public void spawnMine(ShipAPI source, Vector2f mineLoc) {
66 CombatEngineAPI engine = Global.getCombatEngine();
67
68
69 //Vector2f currLoc = mineLoc;
70 MissileAPI mine = (MissileAPI) engine.spawnProjectile(source, null,
71 "riftbeam_minelayer",
72 mineLoc,
73 (float) Math.random() * 360f, null);
74 if (source != null) {
75 Global.getCombatEngine().applyDamageModifiersToSpawnedProjectileWithNullWeapon(
76 source, WeaponType.MISSILE, false, mine.getDamage());
77 }
78
79
80 float fadeInTime = 0.05f;
81 mine.getVelocity().scale(0);
82 mine.fadeOutThenIn(fadeInTime);
83
84 float liveTime = 0f;
85 //liveTime = 0.01f;
86 mine.setFlightTime(mine.getMaxFlightTime() - liveTime);
87 mine.addDamagedAlready(source);
88 mine.setNoMineFFConcerns(true);
89 }
90
91 public Vector2f pickNoTargetDest(BeamAPI beam, WeaponAPI weapon, CombatEngineAPI engine) {
92 Vector2f from = beam.getFrom();
93 Vector2f to = beam.getRayEndPrevFrame();
94 float length = beam.getLengthPrevFrame();
95
96 float f = 0.25f + (float) Math.random() * 0.75f;
97 Vector2f loc = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(from, to));
98 loc.scale(length * f);
99 Vector2f.add(from, loc, loc);
100
101 return Misc.getPointWithinRadius(loc, RIFT_RANGE);
102 }
103
104 public CombatEntityAPI findTarget(BeamAPI beam, WeaponAPI weapon, CombatEngineAPI engine) {
105 Vector2f to = beam.getRayEndPrevFrame();
106
107 Iterator<Object> iter = Global.getCombatEngine().getAllObjectGrid().getCheckIterator(to,
108 RIFT_RANGE * 2f, RIFT_RANGE * 2f);
109 int owner = weapon.getShip().getOwner();
110 WeightedRandomPicker<CombatEntityAPI> picker = new WeightedRandomPicker<CombatEntityAPI>();
111 while (iter.hasNext()) {
112 Object o = iter.next();
113 if (!(o instanceof MissileAPI) &&
114 !(o instanceof ShipAPI)) continue;
115 CombatEntityAPI other = (CombatEntityAPI) o;
116 if (other.getOwner() == owner) continue;
117 if (other instanceof ShipAPI) {
118 ShipAPI ship = (ShipAPI) other;
119 if (!ship.isFighter() && !ship.isDrone()) continue;
120 }
121
122 float radius = Misc.getTargetingRadius(to, other, false);
123 Vector2f p = Misc.closestPointOnSegmentToPoint(beam.getFrom(), beam.getRayEndPrevFrame(), other.getLocation());
124 float dist = Misc.getDistance(p, other.getLocation()) - radius;
125 if (dist > TARGET_RANGE) continue;
126
127 picker.add(other);
128
129 }
130 return picker.pick();
131 }
132
133}
134
135
136
137
138
static CombatEngineAPI getCombatEngine()
Definition Global.java:63
void spawnMine(ShipAPI source, Vector2f mineLoc)
Vector2f pickNoTargetDest(BeamAPI beam, WeaponAPI weapon, CombatEngineAPI engine)
void advance(float amount, CombatEngineAPI engine, WeaponAPI weapon)
CombatEntityAPI findTarget(BeamAPI beam, WeaponAPI weapon, CombatEngineAPI engine)