Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package moze_intel.projecte.gameObjs.entity;
- import moze_intel.projecte.utils.PELogger;
- import moze_intel.projecte.utils.WorldHelper;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.projectile.EntityArrow;
- import net.minecraft.util.AxisAlignedBB;
- import net.minecraft.util.MathHelper;
- import net.minecraft.world.World;
- import javax.vecmath.AxisAngle4d;
- import javax.vecmath.Matrix4d;
- import javax.vecmath.Matrix4f;
- import javax.vecmath.Quat4f;
- import javax.vecmath.Vector3d;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class EntityHomingArrow extends EntityArrow
- {
- private static final int DW_TARGET_ID = 31;
- private static final int NO_TARGET = -1;
- private int newTargetCooldown = 0;
- private Vector3d homingVec = null;
- // The position this arrow should point in for a direct path to the target
- public EntityHomingArrow(World world)
- {
- super(world);
- }
- public EntityHomingArrow(World world, EntityLivingBase par2, float par3)
- {
- super(world, par2, par3);
- }
- @Override
- public void entityInit()
- {
- super.entityInit();
- setIsCritical(true);
- dataWatcher.addObject(DW_TARGET_ID, NO_TARGET); // Target entity id
- }
- @Override
- public void onUpdate()
- {
- boolean inGround = WorldHelper.isArrowInGround(this);
- if (!worldObj.isRemote)
- {
- if (hasTarget() && (!getTarget().isEntityAlive() || inGround))
- {
- dataWatcher.updateObject(DW_TARGET_ID, NO_TARGET);
- PELogger.logInfo("Removing target");
- }
- if (!hasTarget() && !inGround && newTargetCooldown <= 0)
- {
- PELogger.logInfo("Finding new target");
- findNewTarget();
- } else
- {
- newTargetCooldown--;
- }
- }
- if (!hasTarget())
- {
- super.onUpdate();
- return;
- }
- AxisAlignedBB box = this.boundingBox;
- if (!WorldHelper.isArrowInGround(this))
- {
- worldObj.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);
- Entity target = getTarget();
- Vector3d arrowLoc = new Vector3d(posX, posY, posZ);
- Vector3d targetLoc = new Vector3d(target.posX, target.posY, target.posZ);
- Vector3d lookVec = new Vector3d(arrowLoc);
- lookVec.sub(targetLoc);
- // CoolAlias's solution (doesn't work)
- // System.out.printf("before %f, %f, %f%n", posX, posY, posZ);
- // double arcRate = 0.25D;
- // double dx = posX - lookVec.x;
- // double dy = posY - lookVec.y;
- // double dz = posZ - lookVec.z;
- // posX += dx * arcRate;
- // posY += dy * arcRate;
- // posZ += dz * arcRate;
- // setPosition(posX, posY, posZ);
- // System.out.printf("after %f, %f, %f%n", posX, posY, posZ);
- double angleYaw = Math.atan2(lookVec.z, lookVec.x) - Math.PI/2d;
- double anglePitch = Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));
- double dYaw = MathHelper.wrapAngleTo180_double(this.rotationYaw - angleYaw);
- double dPitch = MathHelper.wrapAngleTo180_double(this.rotationPitch - anglePitch);
- AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
- AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
- Quat4f rot = new Quat4f(0, 0, 0, 1);
- Quat4f yawQuat = new Quat4f();
- Quat4f pitchQuat = new Quat4f();
- yawQuat.set(yaw);
- rot.mul(yawQuat);
- pitchQuat.set(pitch);
- rot.mul(pitchQuat);
- Matrix4d matrix = new Matrix4d();
- matrix.setIdentity();
- matrix.setRotation(rot);
- Vector3d apply = new Vector3d(arrowLoc);
- matrix.get(apply);
- posX = apply.x;
- posY = apply.y;
- posZ = apply.z;
- setPosition(posX, posY, posZ);
- this.rotationPitch += dPitch;
- this.rotationYaw += dYaw;
- // old homing code (sucks)
- // double d5 = target.posX - this.posX;
- // double d6 = target.boundingBox.minY + target.height - this.posY;
- // double d7 = target.posZ - this.posZ;
- //
- // this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
- // super.onUpdate();
- }
- }
- private void findNewTarget()
- {
- List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
- Collections.sort(candidates, new Comparator<EntityLiving>() {
- @Override
- public int compare(EntityLiving o1, EntityLiving o2) {
- double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
- if (dist == 0.0)
- {
- return 0;
- } else
- {
- return dist > 0.0 ? 1 : -1;
- }
- }
- });
- if (!candidates.isEmpty())
- {
- dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
- PELogger.logInfo("Found new target");
- }
- newTargetCooldown = 5;
- }
- private EntityLiving getTarget()
- {
- return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
- }
- private boolean hasTarget()
- {
- return getTarget() != null;
- }
- private void updateHeading(double x, double y, double z)
- {
- float f3 = MathHelper.sqrt_double(x * x + z * z);
- this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(x, z) * 180.0D / Math.PI);
- this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(y, (double)f3) * 180.0D / Math.PI);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement