| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.entity.EntityLiving;
|
| 5 | import net.minecraft.entity.EntityLivingBase;
|
| 6 | import net.minecraft.util.MathHelper;
|
| 7 |
|
| 8 | public class EntityLookHelper
|
| 9 | {
|
| 10 | private EntityLiving entity;
|
| 11 |
|
| 12 | /**
|
| 13 | * The amount of change that is made each update for an entity facing a direction.
|
| 14 | */
|
| 15 | private float deltaLookYaw;
|
| 16 |
|
| 17 | /**
|
| 18 | * The amount of change that is made each update for an entity facing a direction.
|
| 19 | */
|
| 20 | private float deltaLookPitch;
|
| 21 |
|
| 22 | /** Whether or not the entity is trying to look at something. */
|
| 23 | private boolean isLooking;
|
| 24 | private double posX;
|
| 25 | private double posY;
|
| 26 | private double posZ;
|
| 27 |
|
| 28 | public EntityLookHelper(EntityLiving entitylivingIn)
|
| 29 | {
|
| 30 | this.entity = entitylivingIn;
|
| 31 | }
|
| 32 |
|
| 33 | /**
|
| 34 | * Sets position to look at using entity
|
| 35 | */
|
| 36 | public void setLookPositionWithEntity(Entity entityIn, float deltaYaw, float deltaPitch)
|
| 37 | {
|
| 38 | this.posX = entityIn.posX;
|
| 39 |
|
| 40 | if (entityIn instanceof EntityLivingBase)
|
| 41 | {
|
| 42 | this.posY = entityIn.posY + (double)entityIn.getEyeHeight();
|
| 43 | }
|
| 44 | else
|
| 45 | {
|
| 46 | this.posY = (entityIn.getEntityBoundingBox().minY + entityIn.getEntityBoundingBox().maxY) / 2.0D;
|
| 47 | }
|
| 48 |
|
| 49 | this.posZ = entityIn.posZ;
|
| 50 | this.deltaLookYaw = deltaYaw;
|
| 51 | this.deltaLookPitch = deltaPitch;
|
| 52 | this.isLooking = true;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * Sets position to look at
|
| 57 | */
|
| 58 | public void setLookPosition(double x, double y, double z, float deltaYaw, float deltaPitch)
|
| 59 | {
|
| 60 | this.posX = x;
|
| 61 | this.posY = y;
|
| 62 | this.posZ = z;
|
| 63 | this.deltaLookYaw = deltaYaw;
|
| 64 | this.deltaLookPitch = deltaPitch;
|
| 65 | this.isLooking = true;
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
| 69 | * Updates look
|
| 70 | */
|
| 71 | public void onUpdateLook()
|
| 72 | {
|
| 73 | this.entity.rotationPitch = 0.0F;
|
| 74 |
|
| 75 | if (this.isLooking)
|
| 76 | {
|
| 77 | this.isLooking = false;
|
| 78 | double d0 = this.posX - this.entity.posX;
|
| 79 | double d1 = this.posY - (this.entity.posY + (double)this.entity.getEyeHeight());
|
| 80 | double d2 = this.posZ - this.entity.posZ;
|
| 81 | double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
|
| 82 | float f = (float)(MathHelper.atan2(d2, d0) * 180.0D / Math.PI) - 90.0F;
|
| 83 | float f1 = (float)(-(MathHelper.atan2(d1, d3) * 180.0D / Math.PI));
|
| 84 | this.entity.rotationPitch = this.updateRotation(this.entity.rotationPitch, f1, this.deltaLookPitch);
|
| 85 | this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, f, this.deltaLookYaw);
|
| 86 | }
|
| 87 | else
|
| 88 | {
|
| 89 | this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, this.entity.renderYawOffset, 10.0F);
|
| 90 | }
|
| 91 |
|
| 92 | float f2 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset);
|
| 93 |
|
| 94 | if (!this.entity.getNavigator().noPath())
|
| 95 | {
|
| 96 | if (f2 < -75.0F)
|
| 97 | {
|
| 98 | this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F;
|
| 99 | }
|
| 100 |
|
| 101 | if (f2 > 75.0F)
|
| 102 | {
|
| 103 | this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private float updateRotation(float p_75652_1_, float p_75652_2_, float p_75652_3_)
|
| 109 | {
|
| 110 | float f = MathHelper.wrapAngleTo180_float(p_75652_2_ - p_75652_1_);
|
| 111 |
|
| 112 | if (f > p_75652_3_)
|
| 113 | {
|
| 114 | f = p_75652_3_;
|
| 115 | }
|
| 116 |
|
| 117 | if (f < -p_75652_3_)
|
| 118 | {
|
| 119 | f = -p_75652_3_;
|
| 120 | }
|
| 121 |
|
| 122 | return p_75652_1_ + f;
|
| 123 | }
|
| 124 |
|
| 125 | public boolean getIsLooking()
|
| 126 | {
|
| 127 | return this.isLooking;
|
| 128 | }
|
| 129 |
|
| 130 | public double getLookPosX()
|
| 131 | {
|
| 132 | return this.posX;
|
| 133 | }
|
| 134 |
|
| 135 | public double getLookPosY()
|
| 136 | {
|
| 137 | return this.posY;
|
| 138 | }
|
| 139 |
|
| 140 | public double getLookPosZ()
|
| 141 | {
|
| 142 | return this.posZ;
|
| 143 | }
|
| 144 | }
|
| 145 |
|