    001package net.minecraft.entity.ai;
    002
    003import net.minecraft.entity.Entity;
    004import net.minecraft.entity.EntityLiving;
    005import net.minecraft.util.MathHelper;
    006
    007public 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 d0 = this.posX - this.entity.posX;
    078            double d1 = this.posY - (this.entity.posY + (double)this.entity.getEyeHeight());
    079            double d2 = this.posZ - this.entity.posZ;
    080            double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
    081            float f = (float)(Math.atan2(d2, d0) * 180.0D / Math.PI) - 90.0F;
    082            float f1 = (float)(-(Math.atan2(d1, d3) * 180.0D / Math.PI));
    083            this.entity.rotationPitch = this.updateRotation(this.entity.rotationPitch, f1, this.deltaLookPitch);
    084            this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, f, this.deltaLookYaw);
    085        }
    086        else
    087        {
    088            this.entity.rotationYawHead = this.updateRotation(this.entity.rotationYawHead, this.entity.renderYawOffset, 10.0F);
    089        }
    090
    091        float f2 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset);
    092
    093        if (!this.entity.getNavigator().noPath())
    094        {
    095            if (f2 < -75.0F)
    096            {
    097                this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F;
    098            }
    099
    100            if (f2 > 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 f3 = MathHelper.wrapAngleTo180_float(par2 - par1);
    110
    111        if (f3 > par3)
    112        {
    113            f3 = par3;
    114        }
    115
    116        if (f3 < -par3)
    117        {
    118            f3 = -par3;
    119        }
    120
    121        return par1 + f3;
    122    }
    123}