package kamkeel.npcs.entity; import kamkeel.npcs.controllers.data.ability.data.energy.EnergyAnchorData; import kamkeel.npcs.controllers.data.ability.data.energy.EnergyCombatData; import kamkeel.npcs.controllers.data.ability.data.energy.EnergyDisplayData; import kamkeel.npcs.controllers.data.ability.data.energy.EnergyLifespanData; import kamkeel.npcs.controllers.data.ability.data.energy.EnergyLightningData; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; 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; import noppes.npcs.EventHooks; import noppes.npcs.entity.EntityNPCInterface; import java.util.List; /** * Laser projectile - fast expanding thin line. * No homing, travels in a straight line from origin to max distance. *
* Design inspired by LouisXIV's energy attack system.
*/
public class EntityAbilityLaser extends EntityEnergyProjectile {
// 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;
// Lock vertical direction after firing (only update yaw, keep pitch fixed)
private boolean lockVerticalDirection = false;
private int reflectedLockTicks = 0;
// End point for rendering
private double endX, endY, endZ;
public EntityAbilityLaser(World world) {
super(world);
}
/**
* Full constructor with all parameters using data classes.
*/
public EntityAbilityLaser(World world, EntityLivingBase owner, EntityLivingBase target,
double x, double y, double z,
float laserWidth,
EnergyDisplayData display, EnergyCombatData combat,
EnergyLightningData lightning, EnergyLifespanData lifespan,
float expansionSpeed, int lingerTicks) {
super(world);
// Initialize base properties (laser doesn't rotate)
initProjectile(owner, target, x, y, z, laserWidth, display, combat, lightning, lifespan);
this.displayData.rotationSpeed = 0.0f;
// 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;
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 {
// Fall back to owner look direction so launch matches visual aim.
Vec3 look = owner == null ? null : owner.getLookVec();
if (look != null) {
this.dirX = look.xCoord;
this.dirY = look.yCoord;
this.dirZ = look.zCoord;
} else 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 {
this.dirX = 1.0;
this.dirY = 0.0;
this.dirZ = 0.0;
}
}
// Initialize end point
this.endX = x;
this.endY = y;
this.endZ = z;
}
@Override
@SideOnly(Side.CLIENT)
public boolean isInRangeToRenderDist(double distance) {
double range = Math.max(128.0D, currentLength * 2.0D + 64.0D);
return distance < range * range;
}
@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;
}
// After reflection, hold trajectory briefly so the rebound is visible.
// Once reflected, never resume owner-tracking — the laser flies in a fixed direction.
if (reflectedLockTicks > 0) {
reflectedLockTicks--;
} else if (!reflected) {
// Track owner's anchor position and rotation each tick
// so the laser follows the NPC's facing direction
updateLaserOriginAndDirection();
}
if (!fullyExtended) {
// Expand the laser
currentLength += expansionSpeed;
if (currentLength >= getMaxDistance()) {
currentLength = getMaxDistance();
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();
// Re-evaluate barriers against the current beam segment before entity hits.
// This prevents first-tick pass-through where the pre-update barrier check
// still sees a zero-length beam.
if (checkBarrierCollisionForCurrentSegment()) {
return;
}
checkEntityCollisionAlongLine();
}
} else {
// Laser is fully extended, count down linger time
ticksSinceFullExtension++;
// Update end point during linger (origin/direction may still change)
endX = startX + dirX * currentLength;
endY = startY + dirY * currentLength;
endZ = startZ + dirZ * currentLength;
if (ticksSinceFullExtension >= lingerTicks) {
if (!worldObj.isRemote) {
EventHooks.onEnergyProjectileExpired(this);
}
this.setDead();
}
}
}
/**
* Update laser origin and direction from the owner's current look rotation.
* Called each tick so the laser origin stays centered on look vector.
*/
private void updateLaserOriginAndDirection() {
Entity owner = getOwnerEntity();
if (owner == null || !(owner instanceof EntityLivingBase)) {
// On client, fall back to entity tracker interpolation when owner not loaded
if (worldObj != null && worldObj.isRemote) {
handleClientInterpolation();
}
return;
}
EntityLivingBase livingOwner = (EntityLivingBase) owner;
// Use owner look vector (head/eye aim) for direction updates so hit logic matches visuals.
Vec3 look = livingOwner.getLookVec();
if (look == null) {
float yaw = (float) Math.toRadians(owner.rotationYaw);
float pitch = (float) Math.toRadians(owner.rotationPitch);
look = Vec3.createVectorHelper(
-Math.sin(yaw) * Math.cos(pitch),
-Math.sin(pitch),
Math.cos(yaw) * Math.cos(pitch)
);
}
if (lockVerticalDirection) {
// Keep current vertical component, but align horizontal direction to current look.
double lookHorizontalLen = Math.sqrt(look.xCoord * look.xCoord + look.zCoord * look.zCoord);
if (lookHorizontalLen > 0.0001) {
double currentHorizontalLen = Math.sqrt(dirX * dirX + dirZ * dirZ);
if (currentHorizontalLen < 0.0001) {
currentHorizontalLen = Math.sqrt(Math.max(0.0001, 1.0 - dirY * dirY));
}
dirX = (look.xCoord / lookHorizontalLen) * currentHorizontalLen;
dirZ = (look.zCoord / lookHorizontalLen) * currentHorizontalLen;
}
} else {
dirX = look.xCoord;
dirY = look.yCoord;
dirZ = look.zCoord;
}
// Keep direction normalized for consistent expansion and collision math.
Vec3 direction = Vec3.createVectorHelper(dirX, dirY, dirZ);
double dirLen = Math.sqrt(direction.xCoord * direction.xCoord + direction.yCoord * direction.yCoord + direction.zCoord * direction.zCoord);
if (dirLen > 0.0001) {
direction = Vec3.createVectorHelper(
direction.xCoord / dirLen,
direction.yCoord / dirLen,
direction.zCoord / dirLen
);
} else {
direction = look != null ? look : Vec3.createVectorHelper(1.0, 0.0, 0.0);
}
dirX = direction.xCoord;
dirY = direction.yCoord;
dirZ = direction.zCoord;
// Keep origin centered on look vector while active and clear owner bbox.
setLookVectorLaunchPosition(livingOwner, direction, false);
syncStartPositionToCurrent();
syncPositionStateToCurrent(false);
}
public void startMoving(EntityLivingBase target) {
beginLookVectorLaunch(false);
currentLength = 0.0f;
fullyExtended = false;
ticksSinceFullExtension = 0;
// Set initial direction: target-based when available, otherwise owner look vector.
Vec3 look = getOwnerLookVector();
if (!setDirectionTowardTarget(target, startX, startY, startZ)) {
if (look != null) {
dirX = look.xCoord;
dirY = look.yCoord;
dirZ = look.zCoord;
} else {
dirX = 1.0;
dirY = 0.0;
dirZ = 0.0;
}
}
// Initialize end point at start (will expand from here)
this.endX = startX;
this.endY = startY;
this.endZ = startZ;
}
private boolean setDirectionTowardTarget(EntityLivingBase target, double sourceX, double sourceY, double sourceZ) {
if (target == null) return false;
double dx = target.posX - sourceX;
double dy = (target.posY + target.getEyeHeight() - 0.4) - sourceY;
double dz = target.posZ - sourceZ;
double len = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (len <= 0.0001) return false;
dirX = dx / len;
dirY = dy / len;
dirZ = dz / len;
return true;
}
private void checkBlockCollision() {
MovingObjectPosition blockHit = rayTraceBlocks(startX, startY, startZ, endX, endY, endZ);
if (blockHit != null && blockHit.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
if (!worldObj.isRemote) {
EventHooks.onEnergyProjectileBlockImpact(this, blockHit.blockX, blockHit.blockY, blockHit.blockZ);
}
// Laser stops at block
currentLength = (float) Math.sqrt(
(blockHit.hitVec.xCoord - startX) * (blockHit.hitVec.xCoord - startX) +
(blockHit.hitVec.yCoord - startY) * (blockHit.hitVec.yCoord - startY) +
(blockHit.hitVec.zCoord - startZ) * (blockHit.hitVec.zCoord - startZ)
);
endX = blockHit.hitVec.xCoord;
endY = blockHit.hitVec.yCoord;
endZ = blockHit.hitVec.zCoord;
fullyExtended = true;
if (isExplosive()) {
// 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() {
// 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;
AxisAlignedBB searchBox = AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
@SuppressWarnings("unchecked")
List