Advertisement
Guest User

Untitled

a guest
Dec 18th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package moze_intel.projecte.gameObjs.entity;
  2.  
  3. import moze_intel.projecte.utils.PELogger;
  4. import moze_intel.projecte.utils.WorldHelper;
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.entity.EntityLiving;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.projectile.EntityArrow;
  9. import net.minecraft.util.AxisAlignedBB;
  10. import net.minecraft.util.MathHelper;
  11. import net.minecraft.world.World;
  12.  
  13. import javax.vecmath.AxisAngle4d;
  14. import javax.vecmath.Matrix4d;
  15. import javax.vecmath.Vector3d;
  16. import java.util.Collections;
  17. import java.util.Comparator;
  18. import java.util.List;
  19.  
  20. public class EntityHomingArrow extends EntityArrow
  21. {
  22. private static final int DW_TARGET_ID = 31;
  23. private static final int NO_TARGET = -1;
  24.  
  25. private int newTargetCooldown = 0;
  26. private Vector3d homingVec = null;
  27. // The position this arrow should point in for a direct path to the target
  28.  
  29. public EntityHomingArrow(World world)
  30. {
  31. super(world);
  32. }
  33.  
  34. public EntityHomingArrow(World world, EntityLivingBase par2, float par3)
  35. {
  36. super(world, par2, par3);
  37. }
  38.  
  39. @Override
  40. public void entityInit()
  41. {
  42. super.entityInit();
  43. setIsCritical(true);
  44. dataWatcher.addObject(DW_TARGET_ID, NO_TARGET); // Target entity id
  45. }
  46.  
  47. @Override
  48. public void onUpdate()
  49. {
  50. boolean inGround = WorldHelper.isArrowInGround(this);
  51. if (!worldObj.isRemote)
  52. {
  53. if (hasTarget() && (!getTarget().isEntityAlive() || inGround))
  54. {
  55. dataWatcher.updateObject(DW_TARGET_ID, NO_TARGET);
  56. PELogger.logInfo("Removing target");
  57. }
  58.  
  59. if (!hasTarget() && !inGround && newTargetCooldown <= 0)
  60. {
  61. PELogger.logInfo("Finding new target");
  62. findNewTarget();
  63. } else
  64. {
  65. newTargetCooldown--;
  66. }
  67. }
  68.  
  69. if (!hasTarget())
  70. {
  71. super.onUpdate();
  72. return;
  73. }
  74.  
  75. AxisAlignedBB box = this.boundingBox;
  76.  
  77. if (!WorldHelper.isArrowInGround(this))
  78. {
  79. worldObj.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);
  80. Entity target = getTarget();
  81.  
  82.  
  83. Vector3d targetVec = new Vector3d(target.posX, target.posY, target.posZ);
  84. Vector3d usVec = new Vector3d(posX, posY, posZ);
  85.  
  86. homingVec = new Vector3d(targetVec);
  87. homingVec.sub(usVec);
  88.  
  89. Vector3d axis = new Vector3d();
  90. axis.cross(usVec, targetVec);
  91.  
  92. Matrix4d transform = new Matrix4d();
  93. transform.set(new AxisAngle4d(axis, Math.PI / 4));
  94.  
  95. transform.transform();
  96.  
  97. // old homing code
  98. // double d5 = target.posX - this.posX;
  99. // double d6 = target.boundingBox.minY + target.height - this.posY;
  100. // double d7 = target.posZ - this.posZ;
  101. //
  102. // this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
  103. // super.onUpdate();
  104. }
  105. }
  106.  
  107. private void findNewTarget()
  108. {
  109. List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
  110. Collections.sort(candidates, new Comparator<EntityLiving>() {
  111. @Override
  112. public int compare(EntityLiving o1, EntityLiving o2) {
  113. double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
  114. if (dist == 0.0)
  115. {
  116. return 0;
  117. } else
  118. {
  119. return dist > 0.0 ? 1 : -1;
  120. }
  121. }
  122. });
  123.  
  124. if (!candidates.isEmpty())
  125. {
  126. dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
  127. PELogger.logInfo("Found new target");
  128. }
  129.  
  130. newTargetCooldown = 5;
  131. }
  132.  
  133. private EntityLiving getTarget()
  134. {
  135. return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
  136. }
  137.  
  138. private boolean hasTarget()
  139. {
  140. return getTarget() != null;
  141. }
  142.  
  143. private void updateHeading(double x, double y, double z)
  144. {
  145. float f3 = MathHelper.sqrt_double(x * x + z * z);
  146. this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(x, z) * 180.0D / Math.PI);
  147. this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(y, (double)f3) * 180.0D / Math.PI);
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement