001 package net.minecraft.entity.ai;
002
003 import net.minecraft.entity.Entity;
004 import net.minecraft.entity.EntityLiving;
005 import net.minecraft.util.MathHelper;
006
007 public class EntityLookHelper
008 {
009 private EntityLiving entity;
010
011 /**
012 * The amount of change that is made each update for an entity facing a direction.
013 */
014 private float deltaLookYaw;
015
016 /**
017 * The amount of change that is made each update for an entity facing a direction.
018 */
019 private float deltaLookPitch;
020
021 /** Whether or not the entity is trying to look at something. */
022 private boolean isLooking = false;
023 private double posX;
024 private double posY;
025 private double posZ;
026
027 public EntityLookHelper(EntityLiving par1EntityLiving)
028 {
029 this.entity = par1EntityLiving;
030 }
031
032 /**
033 * Sets position to look at using entity
034 */
035 public void setLookPositionWithEntity(Entity par1Entity, float par2, float par3)
036 {
037 this.posX = par1Entity.posX;
038
039 if (par1Entity instanceof EntityLiving)
040 {
041 this.posY = par1Entity.posY + (double)par1Entity.getEyeHeight();
042 }
043 else
044 {
045 this.posY = (par1Entity.boundingBox.minY + par1Entity.boundingBox.maxY) / 2.0D;
046 }
047
048 this.posZ = par1Entity.posZ;
049 this.deltaLookYaw = par2;
050 this.deltaLookPitch = par3;
051 this.isLooking = true;
052 }
053
054 /**
055 * Sets position to look at
056 */
057 public void setLookPosition(double par1, double par3, double par5, float par7, float par8)
058 {
059 this.posX = par1;
060 this.posY = par3;
061 this.posZ = par5;
062 this.deltaLookYaw = par7;
063 this.deltaLookPitch = par8;
064 this.isLooking = true;
065 }
066
067 /**
068 * Updates look
069 */
070 public void onUpdateLook()
071 {
072 this.entity.rotationPitch = 0.0F;
073
074 if (this.isLooking)
075 {
076 this.isLooking = false;
077 double var1 = this.posX - this.entity.posX;
078 double var3 = this.posY - (this.entity.posY + (double)this.entity.getEyeHeight());
079 double var5 = this.posZ - this.entity.posZ;
080 double var7 = (double)MathHelper.sqrt_double(var1 * var1 + var5 * var5);
081 float var9 = (float)(Math.atan2(var5, var1) * 180.0D / Math.PI) - 90.0F;
082 float var10 = (float)(-(Math.atan2(var3, var7) * 180.0D / Math.PI));
083 this.entity.rotationPitch = this.updateRotation(this.entity.rotationPitch, var10, this.deltaLookPitch);
084 this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, var9, this.deltaLookYaw);
085 }
086 else
087 {
088 this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, this.entity.renderYawOffset, 10.0F);
089 }
090
091 float var11 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset);
092
093 if (!this.entity.getNavigator().noPath())
094 {
095 if (var11 < -75.0F)
096 {
097 this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F;
098 }
099
100 if (var11 > 75.0F)
101 {
102 this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F;
103 }
104 }
105 }
106
107 private float updateRotation(float par1, float par2, float par3)
108 {
109 float var4 = MathHelper.wrapAngleTo180_float(par2 - par1);
110
111 if (var4 > par3)
112 {
113 var4 = par3;
114 }
115
116 if (var4 < -par3)
117 {
118 var4 = -par3;
119 }
120
121 return par1 + var4;
122 }
123 }