Fix Firing Energy Projectiles and Make Charging State purely visual

This commit is contained in:
Kam committed 2026-02-22 03:07:37 -05:00
1 parent 3a8f829de9
commit 18968bf7c7
18 files changed
+1086 -439

No files matched your search

@@ -18,6 +18,7 @@ import net.minecraft.world.World;
import noppes.npcs.LogWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -31,6 +32,11 @@ import java.util.List;
*/
public class EntityAbilityBeam extends EntityEnergyProjectile {
private enum BeamMode {
ANCHORED,
FREE_TRAIL
}
// Beam shape properties
private float beamWidth = 0.3f;
private float headSize = 0.5f;
@@ -39,22 +45,18 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
private List<Vec3> trailPoints = new ArrayList<>();
private static final int MAX_TRAIL_POINTS = 200;
private static final double MIN_POINT_DISTANCE = 0.2;
private static final int TRAIL_COMPACT_THRESHOLD = 128;
private int trailStartIndex = 0;
// Head position relative to origin
private double headOffsetX, headOffsetY, headOffsetZ;
private double prevHeadOffsetX, prevHeadOffsetY, prevHeadOffsetZ;
// Origin stays fixed (or follows owner)
private boolean attachedToOwner = true;
// Whether to render tail orb (only when anchored)
private boolean renderTailOrb = true;
// Beam behavior mode (single source of truth).
private BeamMode beamMode = BeamMode.ANCHORED;
// Charging state (beam-specific)
private float chargeOffsetDistance = 1.0f;
// Trail fading for non-anchored beams (comet effect)
private boolean fadeTrail = false;
private int trailFadeTime = 20; // Ticks for trail to fully fade
private List<Integer> trailPointAges = new ArrayList<>();
@@ -65,6 +67,46 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
super(world);
}
private static BeamMode modeFromAnchored(boolean anchored) {
return anchored ? BeamMode.ANCHORED : BeamMode.FREE_TRAIL;
}
private boolean isAnchoredMode() {
return beamMode == BeamMode.ANCHORED;
}
private boolean isFadingMode() {
return beamMode == BeamMode.FREE_TRAIL;
}
private int getActiveTrailSize() {
return Math.max(0, trailPoints.size() - trailStartIndex);
}
private void resetTrailStorage() {
trailPoints.clear();
trailPointAges.clear();
trailStartIndex = 0;
}
private void setBeamMode(BeamMode mode) {
beamMode = mode != null ? mode : BeamMode.ANCHORED;
if (isFadingMode()) {
while (trailPointAges.size() < trailPoints.size()) {
trailPointAges.add(0);
}
while (trailPointAges.size() > trailPoints.size()) {
trailPointAges.remove(trailPointAges.size() - 1);
}
} else {
if (trailStartIndex > 0 && trailStartIndex < trailPoints.size()) {
trailPoints = new ArrayList<Vec3>(trailPoints.subList(trailStartIndex, trailPoints.size()));
}
trailStartIndex = 0;
trailPointAges.clear();
}
}
/**
* Full constructor with all parameters using data classes.
*
@@ -89,9 +131,8 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
this.beamWidth = beamWidth;
this.headSize = headSize;
// Anchored mode controls whether origin follows owner and tail orb is rendered
this.attachedToOwner = anchoredMode;
this.renderTailOrb = anchoredMode;
// Beam mode controls tail attachment/orb/fading behavior.
setBeamMode(modeFromAnchored(anchoredMode));
// Initialize head offset at origin (0,0,0 relative)
this.headOffsetX = 0;
@@ -118,7 +159,6 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
this.chargeTick = 0;
this.chargeOffsetDistance = chargeOffsetDistance;
this.anchorData = anchor;
this.fadeTrail = !attachedToOwner;
this.motionX = 0;
this.motionY = 0;
this.motionZ = 0;
@@ -160,10 +200,6 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
this.prevHeadOffsetY = 0;
this.prevHeadOffsetZ = 0;
// Attach to owner for anchor following
this.attachedToOwner = true;
this.renderTailOrb = true;
// Clear motion
this.motionX = 0;
this.motionY = 0;
@@ -174,12 +210,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
* Start preview firing (simulates firing toward a point in front of NPC).
*/
public void startPreviewFiring() {
setCharging(false);
// Origin (tail) stays at the charged position
startX = posX;
startY = posY;
startZ = posZ;
beginLookVectorLaunch(true);
// Reset head offset to origin
headOffsetX = 0;
@@ -189,51 +220,21 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
prevHeadOffsetY = 0;
prevHeadOffsetZ = 0;
// Origin no longer follows owner after firing
attachedToOwner = false;
// Sync prev position to prevent visual jump on first frame
prevPosX = posX;
prevPosY = posY;
prevPosZ = posZ;
// Reset trail
trailPoints.clear();
trailPointAges.clear();
resetTrailStorage();
trailPoints.add(Vec3.createVectorHelper(0, 0, 0));
if (isFadingMode()) trailPointAges.add(0);
// Fire forward based on owner facing direction
Entity owner = getOwnerEntity();
if (owner != null) {
float yaw = (float) Math.toRadians(owner.rotationYaw);
motionX = -Math.sin(yaw) * getSpeed();
motionY = 0;
motionZ = Math.cos(yaw) * getSpeed();
} else {
motionX = getSpeed();
motionY = 0;
motionZ = 0;
}
// Fire along look vector for parity with real launch behavior.
setMotionAlongLookVectorOrFallback(getSpeed(), getSpeed(), 0, 0);
}
/**
* Start the beam firing (exit charging mode).
* Called by ability when windup ends.
* <p>
* For anchored beams: origin follows owner, head starts at charged position
* For non-anchored beams: origin fixed at charged position, head starts there
*/
public void startFiring(EntityLivingBase target) {
setCharging(false);
// For player casters, snap to look vector for crosshair-aligned launch
snapToPlayerLookVector();
// Origin (tail) stays at the charged position - where the orb was
// This is the same for both anchored and non-anchored modes
startX = posX;
startY = posY;
startZ = posZ;
beginLookVectorLaunch(false);
// Head starts at the origin (tail position)
headOffsetX = 0;
@@ -243,36 +244,13 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
prevHeadOffsetY = 0;
prevHeadOffsetZ = 0;
// Origin no longer follows owner after firing starts
// (for anchored mode, tail is fixed in space; for non-anchored, there's no tail)
attachedToOwner = false;
// Initialize trail with just the origin point
trailPoints.clear();
trailPointAges.clear();
resetTrailStorage();
trailPoints.add(Vec3.createVectorHelper(0, 0, 0));
if (fadeTrail) trailPointAges.add(0);
if (isFadingMode()) trailPointAges.add(0);
// Calculate velocity toward target (head starts at origin = startX/Y/Z)
Entity owner = getOwnerEntity();
if (target != null) {
double dx = target.posX - startX;
double dy = (target.posY + target.getEyeHeight()) - startY;
double dz = target.posZ - startZ;
double len = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (len > 0) {
motionX = (dx / len) * getSpeed();
motionY = (dy / len) * getSpeed();
motionZ = (dz / len) * getSpeed();
}
} else if (owner != null) {
float yaw = (float) Math.toRadians(owner.rotationYaw);
float pitch = (float) Math.toRadians(owner.rotationPitch);
motionX = -Math.sin(yaw) * Math.cos(pitch) * getSpeed();
motionY = -Math.sin(pitch) * getSpeed();
motionZ = Math.cos(yaw) * Math.cos(pitch) * getSpeed();
}
// Calculate velocity toward target when provided; otherwise along look vector.
setMotionTowardTargetOrLookVector(target, startX, startY, startZ, getSpeed(), getSpeed(), 0, 0);
if (DEBUG_LOGGING && !worldObj.isRemote) {
LogWriter.info("[Beam] startFiring: origin=(" + startX + "," + startY + "," + startZ +
@@ -310,7 +288,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
prevHeadOffsetZ = headOffsetZ;
// Age trail points for fading effect
if (fadeTrail) {
if (isFadingMode()) {
ageTrailPoints();
}
@@ -344,7 +322,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
headOffsetZ += motionZ;
// Update origin if attached to owner
if (attachedToOwner) {
if (isAnchoredMode()) {
Entity owner = getOwnerEntity();
if (owner != null) {
startX = owner.posX;
@@ -372,7 +350,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
String.format("%.2f", headOffsetZ) + ") origin=(" +
String.format("%.2f", startX) + "," +
String.format("%.2f", startY) + "," +
String.format("%.2f", startZ) + ") trail=" + trailPoints.size());
String.format("%.2f", startZ) + ") trail=" + getActiveTrailSize());
}
// Skip collision checks on first few ticks
@@ -418,25 +396,43 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
* Age trail points and remove old ones (for comet effect).
*/
private void ageTrailPoints() {
// Age all trail points
for (int i = 0; i < trailPointAges.size(); i++) {
if (trailStartIndex >= trailPoints.size()) {
resetTrailStorage();
return;
}
// Age all active trail points.
for (int i = trailStartIndex; i < trailPointAges.size(); i++) {
trailPointAges.set(i, trailPointAges.get(i) + 1);
}
// Remove trail points that have fully faded
while (!trailPointAges.isEmpty() && trailPointAges.get(0) >= trailFadeTime) {
trailPointAges.remove(0);
if (!trailPoints.isEmpty()) {
trailPoints.remove(0);
}
// Move logical start index forward for fully faded points.
while (trailStartIndex < trailPointAges.size() && trailPointAges.get(trailStartIndex) >= trailFadeTime) {
trailStartIndex++;
}
if (trailStartIndex >= trailPoints.size()) {
resetTrailStorage();
return;
}
// Compact stale prefix to avoid unbounded growth of dead entries.
if (trailStartIndex >= TRAIL_COMPACT_THRESHOLD && trailStartIndex * 2 >= trailPoints.size()) {
trailPoints = new ArrayList<Vec3>(trailPoints.subList(trailStartIndex, trailPoints.size()));
trailPointAges = new ArrayList<Integer>(trailPointAges.subList(trailStartIndex, trailPointAges.size()));
trailStartIndex = 0;
}
}
private void addTrailPoint() {
if (trailStartIndex >= trailPoints.size()) {
resetTrailStorage();
}
// Trail points are RELATIVE to origin
if (trailPoints.isEmpty()) {
trailPoints.add(Vec3.createVectorHelper(headOffsetX, headOffsetY, headOffsetZ));
if (fadeTrail) {
if (isFadingMode()) {
trailPointAges.add(0);
}
return;
@@ -451,12 +447,12 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
if (dist >= MIN_POINT_DISTANCE) {
trailPoints.add(Vec3.createVectorHelper(headOffsetX, headOffsetY, headOffsetZ));
if (fadeTrail) {
if (isFadingMode()) {
trailPointAges.add(0);
}
// Limit trail length (only if not using fading - fading handles its own cleanup)
if (!fadeTrail) {
if (!isFadingMode()) {
while (trailPoints.size() > MAX_TRAIL_POINTS) {
trailPoints.remove(0);
}
@@ -474,7 +470,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
headOffsetZ += motionZ;
// Update origin if attached to owner
if (attachedToOwner) {
if (isAnchoredMode()) {
Entity owner = getOwnerEntity();
if (owner != null) {
startX = owner.posX;
@@ -586,7 +582,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
}
public void setAttachedToOwner(boolean attached) {
this.attachedToOwner = attached;
setBeamMode(modeFromAnchored(attached));
}
/**
@@ -609,7 +605,13 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
* Get trail points. These are RELATIVE to origin (startX/Y/Z).
*/
public List<Vec3> getTrailPoints() {
return trailPoints;
if (trailStartIndex <= 0) {
return trailPoints;
}
if (trailStartIndex >= trailPoints.size()) {
return Collections.emptyList();
}
return trailPoints.subList(trailStartIndex, trailPoints.size());
}
public double getOriginX() {
@@ -628,28 +630,34 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
* Whether the tail orb should be rendered (only true in anchored mode).
*/
public boolean shouldRenderTailOrb() {
return renderTailOrb;
return isAnchoredMode();
}
/**
* Whether the beam is attached to its owner (anchored mode).
*/
public boolean isAttachedToOwner() {
return attachedToOwner;
return isAnchoredMode();
}
/**
* Whether trail should fade (comet effect for non-anchored beams).
*/
public boolean hasFadingTrail() {
return fadeTrail;
return isFadingMode();
}
/**
* Get trail point ages for fading calculation.
*/
public List<Integer> getTrailPointAges() {
return trailPointAges;
if (trailStartIndex <= 0) {
return trailPointAges;
}
if (trailStartIndex >= trailPointAges.size()) {
return Collections.emptyList();
}
return trailPointAges.subList(trailStartIndex, trailPointAges.size());
}
/**
@@ -671,17 +679,26 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
this.prevHeadOffsetX = this.headOffsetX;
this.prevHeadOffsetY = this.headOffsetY;
this.prevHeadOffsetZ = this.headOffsetZ;
this.attachedToOwner = !nbt.hasKey("AttachedToOwner") || nbt.getBoolean("AttachedToOwner");
this.renderTailOrb = !nbt.hasKey("RenderTailOrb") || nbt.getBoolean("RenderTailOrb");
readChargingNBT(nbt);
this.chargeOffsetDistance = nbt.hasKey("ChargeOffsetDistance") ? nbt.getFloat("ChargeOffsetDistance") : 1.0f;
this.fadeTrail = nbt.hasKey("FadeTrail") && nbt.getBoolean("FadeTrail");
this.trailFadeTime = nbt.hasKey("TrailFadeTime") ? nbt.getInteger("TrailFadeTime") : 20;
boolean attachedLegacy = !nbt.hasKey("AttachedToOwner") || nbt.getBoolean("AttachedToOwner");
BeamMode loadedMode = modeFromAnchored(attachedLegacy);
if (nbt.hasKey("BeamMode")) {
try {
loadedMode = BeamMode.valueOf(nbt.getString("BeamMode"));
} catch (IllegalArgumentException ignored) {
loadedMode = modeFromAnchored(attachedLegacy);
}
} else if (nbt.hasKey("FadeTrail")) {
loadedMode = nbt.getBoolean("FadeTrail") ? BeamMode.FREE_TRAIL : modeFromAnchored(attachedLegacy);
}
// Read trail points (relative to origin)
trailPoints.clear();
trailPointAges.clear();
trailStartIndex = 0;
if (nbt.hasKey("Trail")) {
NBTTagList trailList = nbt.getTagList("Trail", 10);
for (int i = 0; i < trailList.tagCount(); i++) {
@@ -701,10 +718,7 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
trailPointAges.add(age);
}
}
// If trail points exist but ages don't (legacy data), initialize ages to 0
while (trailPointAges.size() < trailPoints.size()) {
trailPointAges.add(0);
}
setBeamMode(loadedMode);
}
@Override
@@ -714,16 +728,18 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
nbt.setDouble("HeadOffsetX", headOffsetX);
nbt.setDouble("HeadOffsetY", headOffsetY);
nbt.setDouble("HeadOffsetZ", headOffsetZ);
nbt.setBoolean("AttachedToOwner", attachedToOwner);
nbt.setBoolean("RenderTailOrb", renderTailOrb);
nbt.setBoolean("AttachedToOwner", isAnchoredMode());
nbt.setBoolean("RenderTailOrb", isAnchoredMode());
nbt.setString("BeamMode", beamMode.name());
writeChargingNBT(nbt);
nbt.setFloat("ChargeOffsetDistance", chargeOffsetDistance);
nbt.setBoolean("FadeTrail", fadeTrail);
nbt.setBoolean("FadeTrail", isFadingMode());
nbt.setInteger("TrailFadeTime", trailFadeTime);
// Write trail points
NBTTagList trailList = new NBTTagList();
for (Vec3 point : trailPoints) {
for (int i = trailStartIndex; i < trailPoints.size(); i++) {
Vec3 point = trailPoints.get(i);
NBTTagCompound pointNbt = new NBTTagCompound();
pointNbt.setDouble("X", point.xCoord);
pointNbt.setDouble("Y", point.yCoord);
@@ -733,9 +749,11 @@ public class EntityAbilityBeam extends EntityEnergyProjectile {
nbt.setTag("Trail", trailList);
// Write trail point ages for fading trail sync
int[] ages = new int[trailPointAges.size()];
for (int i = 0; i < trailPointAges.size(); i++) {
ages[i] = trailPointAges.get(i);
int activeSize = isFadingMode() ? Math.max(0, trailPoints.size() - trailStartIndex) : 0;
int[] ages = new int[activeSize];
for (int i = 0; i < activeSize; i++) {
int idx = trailStartIndex + i;
ages[i] = idx < trailPointAges.size() ? trailPointAges.get(idx) : 0;
}
nbt.setIntArray("TrailAges", ages);
}