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.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 targetVec = new Vector3d(target.posX, target.posY, target.posZ);
- Vector3d usVec = new Vector3d(posX, posY, posZ);
- homingVec = new Vector3d(targetVec);
- homingVec.sub(usVec);
- Vector3d axis = new Vector3d();
- axis.cross(usVec, targetVec);
- Matrix4d transform = new Matrix4d();
- transform.set(new AxisAngle4d(axis, Math.PI / 4));
- transform.transform();
- // old homing code
- // 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