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