Files
CustomNPC-Plus/src/main/java/kamkeel/npcs/entity/EntityAbilityLaser.java
T

550 lines
19 KiB
Java
Raw Normal View History

2026-01-25 20:39:01 -05:00
package kamkeel.npcs.entity;
2026-02-18 05:13:51 -05:00
import kamkeel.npcs.controllers.data.ability.data.EnergyCombatData;
import kamkeel.npcs.controllers.data.ability.data.EnergyDisplayData;
import kamkeel.npcs.controllers.data.ability.data.EnergyLifespanData;
import kamkeel.npcs.controllers.data.ability.data.EnergyLightningData;
import kamkeel.npcs.controllers.data.ability.data.EnergyTrajectoryData;
import kamkeel.npcs.util.AnchorPointHelper;
2026-01-25 20:39:01 -05:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
2026-02-18 05:13:51 -05:00
import noppes.npcs.EventHooks;
2026-01-25 20:39:01 -05:00
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Laser projectile - fast expanding thin line that pierces through multiple targets.
* No homing, travels in a straight line from origin to max distance.
2026-02-18 05:13:51 -05:00
* <p>
2026-01-25 20:39:01 -05:00
* Design inspired by LouisXIV's energy attack system.
*/
public class EntityAbilityLaser extends EntityEnergyProjectile {
2026-01-25 20:39:01 -05:00
// Laser-specific properties
private float laserWidth = 0.2f;
private float expansionSpeed = 2.0f; // Blocks per tick
private int lingerTicks = 10; // How long laser stays visible after reaching max
// State
private float currentLength = 0.0f;
private boolean fullyExtended = false;
private int ticksSinceFullExtension = 0;
// Direction (normalized)
private double dirX, dirY, dirZ;
2026-02-06 16:28:38 -05:00
// Lock vertical direction after firing (only update yaw, keep pitch fixed)
private boolean lockVerticalDirection = false;
2026-02-18 14:10:55 -05:00
// Whether to die on first entity impact (hit-scan mode)
private boolean dieOnImpact = false;
2026-01-25 20:39:01 -05:00
// Track hit entities to avoid double-damage
private Set<Integer> hitEntities = new HashSet<>();
// End point for rendering
private double endX, endY, endZ;
public EntityAbilityLaser(World world) {
super(world);
}
/**
2026-01-29 14:19:57 -05:00
* Full constructor with all parameters using data classes.
2026-01-25 20:39:01 -05:00
*/
2026-01-30 05:01:54 -05:00
public EntityAbilityLaser(World world, EntityLivingBase owner, EntityLivingBase target,
double x, double y, double z,
float laserWidth,
2026-02-03 17:20:36 -03:00
EnergyDisplayData display, EnergyCombatData combat,
EnergyLightningData lightning, EnergyLifespanData lifespan,
2026-02-03 21:26:27 -03:00
EnergyTrajectoryData trajectory, float expansionSpeed, int lingerTicks) {
2026-01-25 20:39:01 -05:00
super(world);
2026-01-29 14:19:57 -05:00
// Initialize base properties (laser doesn't rotate)
2026-02-03 21:26:27 -03:00
initProjectile(owner, target, x, y, z, laserWidth, display, combat, lightning, lifespan, trajectory);
2026-02-03 17:20:36 -03:00
this.displayData.rotationSpeed = 0.0f;
2026-01-25 20:39:01 -05:00
// Laser-specific properties
this.laserWidth = laserWidth;
this.expansionSpeed = expansionSpeed;
this.lingerTicks = lingerTicks;
// Calculate direction toward target or forward
if (target != null) {
double dx = target.posX - x;
double dy = (target.posY + target.getEyeHeight() - 0.4) - y;
2026-01-25 20:39:01 -05:00
double dz = target.posZ - z;
double len = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (len > 0) {
this.dirX = dx / len;
this.dirY = dy / len;
this.dirZ = dz / len;
}
} else {
// Fire in NPC's facing direction
float yaw = (float) Math.toRadians(owner.rotationYaw);
float pitch = (float) Math.toRadians(owner.rotationPitch);
this.dirX = -Math.sin(yaw) * Math.cos(pitch);
this.dirY = -Math.sin(pitch);
this.dirZ = Math.cos(yaw) * Math.cos(pitch);
}
// Initialize end point
this.endX = x;
this.endY = y;
this.endZ = z;
}
@Override
protected void updateRotation() {
// Laser doesn't rotate
}
@Override
protected boolean checkMaxDistance() {
// Laser handles distance differently - it expands to max then lingers
return false;
}
@Override
protected void updateProjectile() {
if (isCharging()) {
updateCharging();
return;
}
2026-02-06 16:28:38 -05:00
// Track owner's anchor position and rotation each tick
// so the laser follows the NPC's facing direction
updateLaserOriginAndDirection();
2026-01-25 20:39:01 -05:00
if (!fullyExtended) {
// Expand the laser
currentLength += expansionSpeed;
2026-02-03 17:20:36 -03:00
if (currentLength >= getMaxDistance()) {
currentLength = getMaxDistance();
2026-01-25 20:39:01 -05:00
fullyExtended = true;
}
// Update end point
endX = startX + dirX * currentLength;
endY = startY + dirY * currentLength;
endZ = startZ + dirZ * currentLength;
// Check for block collision along the new segment
if (!worldObj.isRemote) {
checkBlockCollision();
checkEntityCollisionAlongLine();
}
} else {
// Laser is fully extended, count down linger time
ticksSinceFullExtension++;
2026-02-06 16:28:38 -05:00
// Update end point during linger (origin/direction may still change)
endX = startX + dirX * currentLength;
endY = startY + dirY * currentLength;
endZ = startZ + dirZ * currentLength;
2026-01-25 20:39:01 -05:00
if (ticksSinceFullExtension >= lingerTicks) {
if (!worldObj.isRemote) {
EventHooks.onEnergyProjectileExpired(this);
}
2026-01-25 20:39:01 -05:00
this.setDead();
}
}
}
2026-02-06 16:28:38 -05:00
/**
* Update laser origin and direction from the owner's current anchor position and rotation.
* Called each tick so the laser follows the NPC when tracking a target.
*/
private void updateLaserOriginAndDirection() {
Entity owner = getOwnerEntity();
if (owner == null || !(owner instanceof EntityLivingBase)) return;
2026-02-06 16:28:38 -05:00
EntityLivingBase livingOwner = (EntityLivingBase) owner;
// Update origin from anchor point
if (anchorData != null) {
Vec3 pos = AnchorPointHelper.calculateAnchorPosition(livingOwner, anchorData);
startX = pos.xCoord;
startY = pos.yCoord;
startZ = pos.zCoord;
} else {
startX = owner.posX;
startY = owner.posY + owner.height * 0.7;
startZ = owner.posZ;
}
// Update direction from owner's current rotation
float yaw = (float) Math.toRadians(owner.rotationYaw);
if (lockVerticalDirection) {
// Only update horizontal direction (yaw), keep vertical (pitch) fixed
double horizontalLen = Math.sqrt(dirX * dirX + dirZ * dirZ);
if (horizontalLen < 0.001) horizontalLen = 1.0;
dirX = -Math.sin(yaw) * horizontalLen;
dirZ = Math.cos(yaw) * horizontalLen;
} else {
float pitch = (float) Math.toRadians(owner.rotationPitch);
dirX = -Math.sin(yaw) * Math.cos(pitch);
dirY = -Math.sin(pitch);
dirZ = Math.cos(yaw) * Math.cos(pitch);
}
// Keep entity positioned at origin
prevPosX = startX;
prevPosY = startY;
prevPosZ = startZ;
setPosition(startX, startY, startZ);
}
public void startMoving(EntityLivingBase target) {
setCharging(false);
2026-02-06 16:28:38 -05:00
// Initialize from current anchor position
// updateLaserOriginAndDirection() will keep these updated each tick
startX = posX;
startY = posY;
startZ = posZ;
2026-02-06 16:28:38 -05:00
// Set initial direction from owner's rotation
2026-02-03 17:20:36 -03:00
Entity owner = getOwnerEntity();
if (owner != null) {
float yaw = (float) Math.toRadians(owner.rotationYaw);
float pitch = (float) Math.toRadians(owner.rotationPitch);
this.dirX = -Math.sin(yaw) * Math.cos(pitch);
this.dirY = -Math.sin(pitch);
this.dirZ = Math.cos(yaw) * Math.cos(pitch);
} else if (target != null) {
double dx = target.posX - startX;
double dy = (target.posY + target.getEyeHeight() - 0.4) - startY;
double dz = target.posZ - startZ;
double len = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (len > 0) {
this.dirX = dx / len;
this.dirY = dy / len;
this.dirZ = dz / len;
}
}
2026-02-06 16:28:38 -05:00
// Initialize end point at start (will expand from here)
this.endX = startX;
this.endY = startY;
this.endZ = startZ;
}
2026-01-25 20:39:01 -05:00
private void checkBlockCollision() {
Vec3 start = Vec3.createVectorHelper(startX, startY, startZ);
Vec3 end = Vec3.createVectorHelper(endX, endY, endZ);
2026-01-28 17:16:59 -05:00
// Use full raytrace that doesn't stop at liquids and checks all blocks
MovingObjectPosition blockHit = worldObj.func_147447_a(start, end, false, true, false);
2026-01-25 20:39:01 -05:00
if (blockHit != null && blockHit.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
if (!worldObj.isRemote) {
EventHooks.onEnergyProjectileBlockImpact(this, blockHit.blockX, blockHit.blockY, blockHit.blockZ);
}
2026-01-25 20:39:01 -05:00
// Laser stops at block
currentLength = (float) Math.sqrt(
(blockHit.hitVec.xCoord - startX) * (blockHit.hitVec.xCoord - startX) +
2026-02-18 05:13:51 -05:00
(blockHit.hitVec.yCoord - startY) * (blockHit.hitVec.yCoord - startY) +
(blockHit.hitVec.zCoord - startZ) * (blockHit.hitVec.zCoord - startZ)
2026-01-25 20:39:01 -05:00
);
endX = blockHit.hitVec.xCoord;
endY = blockHit.hitVec.yCoord;
endZ = blockHit.hitVec.zCoord;
fullyExtended = true;
2026-02-03 17:20:36 -03:00
if (isExplosive()) {
2026-01-25 20:39:01 -05:00
// Explode at hit point
double oldPosX = posX;
double oldPosY = posY;
double oldPosZ = posZ;
posX = endX;
posY = endY;
posZ = endZ;
doExplosion();
posX = oldPosX;
posY = oldPosY;
posZ = oldPosZ;
}
}
}
private void checkEntityCollisionAlongLine() {
2026-02-06 16:28:38 -05:00
// Expand search AABB by entity sizes to ensure we find all potential targets
double expand = Math.max(laserWidth, 1.0);
double minX = Math.min(startX, endX) - expand;
double minY = Math.min(startY, endY) - expand;
double minZ = Math.min(startZ, endZ) - expand;
double maxX = Math.max(startX, endX) + expand;
double maxY = Math.max(startY, endY) + expand;
double maxZ = Math.max(startZ, endZ) + expand;
2026-01-25 20:39:01 -05:00
AxisAlignedBB searchBox = AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
@SuppressWarnings("unchecked")
List<EntityLivingBase> entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, searchBox);
for (EntityLivingBase entity : entities) {
if (shouldIgnoreEntity(entity)) continue;
if (hitEntities.contains(entity.getEntityId())) continue;
2026-02-06 16:28:38 -05:00
// Check if entity's bounding box intersects with the laser line
2026-01-25 20:39:01 -05:00
if (isEntityOnLine(entity)) {
hitEntities.add(entity.getEntityId());
2026-02-01 01:50:39 -03:00
applyDamage(entity);
2026-01-25 20:39:01 -05:00
2026-02-18 14:10:55 -05:00
if (dieOnImpact) {
// Stop at impact point and begin linger/fade
double dx = entity.posX - startX;
double dy = (entity.posY + entity.height * 0.5) - startY;
double dz = entity.posZ - startZ;
float impactDist = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
currentLength = Math.min(impactDist, currentLength);
endX = startX + dirX * currentLength;
endY = startY + dirY * currentLength;
endZ = startZ + dirZ * currentLength;
fullyExtended = true;
return; // Stop checking further entities
}
2026-01-25 20:39:01 -05:00
// Piercing - don't stop, continue to next entity
}
}
}
/**
2026-02-06 16:28:38 -05:00
* Check if an entity's bounding box intersects with the laser line.
* Uses separate XZ (horizontal) and Y (vertical) checks against
* the entity's full bounding box rather than just its center point.
2026-01-25 20:39:01 -05:00
*/
private boolean isEntityOnLine(EntityLivingBase entity) {
double ex = entity.posX;
double ez = entity.posZ;
2026-02-06 16:28:38 -05:00
// Project entity XZ position onto the laser line direction
2026-01-25 20:39:01 -05:00
double vx = ex - startX;
double vz = ez - startZ;
2026-02-06 16:28:38 -05:00
// Project using horizontal direction components only
double dirLenXZ = Math.sqrt(dirX * dirX + dirZ * dirZ);
double dot;
if (dirLenXZ > 0.001) {
dot = (vx * dirX + vz * dirZ) / (dirLenXZ * dirLenXZ) * dirLenXZ;
// Remap to full 3D parameter
dot = (vx * dirX + vz * dirZ + (entity.posY + entity.height * 0.5 - startY) * dirY);
} else {
// Laser fires nearly straight up/down - use full 3D projection
dot = vx * dirX + (entity.posY + entity.height * 0.5 - startY) * dirY + vz * dirZ;
}
2026-01-25 20:39:01 -05:00
dot = Math.max(0, Math.min(dot, currentLength));
2026-02-06 16:28:38 -05:00
// Closest point on laser line at this parameter
2026-01-25 20:39:01 -05:00
double closestX = startX + dirX * dot;
double closestY = startY + dirY * dot;
double closestZ = startZ + dirZ * dot;
2026-02-06 16:28:38 -05:00
// XZ distance check: entity center vs laser line point
// Use minimum collision width so visually thin lasers still hit reliably
double xzDistSq = (ex - closestX) * (ex - closestX) + (ez - closestZ) * (ez - closestZ);
double effectiveWidth = Math.max(laserWidth, 0.5);
double hitRadiusXZ = effectiveWidth * 0.5 + entity.width * 0.5;
if (xzDistSq > hitRadiusXZ * hitRadiusXZ) return false;
// Y overlap check: laser point vs entity's full height range
double entityMinY = entity.boundingBox.minY;
double entityMaxY = entity.boundingBox.maxY;
double laserHalfWidth = effectiveWidth * 0.5;
// Check if laser Y is within entity's height range (expanded by laser half-width)
if (closestY < entityMinY - laserHalfWidth) return false;
if (closestY > entityMaxY + laserHalfWidth) return false;
2026-01-25 20:39:01 -05:00
2026-02-06 16:28:38 -05:00
return true;
2026-01-25 20:39:01 -05:00
}
// ==================== GETTERS FOR RENDERER ====================
2026-02-06 16:28:38 -05:00
public void setLockVerticalDirection(boolean lock) {
this.lockVerticalDirection = lock;
}
public boolean isLockVerticalDirection() {
return lockVerticalDirection;
}
2026-01-25 20:39:01 -05:00
public float getLaserWidth() {
return laserWidth;
}
public void setLaserWidth(float width) {
this.laserWidth = width;
}
public float getExpansionSpeed() {
return expansionSpeed;
}
public void setExpansionSpeed(float speed) {
this.expansionSpeed = speed;
}
public int getLingerTicks() {
return lingerTicks;
}
public void setLingerTicks(int ticks) {
this.lingerTicks = ticks;
}
2026-02-18 14:10:55 -05:00
public boolean isDieOnImpact() {
return dieOnImpact;
}
public void setDieOnImpact(boolean dieOnImpact) {
this.dieOnImpact = dieOnImpact;
}
public void setDirection(double x, double y, double z) {
this.dirX = x;
this.dirY = y;
this.dirZ = z;
}
2026-01-25 20:39:01 -05:00
public float getCurrentLength() {
return currentLength;
}
public boolean isFullyExtended() {
return fullyExtended;
}
public double getStartX() {
return startX;
}
public double getStartY() {
return startY;
}
public double getStartZ() {
return startZ;
}
public double getEndX() {
return endX;
}
public double getEndY() {
return endY;
}
public double getEndZ() {
return endZ;
}
public double getDirX() {
return dirX;
}
public double getDirY() {
return dirY;
}
public double getDirZ() {
return dirZ;
}
/**
* Get the alpha for fade-out effect during linger.
*/
public float getLingerAlpha() {
if (!fullyExtended) return 1.0f;
return 1.0f - ((float) ticksSinceFullExtension / lingerTicks);
}
// ==================== NBT ====================
@Override
protected void readProjectileNBT(NBTTagCompound nbt) {
this.laserWidth = nbt.hasKey("LaserWidth") ? nbt.getFloat("LaserWidth") : 0.2f;
this.expansionSpeed = nbt.hasKey("ExpansionSpeed") ? nbt.getFloat("ExpansionSpeed") : 2.0f;
this.lingerTicks = nbt.hasKey("LingerTicks") ? nbt.getInteger("LingerTicks") : 10;
2026-02-18 14:10:55 -05:00
this.dieOnImpact = nbt.getBoolean("DieOnImpact");
2026-01-25 20:39:01 -05:00
this.dirX = nbt.getDouble("DirX");
this.dirY = nbt.getDouble("DirY");
this.dirZ = nbt.getDouble("DirZ");
this.currentLength = nbt.getFloat("CurrentLength");
this.fullyExtended = nbt.getBoolean("FullyExtended");
this.endX = nbt.getDouble("EndX");
this.endY = nbt.getDouble("EndY");
this.endZ = nbt.getDouble("EndZ");
readChargingNBT(nbt);
2026-01-25 20:39:01 -05:00
}
@Override
protected void writeProjectileNBT(NBTTagCompound nbt) {
nbt.setFloat("LaserWidth", laserWidth);
nbt.setFloat("ExpansionSpeed", expansionSpeed);
nbt.setInteger("LingerTicks", lingerTicks);
2026-02-18 14:10:55 -05:00
nbt.setBoolean("DieOnImpact", dieOnImpact);
2026-01-25 20:39:01 -05:00
nbt.setDouble("DirX", dirX);
nbt.setDouble("DirY", dirY);
nbt.setDouble("DirZ", dirZ);
nbt.setFloat("CurrentLength", currentLength);
nbt.setBoolean("FullyExtended", fullyExtended);
nbt.setDouble("EndX", endX);
nbt.setDouble("EndY", endY);
nbt.setDouble("EndZ", endZ);
writeChargingNBT(nbt);
2026-01-25 20:39:01 -05:00
}
2026-01-28 20:43:32 -05:00
/**
2026-01-29 14:19:57 -05:00
* Setup this laser in preview mode for GUI display.
2026-01-28 20:43:32 -05:00
* Laser doesn't have charging state - spawns at active phase and fires immediately.
*/
2026-02-03 17:20:36 -03:00
public void setupPreview(EntityLivingBase owner, float laserWidth, EnergyDisplayData display,
2026-01-29 14:19:57 -05:00
EnergyLightningData lightning, float expansionSpeed, float maxDistance) {
this.setPreviewMode(true);
this.setPreviewOwner(owner);
2026-01-28 20:43:32 -05:00
// Set visual properties
2026-01-29 14:19:57 -05:00
this.laserWidth = laserWidth;
this.size = laserWidth;
2026-02-03 17:20:36 -03:00
this.displayData = display;
2026-01-29 14:19:57 -05:00
this.expansionSpeed = expansionSpeed;
2026-02-03 17:20:36 -03:00
this.lifespanData.maxDistance = Math.min(maxDistance, 5.0f); // Limit for GUI preview
this.lightningData = lightning;
2026-01-28 20:43:32 -05:00
// Position at chest height
double x = owner.posX;
double y = owner.posY + owner.height * 0.7;
double z = owner.posZ;
2026-01-29 14:19:57 -05:00
this.setPosition(x, y, z);
this.prevPosX = x;
this.prevPosY = y;
this.prevPosZ = z;
this.startX = x;
this.startY = y;
this.startZ = z;
2026-01-28 20:43:32 -05:00
// Fire in owner's facing direction
float yaw = (float) Math.toRadians(owner.rotationYaw);
2026-01-29 14:19:57 -05:00
this.dirX = -Math.sin(yaw);
this.dirY = 0;
this.dirZ = Math.cos(yaw);
2026-01-28 20:43:32 -05:00
// Initialize end point (same as start, will expand)
2026-01-29 14:19:57 -05:00
this.endX = x;
this.endY = y;
this.endZ = z;
2026-01-28 20:43:32 -05:00
}
2026-01-25 20:39:01 -05:00
}